ios - 在后台播放Apple Music

标签 ios audio mpmusicplayercontroller

我正在使用[MPMusicPlayerController applicationMusicPlayer]播放Apple Music。 (AVPlayer和AVAudioPlayer不适用于Apple Music。)

在后台时,我需要知道歌曲何时结束,以便播放下一首歌曲。不幸的是,当应用程序在后台运行时,不会触发MPMusicPlayerControllerPlaybackStateDidChangeNotification。

因此,我使用beginBackgroundTaskWithExpirationHandler创建了一个后台任务,该任务每秒触发一次NSTimer,以检查歌曲的结尾。我已经设置了音频背景模式,但是我的任务仍然只有3分钟。据我了解,如果我使用音频背景模式,我的任务将获得无限的时间。那不是真的吗如果没有,我该如何处理?当然,其他人也遇到过这种情况。

最佳答案

备查:

我发现MPMusicPlayerController永远不会使用applicationMusicPlayer或systemMusicPlayer来使用音频UIBackgroundMode。因此,您永远不会获得超过3分钟的背景时间。但是,AVAudioPlayer确实启用了音频背景模式。因此,我的(丑陋)解决方案是在后台使用AVAudioPlayer循环播放无声MP3。我要假设苹果不会抗议,这似乎是对无声音频的合法使用。请注意,要同时播放AVAudioPlayer和MPMusicPlayerController,必须为AVAudioSession设置AVAudioSessionCategoryOptionMixWithOthers。

关于ios - 在后台播放Apple Music,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687912/

相关文章:

ios - 内存使用量不断增加

ios - 从 UIColor 获取 MidColor

ios - 将存档上传到 App Store 时,Xcode 卡在 "Copying media to the package"上

DICOM 支持的音频格式

iphone - AudioQueue 吃掉了我的缓冲区(前 15 毫秒)

ios - (iOS MPMusicPlayerNotification) app在后台时如何接收通知

ios - 显示附近的其他用户吗?

audio - 为什么使用 `ffmpeg -i input.mp4 -i music.mp3 output.mp4` 向视频添加背景音乐不起作用?

ios - MPMusicPlayerController和iOS 11.3

iphone - 帮助我选择合适的iPhone音频类-MPMoviePlayer与AVAudioPlayer与MPMusicPlayer