如何从 iPod 应用程序播放音乐,同时仍在应用程序中接收远程控制事件?
好的,我试图让用户从 iPod 库中选择歌曲来听,但我仍然希望在我的应用程序中接收远程控制通知(耳机、锁定屏幕 osd 等),这样我就可以做一些额外的事情。到目前为止,我可以播放 iPod 音乐或耳机事件,但不能同时播放两者。
这是我到目前为止所知道的...
如果您使用 MPMusicPlayer,您可以轻松地以编程方式访问整个音乐库。但是,无论您使用 applicationMusicPlayer 还是 ipodMusicPlayer,它(而不是您的应用程序)都会接收远程通知。
如果您使用 AVAudioPlayer(Apple 推荐的播放器,可播放您应用中的大多数声音),您可以轻松获取远程通知,但它本身无法访问 iPod 库。
AVAudioPlayer 可以使用资源 URL 进行初始化,并且 iPod 库中的曲目(MPMediaItem 类型)确实有一个 URL 属性,该属性返回一个 NSURL 实例,文档明确指出该实例与 AVAsset 对象一起使用,但是当您尝试初始化AVAudioPlayer 与该 NSURL,它失败。 (我在 iPod 中使用了“正在播放”的曲目,它是 MP3,它确实返回了一个有效的 NSURL 对象,但初始化失败。更糟糕的是,当它是一个 Audible.com 文件时,NSURL 属性直接返回 nil。)< /p>
- ) p>如果您尝试使用 AVAudioPlayer 的实例来获取远程事件(例如,使用空白声音文件),然后同时使用 MPMusicPlayer 类来播放 iPod 音乐,则您可以进行远程控制访问,直到您实际开始 iPod 播放为止您会丢失它,因为您的音频会话被停用并且系统音频会话变为活动状态。
如果您尝试与 #4 相同的方法,但将音频会话的类别设置为可混合变体,则您的会话不会停用,但一旦 iPod 开始播放,您仍然会失去远程控制功能。
简而言之,每当 MPMusicPlayer 播放时,我似乎无法获取远程事件,并且除了使用 MPMusicPlayer 之外,我不知道还有其他方法可以播放 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...
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.
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 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.)
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.
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哈!解决了!我知道这是可能的! (感谢 Async-games.com 支持!)
以下是如何在后台支持的情况下在您的应用中播放 iPod 音乐,并且您的应用程序接收远程控制通知。
您必须使用 AVPlayer(但不是 AVAudioPlayer。不知道为什么!)使用从库选择器(或 MPMusicPlayerController 中的当前项目或其他位置)获得的 MPMediaItem 的资产 URL 进行初始化,然后将音频会话的类别设置为 Playable (不要启用混合覆盖,否则您将丢失远程事件!)并将适当的键添加到您的 info.plist 文件中,告诉操作系统您的应用程序想要支持背景音频。
完成了,完成了!
这使您可以在后台播放 iPod 库中的项目(由于某种原因,Audible.com 文件除外!),并且仍然可以获得远程事件。当然,由于这是您的音频播放器,它与 iPod 应用程序是分开的,并且会中断 iPod 应用程序,因此您必须做更多的工作,但这些就是中断!
但该死的...我只是希望它能与 Audible.com 文件一起使用。 (对于那些感兴趣的人来说,它不这样做的原因是音频文件的资产 URL 返回 nil。很糟糕!但是你能做什么!)
HA! Solved! I knew it was possible! (Thanks Async-games.com support!)
Here's how to play iPod music in your app, with background support, and with your app receiving the remote control notifications.
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.
Done and done!
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!
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!)
这对于OP来说可能不再有任何用处,但由于人们可能通过谷歌搜索找到此页面,所以我无论如何都会发布它。
另一种(但相当丑陋)的方法,如果您只对音乐远程控制事件感兴趣并且仍然希望能够播放 audible.com 文件...
只需继续使用 MPMusicPlayer 并跟踪其通知(正在播放并状态改变)。要继续在后台接收这些通知,您可以执行各个地方描述的“后台线程魔法”,以防止您的应用程序被挂起。您不会直接接收远程控制(因为 iPod 播放器正在接收它们),但是通过跟踪“正在播放”的变化,您可以推断出 ControlPreviousTrack 和 ControlNextTrack 事件,并且通过跟踪playbackState,您可以推断出 TogglePlayPause命令。
缺点是你的应用程序会无缘无故地一直运行(不过,公平地说,如果 iOS 编程正确,不执行任何操作的后台线程几乎不会消耗电池)。
另一种选择:使用 MPMoviePlayer?我已经检查过它在后台工作正常,并且也应该接收远程控制事件。它可以本地播放 MPMediaItem,所以希望 Audible.com 文件也能播放......
This is probably not going to be of any use anymore for the OP, but as it may be for people finding this page through googling, I will post it anyway.
An alternative (but rather ugly) approach, if you are only interested in the music remote control events and still want to be able to play the audible.com files...
Just keep using the MPMusicPlayer and track its notifications (now playing and state changed). To keep receiving these notifications in the background, you can do the "background thread magic" described in various places to keep your app from being suspended. You are not going to receive the remote controls directly (as the iPod player is receiving them), but by tracking the changes in "now playing" you can infer the ControlPreviousTrack and ControlNextTrack events, and by tracking the playbackState, you can infer the TogglePlayPause command.
The downside is that you are app is going to be running at all times for no good reason (although, to be fair, if iOS is programmed correctly, a background thread doing nothing should consume almost no battery).
Another alternative: use a MPMoviePlayer? I have checked that it works fine in the background, and should receive remote control events as well. It can play MPMediaItem natively, so hopefully the Audible.com files as well...
这是没有办法解决的。如果用户的 iPod 应用程序正在播放 iPod 选择,则所有远程事件都将发送到 iPod,而不是您的应用程序。
There is no way around this. If the users iPod app is playing an iPod selection, then all remote events are going to go to the iPod, not your app.
我注意到 MPMediaItemPropertyAssetURL 的一个想法是,虽然返回的对象位于 NSURL 中,但绝对字符串是这样的:
ipod-library://item/item.mp3?id=580807975475997900
这不是 AVAudioPlayer 想要的。 AVAudioPlayer 想要的是从具有有效文件路径的文件创建的 NSURL 对象。
我不知道如何从 MPMediaItem 获取文件路径。所以我想如果你想在不使用 MPMusicPlayer 的情况下播放 iPod 曲目,AVPlayer 可能是一个不错的选择。
One think I noticed about MPMediaItemPropertyAssetURL is that, although the object returned is in NSURL but the absoluteString is something like this:
ipod-library://item/item.mp3?id=580807975475997900
Which is not what AVAudioPlayer want. What AVAudioPlayer want is NSURL object that is created from a file with a valid file path.
And I have no idea how to get file path from MPMediaItem. So I guess maybe AVPlayer is the way to go if you want to play iPod track without using MPMusicPlayer.