转:iOS 音频开发从AVSpeechSynthesisVoice开始及AVAudioPlayer开发的几个问题处理方法

我一直对音频开发还是比较有兴趣的,所以想深入了解一下,在iOS中最重要的框架还是AVFoundation。
从AVSpeechSythesisVoice开始,先来几行代码,可以从文本转化为语音。

AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"hello world,please attention http://blog.csdn.net/u011397277"];  
    utterance.rate = 0.4f;  
//    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];  
    utterance.pitchMultiplier = 0.8f;  
    utterance.postUtteranceDelay = 0.1f;  
    [self.synthesizer speakUtterance:utterance];  
-(AVSpeechSynthesizer *)synthesizer  
{  
    if (!_synthesizer) {  
        _synthesizer = [[AVSpeechSynthesizer alloc] init];  
    }  
    return _synthesizer;  
}  

是不是很神奇,就几行代码实现了,需要导入头文件

#import   

如果大家有兴趣,可以查看这些源码,对音频的学习有很大的帮助。
github地址:https://github.com/tapharmonic/Learning-AV-Foundation

我在以前的工作中,遇到过这样的问题。应用正在播放音乐,当电话铃声响起,手机被静音,点击手机的锁屏键,插上耳机线,我们的应用应该如何处理?

问题一
处理静音的问题:如果在开发中手机调制"铃声/静音",声音在两种状态下切换,而我们希望的是不让声音消失,我们可以尝试配置音频回话,加入以下的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  
    AVAudioSession *session = [AVAudioSession sharedInstance];  
  
    NSError *error;  
    if (![session setCategory:AVAudioSessionCategoryPlayback error:&error]) {  
        NSLog(@"Category Error: %@", [error localizedDescription]);  
    }  
  
    if (![session setActive:YES error:&error]) {  
        NSLog(@"Activation Error: %@", [error localizedDescription]);  
    }  
      
    return YES;  
}  

问题二
处理手机锁屏的问题:我试过QQ音乐,酷狗音乐,天天动听,如果你锁屏,音乐还是会继续播放的,但是你的手机应用有可能不能继续播放声音,可以尝试使用下面的方法解决,配置info.plist文件

转:iOS 音频开发从AVSpeechSynthesisVoice开始及AVAudioPlayer开发的几个问题处理方法_第1张图片

添加箭头的属性。

问题三
处理中断的问题,比如电话呼入、闹铃响起等,我曾经处理过网络的通话问题,当时我开发的一款能够使用流量,WIFI通话的app,当电话呼入时,这个怎么处理,当电话呼入时,手机是断网的,我们测试过,但是你连接WIFI这个是没有影响的。我们当时是加了一个通知处理的。经过测试,当电话呼入的时候,系统的本身是能够处理这个问题的,但是当对方终止电话的时候,音频是不会如预期的恢复,面对这样的问题应该如何解决。
这种情况我们可以添加通知,在你的应用的播放通知类里面,可以添加以下的几行代码。

NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];  
[nsnc addObserver:self  
         selector:@selector(handleRouteChange:)  
             name:AVAudioSessionRouteChangeNotification  
           object:[AVAudioSession sharedInstance]];  

然后在处理函数handleRouteChange:处理问题。
我们要分析notification.userInfo的信息,可以参考下面的代码。

-(void)handleInterruption:(NSNotification *)notification  
{  
    NSDictionary *info = notification.userInfo;  
      
    AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];  
    if(type == AVAudioSessionInterruptionTypeBegan){  
        //处理问题  
    }else{  
        //处理问题  
    }  
      
}  

问题四
处理线路改变的问题,当你插上耳机时,我们希望声音从耳机内传出,但我们拔掉耳机的时候,我们希望的是音乐停止播放,是的,stop Play。 同样,我们可以添加一个通知处理。可以参考下面的代码处理:

NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];  
[nsnc addObserver:self  
         selector:@selector(handleRouteChange:)  
             name:AVAudioSessionRouteChangeNotification  
           object:[AVAudioSession sharedInstance]];  

下面是处理函数

- (void)handleRouteChange:(NSNotification *)notification {  
  
    NSDictionary *info = notification.userInfo;  
  
    AVAudioSessionRouteChangeReason reason =  
        [info[AVAudioSessionRouteChangeReasonKey] unsignedIntValue];  
  
    if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {  
  
        AVAudioSessionRouteDescription *previousRoute =  
            info[AVAudioSessionRouteChangePreviousRouteKey];  
  
        AVAudioSessionPortDescription *previousOutput = previousRoute.outputs[0];  
        NSString *portType = previousOutput.portType;  
  
        if ([portType isEqualToString:AVAudioSessionPortHeadphones]) {  
            [self stop];  
            [self.delegate playbackStopped];  
        }  
    }  
}  

以上就是一些处理音频开发一些问题处理解决方法,这个音频开发内容还是比较多的,要分很多情况,进行处理,本文还是比较浅的几个问题。

转自:http://blog.csdn.net/u011397277/article/details/52298457

你可能感兴趣的:(转:iOS 音频开发从AVSpeechSynthesisVoice开始及AVAudioPlayer开发的几个问题处理方法)