iPhone OS 4多任务处理 - 在后台播放音频 [英] iPhone OS 4 Multitasking - Playing Audio In background

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

问题描述

我正在尝试使用iPhone OS 4.0的多任务处理能力。我试图在后台播放音频而没有运气。我在info.plist中添加了UIBackgroundModes属性,并提到需要在后台播放音频。我还添加了播放音频的代码。
`

I am trying to use iPhone OS 4.0's multitasking capability. I tried to play audio in the background with no luck. I added UIBackgroundModes property in info.plist and mentioned requires audio to play in background. Also I added the code for playing the audio. `

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"someday" ofType:@"mp3"]]; 
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];

`。单击应用程序中的按钮后,音频开始播放。但当我关闭应用程序时,它会停止。如何让它在后台播放?

`. The audio starts playing once i click on button in the app. But when I shut the app it stops. How can I make it play in the background?

谢谢,
Tony

Thanks, Tony

推荐答案

听起来您没有正确设置音频会话。来自 http://developer.apple.com /iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

It sounds like you didn't set up your Audio Session correctly. From http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html :

例如,当使用默认音频会话时,音频输入当自动锁定时间超时并且屏幕锁定时,您的应用程序停止。如果要确保在屏幕锁定的情况下继续播放,请在应用程序的初始化代码中包含以下行:

For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

AVAudioSessionCategoryPlayback类别可确保在屏幕锁定时继续播放。
激活音频会话会使指定的类别生效。

The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks. Activating the audio session puts the specified category into effect.

这篇关于iPhone OS 4多任务处理 - 在后台播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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