音频播放,同时应用程序在后台的iOS [英] Audio playing while app in background iOS

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

问题描述

我有一个应用程序周围的蓝牙核心主要是基于。
当事情发生的具体的应用被唤醒使用蓝牙核心背景模式,它激发了报警,但是当应用程序是不是在前台我不能得到报警工作。

I have an app mostly based around Core Bluetooth. When something specific happens, the app is woken up using Core Bluetooth background modes and it fires off an alarm, however I can't get the alarm working when the app is not in the foreground.

我有一个初始化报警Singleton类 AVAudioPlayer 是这样的:

I have an Alarm Singleton class which initialises AVAudioPlayer like this:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                    pathForResource:soundName
                             ofType:@"caf"]];

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[self.player prepareToPlay];
self.player.numberOfLoops = -1;
[self.player setVolume:1.0];

NSLog(@"%@", self.player);

这是当我的闹钟code被称为被调用的方法:

This is the method that is called when my alarm code is called:

-(void)startAlert
{
    NSLog(@"%s", __FUNCTION__);

    playing = YES;
    [self.player play];
    NSLog(@"%i", self.player.playing);

    if (vibrate) {
        [self vibratePattern];
    }
}

现在,当应用程序在前台, self.player.playing 收益 1 然而,当应用程序是背景 self.player.playing 收益 0 。为什么会这样?
所有code被调用,所以应用程序是清醒和运作。
该振动完美的作品,它使用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Now when the app is in the foreground, self.player.playing returns 1 however when the app is in the background self.player.playing returns 0. Why would this be? All the code is being called, so the app is awake and functioning. The vibrate works perfectly which uses AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

知道为什么这声音不会玩?

Any idea why this sound won't play?

感谢

推荐答案

我有一个应用程序不是也需要背景音乐,但我的应用程序工作原理与应用背景模式IP语音,因为它有时需要记录。我玩的背景音乐,告诉应用程序单,我需要后台播放音频:

I have an App than also needs background audio but my App works with the App background mode "Voice over IP" as it needs to record sometimes. I play background audio telling the App singleton I need to play audio in background:

UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
if([thePlayer play]){
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
}

编辑:您必须调用 [UIApplication的sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; 之前,您的应用程序被切换到后台。在我的应用程序,它是在同一时间,你开始玩,在你的,如果玩家可能在后台启动,你应该做的:

You must call [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; before your app goes to background. In my app, it is at the same time you start playing, in yours, if the player might be started in background, you should do:

- (void)applicationDidEnterBackground:(UIApplication *)application{
  // You should retain newTaskId to check for background tasks and finish them 
  newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
}

这篇关于音频播放,同时应用程序在后台的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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