如何告诉MPNowPlayingInfoCenter音乐是正在播放还是暂停?

11

我似乎无法让iOS在远程音频控制中显示正确的播放/暂停按钮。我确实接收到了远程控制事件,并设置了nowPlayingInfo字典的所有值。

一切都正常工作,甚至在锁屏界面上看到了封面照片。 除了暂停/播放按钮之外。 它始终看起来像是暂停,即使我的AVAudioPlayer正在播放。 它发送暂停事件,而不管播放状态如何。

我该如何通知iOS AVAudioPlayer已暂停,并且它现在应该在远程控制按钮栏中显示播放按钮?


1
如果你解决了这个问题,请发布你的解决方案。 - SG1
在播放状态更改(例如暂停)时,您是否会更新MPNowPlayingInfoCenter? - chris
4个回答

6

请确保设置了MPNowPlayingInfoPropertyPlaybackRate属性。0.0f表示暂停,1.0f表示播放。在更改这些值时,您还需要设置MPNowPlayingInfoPropertyElapsedPlaybackTime。

以下是示例代码,其中updateMetadata是将这些更改应用于MPNowPlayingInfoCenter.nowPlayingInfo字典的函数。这将向中心指示播放器已暂停。

[self updateMetadata:[NSDictionary dictionaryWithObjectsAndKeys:
     [NSNumber numberWithDouble:audioFile.player.currentTime], 
      MPNowPlayingInfoPropertyElapsedPlaybackTime,
     [NSNumber numberWithFloat:0.0f],  
      MPNowPlayingInfoPropertyPlaybackRate,
      nil]];

0

以下是示例代码,其中updateMetadata是将更改应用于MPNowPlayingInfoCenter.nowPlayingInfo字典的函数。 这将向中心表明播放器已暂停。在Swift中。

self.updateMetadata(NSDictionary.dictionaryWithValuesForKeys(NSNumber(Double(audioFile.player.currentTime))),MPNowPlayingInfoPropertyElapsedPlaybackTime,NSNumber(numberWithFloat:0.0f),MPNowPlayingInfoPropertyPlaybackRate,nil)

0
我今天遇到了这个问题:我发现我在录音并播放时,按钮显示为暂停符号。
当我停止AVAudioRecorder的录制后,暂停按钮变成了播放按钮。

0

很多时候,问题只是iPhone模拟器。一旦您使用AVAudioPlayer实例的play()函数,远程控制栏应自动切换暂停/播放。如果您遇到此功能无法正常工作的问题,请尝试在设备上运行程序。

要切换按钮,您不需要设置任何MPNowPlayingInfoCenterplayingInfo,也不需要保持活动的AVAudioSession


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接