加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 当应用程序转到后台时,如何在自定义应用程序中播

发布时间:2020-12-14 17:37:53 所属栏目:百科 来源:网络整理
导读:我正在开发一个音乐应用程序,即使应用程序进入后台并使用beginReceivingRemoteControlEvents,音乐也会继续播放,以便用户可以通过在iPhone / iPad上单击主页按钮两次来控制播放状态. 我浏览了Apple开发的“音乐应用程序”示例,它使用MPMusicPlayerController
我正在开发一个音乐应用程序,即使应用程序进入后台并使用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”作为第一个键.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读