【问题标题】:Catch the event when a phone call or some other event shuts off the music player iPhone当电话或其他事件关闭音乐播放器 iPhone 时捕捉事件
【发布时间】:2011-12-26 22:12:14
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    相关资源
    最近更新 更多