处理多个AVAudioPlayers的中断

问题描述 投票:0回答:2

当我有一个以上的AVPlayer时,是否可以通过AVPLayer委托来处理中断?我已经尝试了几个东西,在电话后再次启动两个AVPlayer,但仍然只有一个响应并启动,第二个暂停,什么也没做。

iphone avaudioplayer interrupt
2个回答
0
投票

我的猜测是你需要保留你的players数组,以便你可以通过它们进行轮询并暂停/启动每一个。您可以在创建可在audioPlayerBeginInterruption:audioPlayerEndInterruption:中访问的播放器时将每个数组添加到数组中

Apple的文档暗示中断只处理一个player ...如果类已经声明了players的方法输入而不是player那么我会说你可能刚刚完成了一个for循环来轮询它们,如下面的代码:

AVAudioPlayer *player; 
for (player in players) {

    [player pause];
    //or [player play];
}

http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html#//apple_ref/occ/intfm/AVAudioPlayerDelegate/audioPlayerBeginInterruption


0
投票

我相信你已经解决了这个问题,但我还是想记录我的答案。我刚刚在iPhone上测试了这个(4.3.2),我的应用程序正在使用两个不同的AVAudioPlayers播放两种不同的声音。在接到电话时,我会为每个玩家中断两种中断方法:

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

所以你可以很容易地重新启动两个玩家:

- (void) audioPlayerEndInterruption:(AVAudioPlayer *) player {
  NSLog (@"Interruption ended. Resuming audio playback.");
  [player prepareToPlay];
  [player play];
}
© www.soinside.com 2019 - 2024. All rights reserved.