21

Using AVAudioPlayer to add sound to a working application, it stops / hangs on prepareToPlay. Note it also stops / hangs on play. Hitting "Continue program execution" several times makes the application resume. This problem only occurs when running the program in the simulator.

Using Xcode 4.6

Code Snippets:

ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
}

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initilizeAudio];
}

- (void)initilizeAudio
{
    NSError *error = nil;
    NSURL *audioURL = [[NSBundle mainBundle] URLForResource:@"Background" withExtension:@"mp3"];

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
    }
    else
    {
        [self.audioPlayer setDelegate:self];
        [self.audioPlayer prepareToPlay];
    }
}

@end

Stack trace

0 __cxa_throw
21 -[AVAudioPlayer prepareToPlay]
22 -[ViewController viewDidLoad]
.
.
.
zermat
  • 991
  • 9
  • 15
  • 5
    Apparently I am not allowed to answer my own questions yet. so providing the answer in a comment. The problem was I normally develop with a breakpoint set to "All Exceptions", and the actual exception thrown was __cxa_throw. Which apparently turns out to be in C++ libraries that are used to implement AVAudioPlayer. By changing the breakpoint to "All Objective-C Exceptions" the program ran fine. (This can be done by editing the breakpoint and changing the Exception field to Objective-C.) – zermat Mar 13 '13 at 21:44
  • 1
    According to [This post on Meta](http://meta.stackexchange.com/a/17467) you have to wait for 8 hours if you have less then 100 rep. You can accept your own answer 48 hours later. – Sebastian Mar 13 '13 at 22:03

2 Answers2

67

The problem was I normally develop with a breakpoint set to "All Exceptions", and the actual exception thrown was __cxa_throw. Which apparently turns out to be in C++ libraries that are used to implement AVAudioPlayer. By changing the breakpoint to "All Objective-C Exceptions" the program ran fine. (This can be done by editing the breakpoint and changing the Exception field to Objective-C.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
zermat
  • 991
  • 9
  • 15
  • 1
    Awesome, I am glad you posted this because I was scratching my head on this one!!! – LilMoke Mar 26 '13 at 08:55
  • 1
    You sir, saved my day! I can't thank you enough! I had the exact same problem and it was driving me nuts. :-) – thomas May 01 '13 at 06:35
  • Thank you! I was starting to scratch my head since it suddenly stopped working without any changes in my code. It appears that I too had activated an "all exceptions" breakpoint.... – Erwan Feb 04 '14 at 03:23
  • Nice one!! I've been scratching my head at this for the last two years -- prepareToPlay and play always crash in newer iOS versions on the simulator. Changed the exception breaks as you suggested...problem solved. Thanks a million! – Mike Critchley Apr 09 '18 at 23:24
4

I had this problem too. The solution above works, but I think there's a better way to fix it.

The errors the author suggests ignoring are indicating that the mac is trying to send audio to a device that isn't working. I'm guessing the code is written in C, so turning the exceptions to "objective-c only" does silence the exception.

The real fix is to get the audio device working again. For me, the reason for the failure was that I was switching around some audio input and output, but (it seems) IOS simulator had remembered the old settings. So when I tried the various steps in this thread:

Error '!dat' trying to set the (null) audio devices' sample rate

I got the audio to work again, and it stopped throwing any exceptions.

Community
  • 1
  • 1
xazp
  • 71
  • 3
  • Isn't the problem rather that there is no audio hardware on the simulator and that this seems to cause an intermediate C++ exception which can (should) be ignored (my mechanism suggested in accepted answer). – Drux Feb 25 '15 at 10:49