Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: AVSpeechSynthesizer : need to speak text in Headphone left channel

I am using AVSpeechSynthesizer for TextToSpeech. I have to play the speak in HeadPhone left Channel (Mono 2). I have got the following to set the output channel.

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
            }
        }
    }

I have 2 problems in the code

1. Cant able to set outputchannels , It always nil (It is happening on first time calling this method, consecutive calls working fine)

2. outputchannels supports from iOS 10.* But I need to support it from iOS 8.0

Please provide the best way to do that.

like image 791
Sridhar Avatar asked Nov 08 '22 01:11

Sridhar


1 Answers

  1. Instead of checking the channelName, which is descriptive (i.e. for the user), check the channelLabel. There is an enumeration containing the left channel.

  2. I suspect this may not be possible pre-iOS 10. AVAudioSession doesn't appear to have any method to select only the left output channel. You may be able to use overrideAudioPort:error but it would affect the entire app.

like image 102
arsenius Avatar answered Nov 14 '22 22:11

arsenius