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

查看:127
本文介绍了如何在应用程序中接收远程控制事件的同时播放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,您可以轻松地以编程方式访问整个音乐库。但是,无论您使用的是appMusicPlayer还是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.

可以使用资产URL初始化AVAudioPlayer和iPod库中的曲目(类型MPMediaItem)确实有一个URL属性,它返回一个NSURL实例,文档明确说明它与AVAsset对象一起使用,但是当您尝试使用该NSURL初始化AVAudioPlayer时,它会失败。 (我使用iPod中的正在播放轨道,它是一个MP3,它确实返回了一个有效的NSURL对象,但初始化失败。更糟糕的是,当它是一个Audible.com文件时,NSURL属性平坦返回为零。)

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 有关如何解决此问题的建议将受到欢迎。创意或平淡的疯狂。只要它有效,就不要在意。

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

任何人?任何人? Bueller? Bueller?

Anyone? Anyone? Bueller? Bueller?

M

推荐答案

HA!解决了!我知道这是可能的! (感谢 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应用程序,你必须做更多的工作,但这些都是休息!

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天全站免登陆