iOS 背景音频无法播放 [英] iOS background audio not playing

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

问题描述

我有一个使用 CoreBluetooth 背景模式的应用程序.当某个事件发生时,我需要播放警报声.在前台一切正常,所有蓝牙功能在后台正常工作.我也让它在后台运行 UILocalNotification 来发出警报,但是我不喜欢这些缺少音量控制,所以想使用 AVAudioPlayer 播放警报声音.

I have an app that uses CoreBluetooth background modes. When a certain event happens I need to play an alarm sound. Everything works fine in the foreground and all bluetooth functionality works fine in the background. I also have it working where it schedules UILocalNotification's in the background to sound the alarm, however I don't like the lack of volume control with these so want to play the alarm sound using AVAudioPlayer.

我已将背景模式 audio 添加到我的 .plist 文件中,但无法正常播放声音.

I've added the background mode audio to my .plist file but can't get the sound to play properly.

我正在使用单例类作为闹钟,并像这样初始化声音:

I am using a singleton class for the alarm and initialise the sound like this:

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

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player setVolume:1.0];

我开始这样的声音:

-(void)startAlert
{
    playing = YES;
    [player play];
    if (vibrate)
        [self vibratePattern];
}

并将其用于振动:

-(void)vibratePattern
{
    if (vibrate && playing) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        [self performSelector:@selector(vibratePattern) withObject:nil afterDelay:0.75];
    }
}

振动在背景中工作正常,但没有声音.如果我使用 Systemsound 来播放这样的声音,它播放得很好(但没有音量控制):

The vibration works fine in the background, but no sound. If I use Systemsound to play the sound like this, it plays fine (But no volume control):

AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_sound);
AudioServicesPlaySystemSound(_sound);

那么 AVAudioPlayer 没有播放声音文件的原因是什么?

So what could be the reason why the AVAudioPlayer is not playing the sound file?

谢谢

编辑 -------

如果应用程序在后台播放时,声音就会播放.但是,让它在后台播放时开始播放是行不通的.

The sound will play if it's already playing when the app is backgrounded. However making it start to play whilst backgrounded is not working.

推荐答案

在属性列表 (.plist) 文件中添加一个名为 Required background patterns 的键..

add a key named Required background modes in property list (.plist) file ..

如下图..

你能得到帮助吗..

并在

AppDelegate.h

AppDelegate.h

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

AppDelegate.m

AppDelegate.m

在应用 didFinishLaunchingWithOptions 中

in application didFinishLaunchingWithOptions

[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

UInt32 size = sizeof(CFStringRef);
CFStringRef route;
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);

如果您想根据事件进行更改,则必须在 AppDelegate.m 中添加以下代码

If you want changes as per events you have to add following code in AppDelegate.m

- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {

    if (theEvent.type == UIEventTypeRemoteControl)  {
        switch(theEvent.subtype)        {
            case UIEventSubtypeRemoteControlPlay:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            case UIEventSubtypeRemoteControlPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            case UIEventSubtypeRemoteControlStop:
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
                break;
            default:
                return;
        }
    }
}

基于通知必须对其进行处理..

based on notification have to work on it..

code.tutsplus.com 提供了一个教程.

code.tutsplus.com provides a tutorial.

对于 HandsetBluetooth,您必须在 AppDelegate 中添加以下代码

for HandsetBluetooth you have to add following code in AppDelegate

UInt32 size = sizeof(CFStringRef);
    CFStringRef route;
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
    NSLog(@"route = %@", route);
    NSString *routeString=[NSString stringWithFormat:@"%@",route];
    if([routeString isEqualToString:@"HeadsetBT"]){
        UInt32 allowBluetoothInput = 1;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
    }

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

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