Dar*_*ren 6 audio objective-c avfoundation avaudioplayer ios
我有一个使用CoreBluetooth背景模式的应用程序.当某个事件发生时,我需要发出警报声.一切都在前台工作正常,所有蓝牙功能在后台工作正常.我也有它工作的地方UILocalNotification在后台安排发出警报,但我不喜欢这些音量控制不足,所以想用AVAudioPlayer播放闹钟声音.
我已将背景模式添加audio到我的.plist文件中,但无法正常播放声音.
我正在使用单例类进行警报并初始化声音如下:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:soundName
ofType:@"caf"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player setVolume:1.0];
Run Code Online (Sandbox Code Playgroud)
我开始这样的声音:
-(void)startAlert
{
playing = YES;
[player play];
if (vibrate)
[self vibratePattern];
}
Run Code Online (Sandbox Code Playgroud)
并用它来振动:
-(void)vibratePattern
{
if (vibrate && playing) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self performSelector:@selector(vibratePattern) withObject:nil afterDelay:0.75];
}
}
Run Code Online (Sandbox Code Playgroud)
振动在背景中工作正常,但没有声音.如果我用它Systemsound来播放这样的声音,它播放正常(但没有音量控制):
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_sound);
AudioServicesPlaySystemSound(_sound);
Run Code Online (Sandbox Code Playgroud)
那么AVAudioPlayer没有播放声音文件的原因是什么?
谢谢
编辑-------
当应用程序背景时,声音将播放.然而,在背景下开始播放是行不通的.
Ash*_*kad 15
在属性列表(.plist)文件中添加一个名为Required background modes的键.
如下图
你可以得到帮助..
并添加以下代码
AppDelegate.h
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
在应用程序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);
Run Code Online (Sandbox Code Playgroud)
如果您希望根据事件进行更改,则必须在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
基于通知必须工作..
code.tutsplus.com提供了一个教程.
对于HandsetBluetooth,您必须在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);
}
Run Code Online (Sandbox Code Playgroud)
除了plist设置,您还必须修改app delegate.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
Run Code Online (Sandbox Code Playgroud)
另外在你controller写下面的代码.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)
默认情况下,AVAudioPlayer使用AVAudioSessionCategorySoloAmbient类别,该类别由铃声开关静音。该AVAudioSessionCategoryPlayback类别更适合您的情况,因为它不会被开关静音,并且将继续在后台播放:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:soundName
ofType:@"caf"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player setCategory:AVAudioSessionCategoryPlayback error:nil];
player.numberOfLoops = -1;
[player setVolume:1.0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14104 次 |
| 最近记录: |