在 iOS 4 上使用 MPMusicPlayerController 支持后台音频和远程控制。这可能吗?

发布于 2024-09-08 00:44:35 字数 1521 浏览 4 评论 0原文

我在这个问题上花了两天时间,却一无所获。我正在尝试使用 [MPMusicPlayerController applicationMusicPlayer] 播放从用户的 iPod 库中选择的音频,并使其在后台运行并支持远程事件。现在真正播放音乐是比较容易的部分。获取实例,选择歌曲,分配音乐队列并播放。完成了,完成了。但是...a)我无法让它在后台播放,b)即使在前台我也无法让远程控制事件正常工作!

在你问之前,是的,我已经设置了 plist 条目、音频会话类别、调用来表示我有兴趣获取远程事件并设置第一响应者来监听它们,所以请知道,是的,我'我已经阅读了我能找到的关于这个主题的每一个文档*(*我责怪苹果公司在这个主题上根本不清楚,也没有任何示例代码!)并且我已经看过每一个与之相关的 WWDC 视频(甚至冻结屏幕以从其示例中准确复制代码...),因此除非我错过了不在此列表中的内容,否则不会回复任何这些答案来帮忙。

另一件事...我明确谈论使用 MPMusicPlayerController,根据文档,它从不使用应用程序会话。它始终使用系统会话。 (也许这本身就回答了我的问题,但文档没有明确说明这一点,所以我不确定,因此提出了这个问题。)

也就是说,两天后,我的想法是这样的:

  1. 当使用 MPMusicPlayerController 时,无论您调用什么方法或设置什么plist条目,您的应用程序都不会在后台运行。时期。如果您使用 ipodMusicPlayer 实例,音乐会继续播放,但这是因为正在播放的是 iPod,而不是您的应用程序。如果您改用 applicationMusicPlayer 实例,则当转到后台时,音乐会停止。在这两种情况下,您的应用程序都会被挂起。

  2. 无论您使用 ipodMusicPlayer 还是 applicationMusicPlayer 实例,所有远程事件都会转到 iPod 应用程序本身,而不是您的,即使您明确要求它们也是如此。如果您使用 applicationMusicPlayer 实例并使用遥控器选择“播放”,则 iPod 应用程序会收到该命令,因此您的音频会消失并中断,并在 iPod 应用程序中开始播放。如果您选择了 ipodMusicPlayer,那么当然这并不重要,因为您已经明确表示您基本上只是对远程控制 iPod 应用程序感兴趣,而该应用程序实际上是接收远程事件的。

  3. 底部快速切换控件中的图标永远不会更改为应用程序的图标,因为同样,您的应用程序实际上从未设置为接收事件。 iPod 应用程序是这样,这就是它的图标确实出现在那里的原因。

所以我想知道的是......我错了吗?有没有人成功地使用 MPMusicPlayerController 并能够拦截远程事件?虽然我更喜欢使用具有背景音乐支持的 applicationMusicPlayer,这样我就不会弄乱用户的 iPod,但更重要的是远程控制通知,这意味着如果我必须使用 ipodMusicControl 并将我的应用程序放在前台来拦截这些通知消息,就这样吧。这样虽然很丑陋,但至少是这样。

代码示例,或者至少针对内置应用程序模板之一的明确步骤将非常感激。 (甚至不需要实施......只需要步骤。希望这能平息人们不断回答问题时不可避免的“它仍在保密协议下”的情况。)

马克

I've spent two days on this and have gotten nowhere. I'm trying to use [MPMusicPlayerController applicationMusicPlayer] to play audio chosen from the user's iPod library and have it run in the background as well as support remote events. Now getting the music actually playing is the easy part. Get the instance, pick the songs, assign the music queue and play. Done and done. BUT... a) I can't get it to play in the background, and b) even when in the foreground I can't get the remote control events to work at all!

And before you ask, yes, I have set the plist entries, the audio session category, the call to say I'm interested in getting remote events and set up a first responder to listen for them, so please know, yes, I've read read every single document on the subject that I could find* (*a task I blame Apple for for not being clear at all on this topic, nor having ANY example code for it!) and I've watched every one of the WWDC videos relating to it (even freezing the screen to copy the code exactly from their example...) so unless I've missed something not in this list, replying with any of those answers is not going to help.

One more thing... I am explicitly talking about using the MPMusicPlayerController which according to the docs, never uses an application session. It always uses the system session. (Maybe that in itself answers my question, but the docs don't clearly say that so I'm not sure, hence this question.)

That said, after two days, my thoughts are this:

  1. When using the MPMusicPlayerController, regardless of what methods you call or what plist entries you set, your app will never run in the background. Period. If you use the ipodMusicPlayer instance, the music keeps playing, but that's because it's the iPod that's playing, not your app. If you use the applicationMusicPlayer instance instead, when going to the background your music stops. In both cases, your app is suspended.

  2. Regardless of your using the ipodMusicPlayer or applicationMusicPlayer instances, all remote events go to the iPod application itself, not yours, even if you've explicitly asked for them. If you are using the applicationMusicPlayer instance and you use the remote to select 'Play', the iPod app receives the command so your audio ducks out and is interrupted and playback begins in the iPod app. If you've chosen the ipodMusicPlayer instead, then of course it doesn't matter as you have explicitly said you're basically just interested in remotely controlling the iPod app which again, is what actually receives the remote events.

  3. The icon in the quick-switch controls at the bottom never changes to your app's icon because again, your app is never actually set up to receive the events. The iPod application is, which is why its icon does appear there.

So what I want to know is... am I wrong here? Has anyone successfully been able to use MPMusicPlayerController and been able to intercept the remote events? While I'd prefer to use the applicationMusicPlayer with background music support so I don't muck with the user's iPod, the bigger thing is remote control notifications, meaning if I have to use the ipodMusicControl and keep my app in the foreground to intercept those messages, so be it. It's ugly that way, but at least it's something.

Code examples, or at least explicit steps against one of the built-in app templates would be GREATLY appreciated. (Don't even need the implementation... just the steps. Hopefully that will appease the inevitable 'It's still under NDA' thing that people keep answering questions with.)

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2024-09-15 00:44:35

我解决了。该信息在我的其他问题中...

Stack Overflow:播放iPod 音乐,同时接收远程控制事件

...但简短的版本是您必须使用 AVPlayer(但不是 AVAudioPlayer。不知道为什么!)以及从库中获取的 MPMediaItem 的资产 URL,然后将音频会话的类别设置为“可播放”(请勿启用可混合!)并将相应的键添加到您的 info.plist 文件中,告诉操作系统您的应用程序想要支持背景音频。

这使您可以播放 iPod 库中的项目(由于某种原因,Audible.com 文件除外!),并且仍然可以获得远程事件。当然,您必须做更多的工作,并且由于这是您的音频播放器,它与 iPod 应用程序是分开的,并且会中断 iPod 应用程序(这可能是理想的,也可能不是。再次强调,不要启用混合,否则 iPod 应用程序将劫持远程控制事件)但这些就是休息!

I solved it. The info is in my other question over here...

Stack Overflow: Play iPod music while receiving remote control events

...but the short version is you have to use AVPlayer (but not AVAudioPlayer. No idea why that is!) with the asset URL from the MPMediaItem you got from the library, then set the audio session's category to Playable (do NOT enable mixable!) and add the appropriate keys to your info.plist file telling the OS your app wants to support background audio.

This lets you play items from your iPod library (except Audible.com files for some reason!) and still get remote events. Granted you have to do more work, and since this is your audio player which is separate from, and will interrupt the iPod app (which may or may not be desirable. And again, don't enable mixing or the iPod app will hijack the remote control events) but those are the breaks!

可是我不能没有你 2024-09-15 00:44:35

对于任何想知道的人,我发现要在后台播放音频,您必须将音频会话的类别设置为“可播放”,然后背景音频就可以正常工作。如果您还想同时播放自己的声音,则必须将类别标记为可混合。这样就解决了背景音乐部分。但我发现,每当 iPod 正在播放时,您似乎都无法收到远程通知。

这是更新的线程...

如何从 iPod 应用程序播放音乐同时仍在您的应用程序中接收远程控制事件?

M

For anyone who wants to know, I found out to get the audio playing in the background, you have to set the audio session's category to Playable and then background audio works just fine. If you also want to play your own sounds at the same time, you have to mark the category as mixable. That solved the background music part. But what I've found out is any time the iPod is playing, it doesn't seem possible for you to get remote notifications.

Here's the updated thread...

How can you play music from the iPod app while still receiving remote control events in your app?

M

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文