davidliu
Committed by GitHub

Use LegacyAudioSwitch for API < 23 devices (#197)

@@ -2,11 +2,10 @@ package io.livekit.android.audio @@ -2,11 +2,10 @@ package io.livekit.android.audio
2 2
3 import android.content.Context 3 import android.content.Context
4 import android.media.AudioManager 4 import android.media.AudioManager
  5 +import android.os.Build
5 import android.os.Handler 6 import android.os.Handler
6 import android.os.Looper 7 import android.os.Looper
7 -import com.twilio.audioswitch.AudioDevice  
8 -import com.twilio.audioswitch.AudioDeviceChangeListener  
9 -import com.twilio.audioswitch.AudioSwitch 8 +import com.twilio.audioswitch.*
10 import javax.inject.Inject 9 import javax.inject.Inject
11 import javax.inject.Singleton 10 import javax.inject.Singleton
12 11
@@ -49,7 +48,7 @@ constructor(private val context: Context) : AudioHandler { @@ -49,7 +48,7 @@ constructor(private val context: Context) : AudioHandler {
49 */ 48 */
50 var preferredDeviceList: List<Class<out AudioDevice>>? = null 49 var preferredDeviceList: List<Class<out AudioDevice>>? = null
51 50
52 - private var audioSwitch: AudioSwitch? = null 51 + private var audioSwitch: AbstractAudioSwitch? = null
53 52
54 // AudioSwitch is not threadsafe, so all calls should be done on the main thread. 53 // AudioSwitch is not threadsafe, so all calls should be done on the main thread.
55 private val handler = Handler(Looper.getMainLooper()) 54 private val handler = Handler(Looper.getMainLooper())
@@ -58,12 +57,22 @@ constructor(private val context: Context) : AudioHandler { @@ -58,12 +57,22 @@ constructor(private val context: Context) : AudioHandler {
58 if (audioSwitch == null) { 57 if (audioSwitch == null) {
59 handler.removeCallbacksAndMessages(null) 58 handler.removeCallbacksAndMessages(null)
60 handler.postAtFrontOfQueue { 59 handler.postAtFrontOfQueue {
61 - val switch = AudioSwitch(  
62 - context = context,  
63 - loggingEnabled = loggingEnabled,  
64 - audioFocusChangeListener = onAudioFocusChangeListener ?: defaultOnAudioFocusChangeListener,  
65 - preferredDeviceList = preferredDeviceList ?: defaultPreferredDeviceList  
66 - ) 60 + val switch =
  61 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  62 + AudioSwitch(
  63 + context = context,
  64 + loggingEnabled = loggingEnabled,
  65 + audioFocusChangeListener = onAudioFocusChangeListener ?: defaultOnAudioFocusChangeListener,
  66 + preferredDeviceList = preferredDeviceList ?: defaultPreferredDeviceList
  67 + )
  68 + } else {
  69 + LegacyAudioSwitch(
  70 + context = context,
  71 + loggingEnabled = loggingEnabled,
  72 + audioFocusChangeListener = onAudioFocusChangeListener ?: defaultOnAudioFocusChangeListener,
  73 + preferredDeviceList = preferredDeviceList ?: defaultPreferredDeviceList
  74 + )
  75 + }
67 audioSwitch = switch 76 audioSwitch = switch
68 switch.start(audioDeviceChangeListener ?: defaultAudioDeviceChangeListener) 77 switch.start(audioDeviceChangeListener ?: defaultAudioDeviceChangeListener)
69 switch.activate() 78 switch.activate()