iOS 同时播放两种声音
在做直播的时候遇见一种情况,当直播推流的过程中断掉的情况下,
想要播放一段声音来提示用户当前推流已经断掉,需要进行处理
在直播的推流使用的是AudioToolbox/AudioToolbox.h
所以当我使用
static SystemSoundID soundIDTest = 0;//当soundIDTest == kSystemSoundID_Vibrate的时候为震动
NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
if (path) {
AudioServicesCreateSystemSoundID( (__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundIDTest );
}
AudioServicesPlaySystemSound( soundIDTest );
在直播断掉的时候并不能进行播放
原因: 上面这段代码用的也是AudioToolbox ,
解决办法:
想要使用另外一种方式来进行播放音频的设置:
-(AVAudioPlayer *)audioplayer
{
if (!_audioplayer) {
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"4" ofType:@"wav"];
NSURL *url = makeURL(urlStr);
NSError *error = nil;
_audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
_audioplayer.numberOfLoops = 0;
_audioplayer.delegate = self;
[_audioplayer prepareToPlay];
}
return _audioplayer;
}
在需要提示音的时候 调用 audioplayer 就好使