Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode stops on prepareToPlay

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]
.
.
.
like image 763
zermat Avatar asked Mar 13 '13 21:03

zermat


2 Answers

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.

like image 62
zermat Avatar answered Nov 06 '22 09:11

zermat


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.

like image 35
xazp Avatar answered Nov 06 '22 09:11

xazp