2

我需要帮助解决在 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 一起崩溃?或者至少你能给我一个关于如何隔离这个问题以确定根本原因的建议吗?

4

2 回答 2

1

换个电话试试[musicPlayer performSelectorOnMainThread:@selector(play) ...]

于 2012-06-12T05:38:53.197 回答
0

我们刚刚跟踪了一次非常相似的崩溃。

我们的结果如下所述:

https://github.com/mattgallagher/AudioStreamer/issues/6

特别是 MyAudioSessionInterruptionListener(或传递给 AudioSessionInitialize 的回调的名称)及其 inClientData 在注册后无法更改,因此即使底层对象已被释放,回调也必须始终做一些明智的事情。

为 AudioStream 建议的解决方案是使用静态变量,并确保它指向当前对回调感兴趣的对象,而不是指向已释放的对象 - 重要的是不要使用 inClientData。

于 2012-12-04T17:22:24.540 回答