iOS:AVSpeechSynthesizer:需要在Headphone左声道中说出文字

时间:2017-02-20 10:09:59

标签: ios swift avfoundation text-to-speech

我正在为TextToSpeech使用AVSpeechSynthesizer。我必须在HeadPhone左侧的Channel(Mono 2)中演奏。我有以下设置输出通道。

func initalizeSpeechForRightChannel(){     
    let avSession = AVAudioSession.sharedInstance()
    let route = avSession.currentRoute
    let outputPorts = route.outputs
    var channels:[AVAudioSessionChannelDescription] = []
    //NSMutableArray *channels = [NSMutableArray array];
    var leftAudioChannel:AVAudioSessionChannelDescription? = nil
    var leftAudioPortDesc:AVAudioSessionPortDescription? = nil 
    for  outputPort in outputPorts {
        for channel in outputPort.channels! {
            leftAudioPortDesc = outputPort
            //print("Name: \(channel.channelName)")
            if channel.channelName == "Headphones Left" {
                channels.append(channel)
                leftAudioChannel = channel
            }else {
               // leftAudioPortDesc?.channels?.removeObject(channel)
            }
        }
    }

    if channels.count > 0 {
        if #available(iOS 10.0, *) {
            print("Setting Left Channel")
            speechSynthesizer.outputChannels = channels
            print("Checking output channel : \(speechSynthesizer.outputChannels?.count)")

        } else {
            // Fallback on earlier versions
            }
        }
    }

我在代码中有2个问题

1。无法设置输出通道,它始终为零(这是在第一次调用此方法时发生,连续调用工作正常)

2。来自iOS 10的outputchannels支持。*但我需要从iOS支持它 8.0

请提供最好的方法。

1 个答案:

答案 0 :(得分:1)

  1. 检查channelName,而不是检查描述性的channelLabel(即用户)。 There is an enumeration containing the left channel

  2. 我怀疑在iOS 10之前可能无法实现这一点。 AVAudioSession似乎没有任何方法可以只选择左输出通道。可能能够使用overrideAudioPort:error但会影响整个应用程序。