Tuesday, March 7, 2017

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

Leave a Comment

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.

1 Answers

Answers 1

  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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment