如何在您的应用程序中接收远程控制事件的同时从 iPod 应用程序播放音乐? [英] How can you play music from the iPod app while still receiving remote control events in your app?

查看:7
本文介绍了如何在您的应用程序中接收远程控制事件的同时从 iPod 应用程序播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试让用户从他们的 iPod 库中选择歌曲来收听,但我仍然想在我的应用中接收远程控制通知(耳机、锁屏 OSD 等),以便我可以做一些额外的东西.到目前为止,我可以播放 iPod 音乐或耳机事件,但不能同时播放.

Ok, I'm trying to let a user choose songs from their iPod library to listen to, but I still want to receive remote control notifications (headphones, lock screen osd, etc.) in my app so I can do some extra things. So far I can get either iPod music playing, or headphone events, but not both simultaneously.

这是我目前所知道的......

Here's what I know so far...

  1. 如果您使用 MPMusicPlayer,您可以轻松地以编程方式访问整个音乐库.但是,无论您使用的是 applicationMusicPlayer 还是 ipodMusicPlayer,它而不是您的应用会接收远程通知.

  1. If you use the MPMusicPlayer, you can easily have programmatic access to the entire music library. However, it, not your app, receives the remote notifications regardless if you use applicationMusicPlayer or ipodMusicPlayer.

如果您使用 AVAudioPlayer(Apple 推荐的用于您应用中大多数声音的播放器),您可以轻松获得远程通知,但它本身无法访问 iPod 库.

If you use AVAudioPlayer (Apple's recommended player for most sounds in your app), you can easily get remote notifications, but it doesn't natively have access to the iPod library.

AVAudioPlayer 可以使用资产 URL 进行初始化,并且 iPod 库(MPMediaItem 类型)中的曲目确实有一个 URL 属性,该属性返回一个 NSURL 实例,文档明确说明它可以与 AVAsset 对象一起使用,但是当你尝试使用该 NSURL 初始化 AVAudioPlayer,它失败了.(我在 iPod 中使用了正在播放"的曲目,它是一个 MP3,它确实返回了一个有效的 NSURL 对象,但初始化失败.更糟糕的是,当它是一个 Audible.com 文件时,NSURL 属性完全返回 nil.)

AVAudioPlayer can be initialized with an asset URL, and tracks in the iPod library (type MPMediaItem) do have a URL property that returns a NSURL instance which the documentation says its explicitly for use with AVAsset objects, but when you try initializing the AVAudioPlayer with that NSURL, it fails. (I used the 'now playing' track in iPod which was a MP3 and it did return a valid NSURL object but initialization failed. Even worse, when it was an Audible.com file, the NSURL property flat-out returned nil.)

如果您尝试使用 AVAudioPlayer 的实例来获取远程事件(例如,使用空白声音文件),然后同时使用 MPMusicPlayer 类播放 iPod 音乐,您可以远程控制访问,直到您真正启动 iPod由于您的音频会话被停用并且系统音频会话变为活动状态,因此您在播放时丢失了它.

If you try using an instance of the AVAudioPlayer to get remote events (say, with a blank sound file), then simultaneously use the MPMusicPlayer class to play iPod music, you have remote control access until you actually start iPod playback at which time you lose it since your audio session gets deactivated and the system audio session becomes active.

如果您尝试与 #4 相同的方法,但您将音频会话的类别设置为可混合变体,您的会话不会被停用,但一旦 iPod 开始播放,您仍然会失去远程控制功能.

If you try the same as #4 but you instead set the audio session's category to a mixable variant, your session doesn't get deactivated, but you still lose remote control capability once the iPod starts playing.

简而言之,每当播放 MPMusicPlayer 时,我似乎都无法获得远程事件,而且除了使用 MPMusicPlayer 之外,我不知道还有其他方法可以播放 iPod 库中的内容.

In short, whenever MPMusicPlayer is playing, I can't seem to get remote events, and I don't know of any other way to play content from the iPod's library other than by using MPMusicPlayer.

任何关于如何解决这个问题的建议都将受到欢迎.有创意或完全疯狂.没关系,只要它有效.

ANY suggestions on how to get around this would be welcome. Creative or flat-out crazy. Don't care so long as it works.

有人吗?任何人?布勒?比勒?

Anyone? Anyone? Bueller? Bueller?

M

推荐答案

哈!解决了!我知道这是可能的!(感谢 Async-games.com 支持!)

HA! Solved! I knew it was possible! (Thanks Async-games.com support!)

以下是如何在您的应用中播放 iPod 音乐、支持后台以及让您的应用接收远程控制通知.

Here's how to play iPod music in your app, with background support, and with your app receiving the remote control notifications.

您必须使用 AVPlayer(但不是 AVAudioPlayer.不知道为什么会这样!)使用您从库选择器(或 MPMusicPlayerController 中的当前项目或其他任何地方)获得的 MPMediaItem 中的资产 URL 进行初始化,然后设置音频会话的类别为 Playable(不要启用混合覆盖,否则您将丢失远程事件!)并将适当的键添加到您的 info.plist 文件中,告诉操作系统您的应用程序想要支持背景音频.

You have to use AVPlayer (but not AVAudioPlayer. No idea why that is!) initialized with the asset URL from the MPMediaItem you got from the library picker (or current item in the MPMusicPlayerController or wherever), then set the audio session's category to Playable (do NOT enable the mixing override or you'll lose the remote events!) and add the appropriate keys to your info.plist file telling the OS your app wants to support background audio.

大功告成!

这使您可以在后台播放 iPod 库中的项目(由于某种原因,Audible.com 文件除外!),并且仍然可以获取远程事件.当然,因为这是你的音频播放器,它与 iPod 应用程序是分开的,并且会中断 iPod 应用程序,你必须做更多的工作,但这些都是休息时间!

This lets you play items from your iPod library (except Audible.com files for some reason!) in the background and still get remote events. Granted since this is your audio player which is separate from, and will interrupt the iPod app, you have to do more work, but those are the breaks!

该死的……我只是希望它能与 Audible.com 文件一起使用.(对于那些感兴趣的人,它没有的原因是声音文件的资产 URL 返回 nil.臭!但是你能做什么!)

Damn though... I just wished it worked with Audible.com files. (For those interested, the reason it doesn't is the asset URL for an audible file returns nil. Stinks! But what can ya do!)

这篇关于如何在您的应用程序中接收远程控制事件的同时从 iPod 应用程序播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆