通过XCode中的AVAudioPlayer开始播放后台任务中的音频 [英] start playing audio from a background task via AVAudioPlayer in XCode

查看:70
本文介绍了通过XCode中的AVAudioPlayer开始播放后台任务中的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过AVAudioPlayer从后台任务开始播放声音然后实例化,所以这就是我所拥有的。

I am trying to start playing a sound from a background task via an AVAudioPlayer that is instantiated then, so here's what I've got.

为了便于阅读我切out所有用户选择的值。

For readability I cut out all user selected values.

- (void)restartHandler {
    bg = 0;
    bg = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:bg];
    }];
    tim = [NSTimer timerWithTimeInterval:.1 target:self selector:@selector(upd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:tim forMode:NSRunLoopCommonModes];
}

- (void)upd {
    if ([[NSDate date] timeIntervalSinceDate:startDate] >= difference) {
        [self playSoundFile];
        [tim invalidate];
        [[UIApplication sharedApplication] endBackgroundTask:bg];
    }
}

- (void)playSoundFile {

    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];


    // new player object
    _player = [[AVQueuePlayer alloc] init];
    [_player insertItem:[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"Manamana" withExtension:@"m4a"]] afterItem:nil];

    [_player setVolume:.7];
    [_player play];

    NSLog(@"Testing");
}

说明: bg tim startDate 差异 _player 是全局声明的变量。我从用户触发的方法调用 - (void)restartHandler 并在其内部为启动10Hz重复计时器 - (void)upd 。当达到预设差异时, - (void)playSoundFile 被调用并启动并启动播放器。调用方法结束时的测试NSLog。

Explanation: bg, tim, startDate, difference and _player are globally declared variables. I call - (void)restartHandler from a user-fired method and inside it start a 10Hz repeating timer for - (void)upd. When a pre-set difference is reached, - (void)playSoundFile gets called and initiates and starts the player. The testing NSLog at the end of the method gets called.

现在奇怪的是,如果我调用 - (void)playSoundFile 当应用程序处于活动状态时,一切正常,但是如果我从后台任务启动它就不会播放任何声音。

Now the strange thing is if I call - (void)playSoundFile when the app is active, everything works out just fine, but if I start it from the background task it just won't play any sound.

任何帮助非常感谢,提示,解决方案或抬头。

Any help, hint, solution or heads up is greatly appreciated.

编辑

所以我尝试在运行时使用不同的线程,因为它似乎,如果播放器未在主线程上实例化,则会出现同样的问题。
我也尝试使用 AVPlayer AVAudioPlayer 其中 AVAudioPlayer .playing 属性确实返回YES,但仍然没有声音播放。

So I tried using different threads at runtime and as it seems, if the Player is not instantiated on the Main Thread this same problem will appear. I also tried using AVPlayer and AVAudioPlayer where the AVAudioPlayer's .playing property did return YES and still no sound was playing.

推荐答案

根据我在编写播放器应用程序后所学到的内容,即使您已将所有内容配置正确,但当您的应用程序已在后台运行超过X秒时,您似乎无法开始播放音频。

From what I've learned after writing an player App, it seems that you can not start playing audio when your App is already in background for longer than X seconds, even if you have configured everything right.

要修复它,你必须明智地使用后台任务。
最重要的是你必须 playSoundFile endBackgroundTask >。 延迟大约5-10秒。

To fix it, you have to use background task wisely. The most important thing is that you must NOT call endBackgroundTask immediately after playSoundFile. Delay it for about 5-10 seconds.

以下是我为iOS6和iOS7管理的方法。

Here is how I managed doing it for iOS6 and iOS7.

1,在plist中添加音频UIBackgroundModes。

1, Add audio UIBackgroundModes in plist.

2,设置音频会话类别:

2, Set audio session category:

3,在主线程中创建AVQueuePlayer,并在应用输入背景之前将音频素材排入队列。

3, Create an AVQueuePlayer in the main thread and enqueue an audio asset before the App enter background.

* 继续在后台播放(如播放播放列表) *

*For continues playing in background (like playing a playlist)*

4,确保音频AVQueuePlayer中的队列永远不会变空,否则你的应用程序将在完成播放最后一个资产时被暂停。

4, Make sure the audio queue in AVQueuePlayer never become empty, otherwise your App will be suspended when it finishes playing the last asset.

5,如果5秒不是足以让你获得下一个资产,你可以使用后台任务来延迟暂停。当资产准备就绪时,你应该在10秒后调用 endBackgroundTask

5, If 5 seconds is not enough for you to get the next asset you can employ background task to delay the suspend. When the asset is ready you should call endBackgroundTask after 10 seconds.

这篇关于通过XCode中的AVAudioPlayer开始播放后台任务中的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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