Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer failing on iOS simulator only

I have this code, which seems to be pretty standard when it comes to play sound in an iOS app.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"click"
                                     ofType:@"wav"]];

NSError *error;
self._audioPlayer = [[AVAudioPlayer alloc]
                initWithContentsOfURL:url
                error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    self._audioPlayer.delegate = self;
    [self._audioPlayer prepareToPlay];
    [self._audioPlayer play];
}

it works absolutely fine on the device.

On the simulator however, it doesn't play anything, just print this error message (which is caught somewhere at a low level as the app doesn't crash).

2014-05-14 18:10:46.121 sample_app[7684:14303] 18:10:46.121 ERROR:     189: EXCEPTION thrown ('fmt?'): -

(this error can be printed many times)

when debugging, everything looks OK, the nserror stays nil, url is valid, etc.

audioPlayerDidFinishPlaying:successfully: is event called with 1.

Anybody has had the same issue or has a suggestion regarding what's wrong with my environment?

Many Thanks

like image 925
MikaelW Avatar asked May 14 '14 17:05

MikaelW


1 Answers

I had this same problem, and found a strange solution. Try this:

  1. Quit the simulator
  2. Open your OSX system prefs and select Sound.
  3. Select Output as "Headphones"
  4. Select Input to "Internal Microphone" (even though you would think this shouldn't make any difference for playing audio on the simulator).
  5. Run your app

Don't know why this works, must be a simulator bug. If you make these changes while the simulator is running, you'll notice a log message pop up in Xcode warning you to restart the simulator.

like image 121
inorganik Avatar answered Oct 19 '22 19:10

inorganik