iOS推送语音播报相关iOS大咖说

语音播报(类似支付宝商户收款到账提醒)

2017-10-10  本文已影响728人  我想说_

这篇文章主要解决在实现语音播报的基础上出现的三个问题.
1.手机静音状态下不能播报(全部使用的系统方法,没有使用其他第三方);
2.电话接入时暂停,挂断后继续播报(几率很小,但是还要处理);
3.手机正在播放音乐,收到离线推送,播报推送内容,完毕后继续播放音乐;
4.如果手机音量过小,调节音量

处理以上4个问题废了很大功夫,虽然代码很少.
demo已上传到github←戳

再此之前已经做好离线播报的功能,只是还没有完善,会有上述几个问题.
使用的是NotificationService模块,然后在获取推送后,文字转语音进行语音播报.(实现离线播报的过程)

1.解决方案,设置语音播报的模式,查看AVAudioSession文档

 [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
 [[AVAudioSession sharedInstance]setActive:YES error:nil];

2.解决方案,注册通知来监听AVAudioSessionInterruptionTypeKey类型
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

- (void)handleInterruption:(NSNotification *)notificaiton{
    NSLog(@"%@",notificaiton.userInfo);
    AVAudioSessionInterruptionType type = [notificaiton.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if (type == AVAudioSessionInterruptionTypeBegan) {//1是被系统占用 有电话进入
        //暂停播报,并保存进度
        [_synth pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    } else {
        //恢复播报
        [_synth continueSpeaking];
    }
}

3.解决方案,使用AVSpeechSynthesizer代理来监听当前播报是否完成

//播报结束后 关闭播报线程  继续播放音乐
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

4.解决方案,使用MPVolumeView用来控制播报时的音量

    _volumeView = [[MPVolumeView alloc]init];
    _volumeView.showsRouteButton = NO;
    //默认YES,这里为了突出,故意设置一遍
    _volumeView.showsVolumeSlider = YES;
    
    [_volumeView sizeToFit];
    [_volumeView setFrame:CGRectMake(-100, -100, 10, 10)];
    
    [_volumeView userActivity];
    
    for (UIView *view in [_volumeView subviews]){
        if ([[view.class description] isEqualToString:@"MPVolumeSlider"]){
            _volumeSlider = (UISlider*)view;
            
            break;
        }
    }
    //输出当前音量 保存 改变后再改回之前音量
    _volValue = [[AVAudioSession sharedInstance] outputVolume];
    
    //设置默认打开视频时声音为0.3,如果不设置的话,获取的当前声音始终是0
    [_volumeSlider setValue:1];
    
    //获取最是刚打开时的音量值
    _volumeValue = _volumeSlider.value;
    
    //设置展示音量条的值
    
    _showVolueSlider.value = _volumeValue;

播报完成后,恢复之前音量

//播报结束后 关闭播报线程  继续播放音乐
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
    [_volumeSlider setValue:_volValue];//恢复之前音量
}

以上就是解决四个问题的方案.如果有其他bug,会持续补充,实现离线播报也会找时间整理一下.

不想下载demo的,下面是整段代码 本人QQ 1165889915

@interface ViewController ()<AVAudioSessionDelegate,AVSpeechSynthesizerDelegate>

@property (nonatomic, strong)AVSpeechSynthesizer *synth;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    btn.frame =CGRectMake(100, 200, 70, 40);
    [btn setTitle:@"测试" forState:UIControlStateNormal];
    btn.backgroundColor =[UIColor orangeColor];
    [btn addTarget:self action:sel_registerName("btnClick") forControlEvents:UIControlEventTouchUpInside];

}

- (void)btnClick{
    //设置语音播报的模式 主要处理静音模式
    [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    [[AVAudioSession sharedInstance]setActive:YES error:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

        NSString *str =[NSString stringWithFormat:@"知君仙骨无寒暑。千载相逢犹旦暮"];
        
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:str];
        AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置发音,这是中文普通话
        utterance.voice = voice;
        
        _synth = [[AVSpeechSynthesizer alloc] init];
        _synth.delegate =self;
        
        [_synth speakUtterance:utterance];
}
- (void)handleInterruption:(NSNotification *)notificaiton{
    NSLog(@"%@",notificaiton.userInfo);
    AVAudioSessionInterruptionType type = [notificaiton.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if (type == AVAudioSessionInterruptionTypeBegan) {//1是被系统占用 有电话进入
         //暂停播报,并保存进度
        [_synth pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    } else {
        //恢复播报
        [_synth continueSpeaking];
    }
}

//播报结束后 关闭播报线程  继续播放音乐
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读