简体   繁体   English

从后台启动AVQueuePlayer

[英]Starting AVQueuePlayer from background

Can't make AVQueuePlayer start playing sound queue when it's starting after the app went into the background. 应用进入后台后,无法使AVQueuePlayer开始播放声音队列。

The basic question is: how to start sound from newly created AVQueuePlayer instance from background? 基本问题是: 如何从后台从新创建的AVQueuePlayer实例启动声音?

It's for navi-like app that need to play couple of combined sounds with appropriate directions when the time comes. 适用于类似navi的应用程序,需要在时间到时播放具有适当方向的组合声音。 And most of the time the app works in background... 大多数情况下,应用程序在后台运行...

The details are below... 详细信息如下...

It plays just fine when I start it from active application, and finishes playing sound even after app went to background. 当我从活动的应用程序启动时,它的播放效果很好,即使在应用程序进入后台后,它也可以完成播放声音。

What I did so far: 到目前为止,我做了什么:

In AppDelegate inside didFinishLaunchingWithOptions I added: AppDelegate didFinishLaunchingWithOptions内部,我添加了:

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

    // Change the default output audio route
    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                            sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);   

After the app started I clicked the home button so the app went into the background. 应用启动后,我单击主页按钮,使应用进入后台。

When the time came this code executes (the app is still in background, but note, that I have enabled Audio and AirPlay background mode ): 时间到了,此代码将执行(应用程序仍在后台,但请注意,我已启用Audio和AirPlay后台模式 ):

-(void)playTrainingFinishedSound
{
    NSMutableArray *queue = [NSMutableArray array];
    [queue addObject:[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"alertkoniectreningu" withExtension:@"m4a"]]];
    [self initializeAudioPlayerWithQueue:queue];
    [self.appDelegate.audioPlayer play];
}

-(void)initializeAudioPlayerWithQueue:(NSArray *)queue
{
    self.appDelegate.audioPlayer = [[AVQueuePlayer alloc] initWithItems:queue];
    self.appDelegate.audioPlayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
}

Unfortunately this code doesn't make any sound, opposite to the situation when the app was in foreground. 不幸的是,与应用程序处于前台的情况相反,此代码没有发出任何声音。

Oh God, there was just one line missing in the AppDelegate didFinishLaunchingWithOptions [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 哦,天哪,AppDelegate didFinishLaunchingWithOptions [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];只缺少[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

so it looks now like: 所以现在看起来像:

    audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    if (audioSession) [audioSession setActive:YES error:nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Change the default output audio route
    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                            sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);  

The reason the AVPlayer is failing is because you can't create/initialize an Audio Unit while an application is in the background, which is what the AVPlayer is attempting to do under the hood. AVPlayer失败的原因是因为您无法在后台运行应用程序时创建/初始化音频单元,而这正是AVPlayer试图在后台进行的操作。 The exception to this rule is of course audio-playing applications which can be started/resumed via the "Play" buttons built into the OS, while they are in the background. 当然,该规则的例外是音频播放应用程序,当它们处于后台时,可以通过操作系统中内置的“播放”按钮来启动/恢复它们。 Thus, applications which subscribe to remote control events have the capability to start Audio Units in the background, which is why subscribing to these events appears to solve the problem. 因此,订阅远程控制事件的应用程序具有在后台启动音频单元的能力,这就是为什么订阅这些事件似乎可以解决问题的原因。

It's unclear whether Apple is OK with using the API feature in this way. 尚不清楚Apple是否可以通过这种方式使用API​​功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM