因此,我正在编写一个可在AVPlayer中播放音乐的应用程序,我想在电话打进或其他原因结束音乐在后台播放时捕获该事件。

- (void)applicationWillResignActive:(UIApplication *)application

这是在应用程序中断时捕捉的正确方法,但是当用户完成电话通话时又如何呢?还有另一种方法,我可以在完成电话并设置应用程序后从停止播放的位置恢复播放器再次活跃?

我在这里先向您的帮助表示感谢。

编辑:答案

- (void) beginInterruption {
    if (playing) {
        playing = NO;
        interruptedWhilePlaying = YES;
        [self updateUserInterface];
    }
}

NSError *activationError = nil;
- (void) endInterruption {
    if (interruptedWhilePlaying) {
        [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
        [player play];
        playing = YES;
        interruptedWhilePlaying = NO;
        [self updateUserInterface];
    }
}


只要实现AVAudioSession委托,这就是答案

最佳答案

对于音频代码,要在AVAudioPlayerDelegate中实现的功能是:

 - (void)audioPlayerBeginInterruption:(AVAudioPlayer *) player;
 - (void)audioPlayerEndInterruption:(AVAudioPlayer *) player;

关于iphone - 当电话或其他事件关闭音乐播放器iPhone时捕获事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8639462/

10-10 20:42
查看更多