iOS的背景音乐不打 [英] iOS background audio not playing

查看:248
本文介绍了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.

我已经添加了背景模式音频我的的.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];

我开始的声音是这样的:

I start the sound like this:

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

,并使用这个振动:

and use this for the vibration:

-(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)文件。

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

,如下图。


你可能会得到帮助。

may you get help..

和添加按照code

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 code

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提供了一个<一个href=\"http://$c$c.tutsplus.com/tutorials/ios-multitasking-background-audio--mobile-6833\">tutorial.

code.tutsplus.com provides a tutorial.

有关HandsetBluetooth你必须添加以下在AppDelegate中code

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天全站免登陆