【问题标题】:How to play AVSpeechSynthesizer through call receiver speaker?如何通过呼叫接收扬声器播放 AVSpeechSynthesizer?
【发布时间】:2016-07-27 07:44:53
【问题描述】:

我喜欢通过呼叫接收器扬声器播放音频,目前我正在使用它来播放一些文本作为音频。

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:_strTextCheck];
    AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
    [syn speakUtterance:utterance];

我知道了,但这不适用于 AVSpeechSynthesizer:

[AVAudioSession  overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

它在普通扬声器上正常工作,但我想通过呼叫接收器扬声器播放,可以这样做吗?

【问题讨论】:

    标签: ios objective-c avspeechsynthesizer


    【解决方案1】:

    默认行为是通过呼叫接收者播放。因此,如果您取消设置覆盖 - 或者首先不设置它 - 您应该得到您所追求的行为:

    [[AVAudioSession sharedInstance] 
         overrideOutputAudioPort:AVAudioSessionPortOverrideNone
                           error:nil];
    

    这是一个完整的例子。您还需要设置 audioSession 类别。

    - (void)playVoice {
        [[AVAudioSession sharedInstance] 
                 setCategory:AVAudioSessionCategoryPlayAndRecord
                      error:nil];
    
        //try one or the other but not both...
    
        //[self playVoiceOnSpeaker:@"test test test"];
    
        [self playVoiceOnReceiver:@"test test test"];
    
    }
    
    -(void)playVoiceOnSpeaker:(NSString*)str
    {
        [[AVAudioSession sharedInstance]  
             overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                               error:nil];
        [self playVoiceByComputer:str];
    }
    
    -(void)playVoiceOnReceiver:(NSString*)str
    {
        [[AVAudioSession sharedInstance]
             overrideOutputAudioPort:AVAudioSessionPortOverrideNone
                               error:nil];
        [self playVoiceByComputer:str];
    }
    
    -(void)playVoiceByComputer:(NSString*)str
    {
        AVSpeechUtterance *utterance = 
              [AVSpeechUtterance speechUtteranceWithString:str];
        AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
        [syn speakUtterance:utterance];
    }
    

    【讨论】:

    • 但它仍然在扬声器上运行 bro paste.ofcode.org/uEi8M5FjcGhB97NurcEEsM
    • 通过接收器扬声器它没有运行
    • @KishoreKumar - 查看我的更新 - 您需要检查您的 audioSession 类别是否正确
    【解决方案2】:

    这对我有用:

    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多