Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialogechi1
14,455 PointsXcode 6 beta, sound does not play, here's a workaround
This my response to this question (see below): https://teamtreehouse.com/forum/hi-i-got-the-video-tutorial-and-not-sound-in-the-app
I also wanted to find out whether there is any way to do this with the AudioToolbox in Xcode? Any help would be amazing! Thanks in advance
Hi guys, I figured out a workaround for the sound effect. I am also using Xcode 6 beta:
1: add the AVFoundation.framework (same way that we added the AudioToolbox Framework in the video).
2: Import the AVFoundation/AVFoundation.h header, by adding it to the top of the ViewController.m file
3: Create an instance variable in the view controller to hold an AVAudioPlayer object. I called mine _soundFX
@interface ViewController() {
AVAudioPlayer *_soundFX;
}
4: We've already constructed the URL to the sound file. Comment out or delete the AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect); line. Below it, add:
_soundFX = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
5: Then, in the makePrediction method that we added in a previous video, you can simply call the play method on _soundFX
[_soundFX play];
Hope this helps!
Best, Ogechi
13 Answers
Leo Picado
9,182 Pointsthat was it for me in Xcode 6 stable, thank you!
Nirav Shah
9,468 PointsThanks
Andrew King
3,411 PointsI tweeted Amit, hopefully he'll be able to shed some light.
ogechi1
14,455 PointsThank you!
Shinji Takai
19,816 PointsThanks :)
Levi Lais
5,975 PointsPERFECT! Thanks!
Raqibul Hayder
2,386 PointsThat worked like a charm!!
Mike Lange
794 PointsNice one ogechil, thanks for that!
One question however - any reason you're using the _ for your soundFX variable? I don't think we need to use the underscore.
ogechi1
14,455 PointsYou are correct, Mike -- it is a variable name, so any allowable name will do. Here's some insight into that particular Objective C idiom: A motivation for ivar decorations :)
P.S. ivar = instance variable
Morgan Grant
1,434 PointsThanks, worked great :)
Leo Zhang
3,389 PointsThanks for that! It works perfectly!!!
Maarten Tolhuijs
13,349 PointsThanks! Works for me too!
Shaun Kelly
5,648 PointsMine doesn't work and an error pops up on the line _soundFC [_soundFX play]; that says Expected ']' ???
#import "ViewController.h"
#import "THCrystalBall.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@end
@implementation ViewController {
AVAudioPlayer *_soundFX;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"crystabl_ball" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
_soundFX = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
self.crystalBall = [[THCrystalBall alloc] init];
self.backgroundImageView.animationImages= [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"CB00001"],
[UIImage imageNamed:@"CB00002"],
[UIImage imageNamed:@"CB00003"],
[UIImage imageNamed:@"CB00004"],
[UIImage imageNamed:@"CB00005"],
[UIImage imageNamed:@"CB00006"],
[UIImage imageNamed:@"CB00007"],
[UIImage imageNamed:@"CB00008"],
[UIImage imageNamed:@"CB00009"],
[UIImage imageNamed:@"CB00010"],
[UIImage imageNamed:@"CB00011"],
[UIImage imageNamed:@"CB00012"],
[UIImage imageNamed:@"CB00013"],
[UIImage imageNamed:@"CB00014"],
[UIImage imageNamed:@"CB00015"],
[UIImage imageNamed:@"CB00016"],
[UIImage imageNamed:@"CB00017"],
[UIImage imageNamed:@"CB00018"],
[UIImage imageNamed:@"CB00019"],
[UIImage imageNamed:@"CB00020"],
[UIImage imageNamed:@"CB00021"],
[UIImage imageNamed:@"CB00022"],
[UIImage imageNamed:@"CB00023"],
[UIImage imageNamed:@"CB00024"],
[UIImage imageNamed:@"CB00025"],
[UIImage imageNamed:@"CB00026"],
[UIImage imageNamed:@"CB00027"],
[UIImage imageNamed:@"CB00028"],
[UIImage imageNamed:@"CB00029"],
[UIImage imageNamed:@"CB00030"],
[UIImage imageNamed:@"CB00031"],
[UIImage imageNamed:@"CB00032"],
[UIImage imageNamed:@"CB00033"],
[UIImage imageNamed:@"CB00034"],
[UIImage imageNamed:@"CB00035"],
[UIImage imageNamed:@"CB00036"],
[UIImage imageNamed:@"CB00037"],
[UIImage imageNamed:@"CB00038"],
[UIImage imageNamed:@"CB00039"],
[UIImage imageNamed:@"CB00040"],
[UIImage imageNamed:@"CB00041"],
[UIImage imageNamed:@"CB00042"],
[UIImage imageNamed:@"CB00043"],
[UIImage imageNamed:@"CB00044"],
[UIImage imageNamed:@"CB00045"],
[UIImage imageNamed:@"CB00046"],
[UIImage imageNamed:@"CB00047"],
[UIImage imageNamed:@"CB00048"],
[UIImage imageNamed:@"CB00049"],
[UIImage imageNamed:@"CB00050"],
[UIImage imageNamed:@"CB00051"],
[UIImage imageNamed:@"CB00052"],
[UIImage imageNamed:@"CB00053"],
[UIImage imageNamed:@"CB00054"],
[UIImage imageNamed:@"CB00055"],
[UIImage imageNamed:@"CB00056"],
[UIImage imageNamed:@"CB00057"],
[UIImage imageNamed:@"CB00058"],
[UIImage imageNamed:@"CB00059"],
[UIImage imageNamed:@"CB00060"], nil];
self.backgroundImageView.animationDuration = 2.0f;
self.backgroundImageView.animationRepeatCount = 1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Motion Events
//SHAKE EVENT
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
self.predictionLabel.text = nil;
self.predictionLabel.alpha = 0.0f;
}
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
[self makePredictions];
}
}
-(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
#pragma mark - Touch Events
//TOUCH EVENT
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.predictionLabel.text = nil;
self.predictionLabel.alpha = 0.0;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self makePredictions];
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
#pragma mark - Prediction
- (void) makePredictions {
_soundFX [_soundFX play];
[self.backgroundImageView startAnimating];
self.predictionLabel.text = [self.crystalBall randomPredictions];
[UIView animateWithDuration:6.0 animations:^{
self.predictionLabel.alpha = 1.0f;
}];
}
Cody HtueAung
Courses Plus Student 3,489 Pointsjust [_soundFX play]; not _soundFX [_soundFX play];
Shaun Kelly
5,648 Pointsthanks but now it's saying there was a Thread 1: breakpoint 1.1 on my line NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; ?
Shaun Kelly
5,648 PointsMy code is wrong again, and saying Unknown type name 'AVAudioPlayer' and use of undeclared identifier 'AVAudioPlayer'
#import "ViewController.h"
#import "Model.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController () {
AVAudioPlayer *_soundFX;
}
@end
@implementation ViewController {
SystemSoundID *soundEffect;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"crystal_ball"ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
_soundFX = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
Iván Jesús Fanjul Cosént
3,882 PointsThank you very much!! worked perfectly
Szymon Kadłuczka
8,762 PointsSzymon Kadłuczka
8,762 PointsWorked for me, as well. Thanks!
iPhone 5 (8.0) Simulator in Xcode 6