当电话或其他事件关闭音乐播放器iPhone时,赶上该事件

时间:2011-12-26 22:12:15

标签: iphone objective-c ios

所以我正在编写一个在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委托

,这就是答案

1 个答案:

答案 0 :(得分:1)

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

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