在iOS4上进入后台播放音频 [英] Entering background on iOS4 to play audio

查看:32
本文介绍了在iOS4上进入后台播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谈论在后台播放音频时,文档写得相当糟糕.它给人的印象是,要继续播放当前正在播放的音频,只需将键/值对添加到 info.plist 文件和 wallah,这很神奇.

然而,事实并非如此.例如,如果我播放 2 分钟长的 mp3,显然音频足够长,可以在我按下主页按钮将我的应用程序发送到后台后播放.问题是,即使我的 info.plist 文件中有那个键/值对,它也会暂停音频,然后在我切换回应用程序后继续播放.

Apple 声明所有音频框架都支持背景和声音应该继续播放,直到它结束,然后 Apple 将暂停您的应用程序.

所以,我的问题是:他们在做什么而我错过了?我是否还必须以某种方式使用他们的新代表或调用 applicationDidEnterBackground 上的音频?对我来说,这没有意义,因为我没有保存状态(他们通过快速应用程序切换为我做)或真正在后台处理任何东西,而不是他们说应该自动处理的音频.

解决方案

文档中缺少的一个部分是您需要设置音频会话.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

添加它,你就可以开始了.

The documentation is rather poorly written when talking about playing audio in the background. It gives the impression that all you have to do to continue playing the audio that you are currently playing is to just add a key/value pair to the info.plist file and wallah, it's magic.

However, this is not the case. For instance, if I play an mp3 that is 2 minutes long, obviously the audio is long enough to be able to play after I hit the home button to send my app to the background. The thing is, even though I have that key/value pair in my info.plist file, it pauses the audio and then resumes playing once I switch back to the app.

Apple states that all frameworks for audio support background and the sound should continue playing up until the point that it ends and then Apple will suspend your app.

So, my question is: What are they doing that I am missing? Do I also have to use their new delegates somehow or call the audio on the applicationDidEnterBackground? To me, that wouldn't make sense since I am not saving state (they do for me with fast app switching) or really handling anything in the background other than audio which they say is supposed to be handled automagically.

解决方案

The one part missing from the documentation is you need to set your audio session.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Add that and you'll be good to go.

这篇关于在iOS4上进入后台播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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