使用AVAudioPlayer向工作的应用程序添加声音,它会停止/挂起在prepareToPlay上。注意它也停止/挂起播放。几次打“继续程序执行”使应用程序恢复。只有在模拟器中运行程序时才会出现此问题。
使用Xcode 4.6
代码段:
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
堆栈跟踪
0 __cxa_throw 21 -[AVAudioPlayer prepareToPlay] 22 -[ViewController viewDidLoad] . . .
解决方法
问题是我通常用断点设置为“所有异常”开发,抛出的实际异常是__cxa_throw。这显然是在用于实现AVAudioPlayer的C库中。通过将断点更改为“所有Objective-C异常”程序运行正常。 (这可以通过编辑断点并将异常字段更改为Objective-C来完成。