我需要帮助解决在MPMoviePlayerController和MPMusicPlayerController之间转换期间偶尔会出现在AudioToolBox(AVAudioSessionPropertyListener)中发生的EXC_BAD_ACCESS崩溃的问题。以下是呼叫追踪:
OS Version: iPhone OS 5.1 (9B176)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000009
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x3676ef78 objc_msgSend + 16
1 AVFoundation 0x31e60b1c _ZL30AVAudioSessionPropertyListenerPvmmPKv + 236
2 AudioToolbox 0x3630b300 AudioSessionPropertyListeners::CallPropertyListenersImp(unsigned long, unsigned long, void const*) + 268
3 AudioToolbox 0x3630b5de AudioSessionPropertyListeners::CallPropertyListeners(unsigned long, unsigned long, void const*) + 234
4 AudioToolbox 0x3630925a SSServer_AudioSessionInterruptionListenerMessage + 50
5 AudioToolbox 0x362b0d2c _XAudioSessionInterruptionListenerMessage + 56
6 AudioToolbox 0x36245cdc mshMIGPerform + 368
7 CoreFoundation 0x3532151c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
8 CoreFoundation 0x353214be __CFRunLoopDoSource1 + 134
9 CoreFoundation 0x3532030c __CFRunLoopRun + 1364
10 CoreFoundation 0x352a349e CFRunLoopRunSpecific + 294
11 CoreFoundation 0x352a3366 CFRunLoopRunInMode + 98
12 GraphicsServices 0x3659f432 GSEventRunModal + 130
13 UIKit 0x32399e76 UIApplicationMain + 1074
14 MyGreatApp 0x00054986 main (main.m:14)
15 MyGreatApp 0x00054944 start + 32
我的应用程序交替使用音乐播放器和电影播放器。电影播放器用于播放播客。每次播放新的播客时,电影播放器都会分配自动释放。当我完成电影播放器后,我删除了我设置的所有观察者,确保电影播放器已停止,然后重新启动音乐播放器。
以下是一些初始化电影播放器使用的相关代码:
[musicPlayer pause];
self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL: address] autorelease]; // Release the old moviePlayer and create a new one.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
// Register to receive a notification when the movie playback state changes (specifically looking for the state of interrupted).
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[self.moviePlayer play];
电影可以在播放后完成,或者用户可以通过按快进按钮来终止电影。以下是处理这两种情况的相关代码:
-(IBAction) fastForwardMovie: (id) sender {
// The self.moviePlayer is still playing, but the user wants to skip the rest of the movie/podcast.
// Lets fake a movie playback did finish event
NSNotification *notification = [NSNotification notificationWithName:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self moviePlayBackDidFinish: notification]; // Fake a finished playback so that the music player can be restarted
return;
}
// Notification called when the movie FINISHED playing.
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
// Remove further notifications until the next time we need the movie player
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer] ;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer] ;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer] ;
// Get rid of the movie player
self.moviePlayer.initialPlaybackTime = -1; // seems that the best way to kill the movie player is first setting the playback time to -1
[self.moviePlayer stop]; // followed by stopping the player
[musicPlayer play];
}
电影(播客)结束后,即使发生崩溃(但应用终止),音乐也会播放。这是因为我使用的是iPod音乐播放器,但这可能表明问题与电影播放器有关。
最后一点。我试图仔细遵循Apple的音频会话编程指南的指导原则,它指出当所需的配置要使所有音频混合时,您应该“使用可混合的类别配置配置音频会话”和“利用电影播放器的默认useApplicationAudioSession值为YES“。
任何人都可以帮我弄清楚我可能做错了什么导致AVAudioSessionPropertyListener与EXC_BAD_ACCESS崩溃?或者至少可以给我一个关于如何隔离这个问题以确定根本原因的建议吗?
答案 0 :(得分:1)
请尝试拨打[musicPlayer performSelectorOnMainThread:@selector(play) ...]
?
答案 1 :(得分:0)
我们刚刚跟踪了一次非常类似的崩溃。
我们的结果如下所述:
https://github.com/mattgallagher/AudioStreamer/issues/6
特别是,MyAudioSessionInterruptionListener(或传递给的回调的名称) AudioSessionInitialize)并且它的inClientData在注册后无法更改,因此即使底层对象已被解除分配,回调也必须始终做一些合理的事情。
为AudioStream建议的解决方案是使用静态变量,并确保它指向当前对回调感兴趣的对象,并且永远不会指向解除分配的对象 - 重要的是不要使用inClientData。