objective-c – 当应用程序转到后台时,如何在自定义应用程序中播放来自ipod的音乐

前端之家收集整理的这篇文章主要介绍了objective-c – 当应用程序转到后台时,如何在自定义应用程序中播放来自ipod的音乐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个音乐应用程序,即使应用程序进入后台并使用beginReceivingRemoteControlEvents,音乐也会继续播放,以便用户可以通过在iPhone / iPad上单击主页按钮两次来控制播放状态.

我浏览了Apple开发的“音乐应用程序”示例,它使用MPMusicPlayerController从iPod库中获取歌曲,但是一旦应用程序进入后台并且不支持beginReceivingRemoteControlEvents,它就会停止播放该歌曲.

我检查了一些文章,说它使用AVPlayer,但它不支持从iPod库中获取音乐,因此当应用程序进入后台并且通过remoteEvents控制时,我陷入了如何播放音乐的困境.

解决方法

iPodMusicPlayer将在后台播放,但通过iOS iPod音乐播放器播放.您的应用不会在后台收到通知.为此,您需要设置AVAudioSession并使用AVPlayer.

Check here for more information

Apple has a great resource here

来自Apple Docs:

Use an MPMusicPlayerController object,or music player,to play media items from the device iPod library. There are two types of music player:

The application music player plays music locally within your
application. It is not aware of the iPod app’s now-playing item,nor
does it affect the iPod state.

The iPod music player employs the
built-in iPod app on your behalf. On instantiation,it takes on the
current iPod application state,such as the identification of the
now-playing item. If a user switches away from your app while music is
playing,that music continues to play. The iPod app then has your
music player’s most recently-set repeat mode,shuffle mode,playback
state,and now-playing item.

确保你正在使用

+ iPodMusicPlayer

并不是

+ applicationMusicPlayer

Here is a good article on how to import the library into an AVAudioSession

在info.plist文件添加一行,名称为“UIBackgroundModes”输入“audio”作为第一个键.

猜你在找的Xcode相关文章