AVAudioPlayer无法在后台播放 [英] AVAudioPlayer doesn't start playing in background

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

问题描述

我正在使用 AVAudioPlayer NSFileHandle 来进行音频流传输。基本思想是我将流式音频数据保存到带有文件系统的设备,然后使用 AVAudioPlayer 来播放刚刚保存的文件。 AVAudioPlayer 即使流媒体正在播放,仍然可以播放该文件。

I am using AVAudioPlayer and NSFileHandle to do audio streaming. The basic idea is that I save the streaming audio data to the device with file system and then use AVAudioPlayer to playback the file just saved. AVAudioPlayer can still play that file even the streaming is in progress.

我有一个 UIButton 开始流媒体/下载。当下载的数据累积到特定数量的字节时, AVAudioPlayer 将播放( AVAudioPlayer的播放方法自动触发)。我的问题是:我点击按钮开始流式传输/下载,然后很快按下iPhone的主页按钮,让我的应用程序转到后台。下载继续在后台工作,但 AVAudioPlayer 的播放方法返回NO,这意味着播放方法不播放音频。

I have a UIButton to start streaming/downloading. When the downloaded data is accumulated to a specific amount of Bytes, AVAudioPlayer will play (the "play" method of AVAudioPlayer is triggered) automatically. My question is: I tap the button to start streaming/downloading, then press iPhone's home button very soon to make my app go to background. The download keeps working in the background but the "play" method of AVAudioPlayer returns "NO" which means the "play" method doesn't play the audio.

我在音频播放器的init方法中添加了 AVAudioSession 相关代码:

I added AVAudioSession related code to my audio player's init method:

- (id) initWithURL:(NSString *) downloadedMusicURL
{
    if ((self = [super init]))
    {       
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

        CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)downloadedMusicURL, kCFURLPOSIXPathStyle, FALSE);

        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:CFBridgingRelease(url) error:nil];

        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    }

    return self;
}

同样在info.plist中我添加了一个项目App播放音频关键必需的背景模式。但是,播放仍然无法在后台正确调用。还有什么我想念的?非常感谢提前。

Also in the info.plist I added an item "App plays audio" for the Key "Required background modes". However, the "play" still doesn't get called properly in the background. What else did I miss? Thanks a lot in advance.

推荐答案

您的应用无法在后台播放音频。这是有道理的。如果用户没有主动使用的应用程序突然开始产生声音,请想想设备会是什么样子!请记住,对用户而言,将应用程序发送到后台是相当于退出该应用程序。你的应用程序不能像僵尸一样上升并开始制造噪音!

Your app cannot start playing audio in the background. And this makes sense. Think what the device would be like if an app that the user is not actively using could suddenly start producing sound! Remember, to the user, sending an app to the background is the naive equivalent of quitting that app. Your app must not rise like a zombie from the dead and start making noise!

背景音频设置允许你的应用程序在前台产生音频然后进入后台时,/ em> 继续这样做。然后它可以继续负责音频(如果它接收远程事件),直到其他应用程序产生声音;之后,你的应用程序再次运行,直到它被带到前台并产生声音。

The background audio setting allows your app that was producing audio while in the foreground to continue to do so when it then goes into the the background. It then can continue being in charge of the audio (if it receives remote events) until some other app produces sound; after that, your app is once again out of the running until it is brought to the foreground and produces sound.

所以你需要开始播放一些声音 你的应用程序进入后台之前,如果你想让它工作。

So you'll need to start playing some sound before your app goes into the background, if you want this to work.

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

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