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

Sri*_*har 5 text-to-speech avfoundation ios swift

我正在使用AVTpeechSynthesizer for TextToSpeech.我必须在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
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在代码中有2个问题

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

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

请提供最好的方法.

ars*_*ius 1

  1. 不要检查channelName描述性的(即针对用户),而是检查channelLabel. 有一个包含左通道的枚举

  2. 我怀疑这在 iOS 10 之前可能是不可能的 。AVAudioSession 似乎没有任何方法来仅选择左输出通道。也许可以使用overrideAudioPort:error,但它会影响整个应用程序。