在我的应用程序中,我使用AVAudioPlayer播放音频;一切正常,可以在几分钟内正常工作,但有时会崩溃,这是我的代码和控制台输出,但我不知道如何解决我的问题...
我有这个代码:
- (void) viewDidLoad{
[super viewDidLoad];
soundID = [[AVAudioPlayer alloc]init];
}
- (void)playAudioReturnLettera{
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
@"returnObject.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
[soundID initWithContentsOfURL:filePath error:nil];
soundID.delegate = self; //<-- here I have the problem (Thread 1)
[soundID prepareToPlay];
[soundID play];
}
这是控制台中的输出:
10:13:07.639 <0x3fbdad98> shm_open failed: "AppleAudioQueue.100.778" (23) flags=0x2 errno=24
2012-04-26 10:13:07.643 Game[155:418b] 10:13:07.643 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.779" (23) flags=0x2 errno=24
2012-04-26 10:13:07.655 Game[155:418f] 10:13:07.655 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.780" (23) flags=0x2 errno=24
2012-04-26 10:13:07.664 Game[155:4193] 10:13:07.664 <0x2fffd000> shm_open failed:
"AppleAudioQueue.100.781" (23) flags=0x2 errno=24
2012-04-26 10:13:07.680 Game[155:4197] 10:13:07.680 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.782" (23) flags=0x2 errno=24
2012-04-26 10:13:07.696 Game[155:419b] 10:13:07.696 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.783" (23) flags=0x2 errno=24
2012-04-26 10:13:07.711 Game[155:419f] 10:13:07.711 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.784" (23) flags=0x2 errno=24
2012-04-26 10:13:07.727 Game[155:41a3] 10:13:07.727 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.785" (23) flags=0x2 errno=24
2012-04-26 10:13:07.744 Game[155:41a7] 10:13:07.744 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.786" (23) flags=0x2 errno=24
2012-04-26 10:13:07.759 Game[155:41ab] 10:13:07.759 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.787" (23) flags=0x2 errno=24
2012-04-26 10:13:07.775 Game[155:41af] 10:13:07.775 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.788" (23) flags=0x2 errno=24
2012-04-26 10:13:07.792 Game[155:41b3] 10:13:07.793 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.789" (23) flags=0x2 errno=24
2012-04-26 10:13:07.807 Game[155:41b7] 10:13:07.807 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.790" (23) flags=0x2 errno=24
2012-04-26 10:13:07.823 Game[155:41bb] 10:13:07.823 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.791" (23) flags=0x2 errno=24
2012-04-26 10:13:07.840 Game[155:41bf] 10:13:07.841 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.792" (23) flags=0x2 errno=24
2012-04-26 10:13:07.857 Game[155:41c3] 10:13:07.857 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.793" (23) flags=0x2 errno=24
2012-04-26 10:13:07.873 Game[155:41c7] 10:13:07.873 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.794" (23) flags=0x2 errno=24
2012-04-26 10:13:07.890 Game[155:41cb] 10:13:07.890 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.795" (23) flags=0x2 errno=24
2012-04-26 10:13:07.905 Game[155:41cf] 10:13:07.906 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.796" (23) flags=0x2 errno=24
2012-04-26 10:13:07.921 Game[155:41d3] 10:13:07.921 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.797" (23) flags=0x2 errno=24
2012-04-26 10:13:07.938 Game[155:41d7] 10:13:07.938 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.798" (23) flags=0x2 errno=24
2012-04-26 10:13:10.478 Game[155:707] *** -[AVAudioPlayer setDelegate:]: message sent to deallocated instance 0x546bb50
最佳答案
您在已初始化的对象(initWithContentsOfURL:error:
)上调用初始化方法(soundID
),而不存储该方法返回的对象。
将分配和初始化保持在一起(并且仅初始化一次)是一个好主意,因此您的代码应如下所示:
- (void) viewDidLoad{
[super viewDidLoad];
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
@"returnObject.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
}
- (void)playAudioReturnLettera{
[soundID prepareToPlay];
[soundID play];
}
关于ios - iOS:AVAudioPlayer出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10329689/