davidliu

Main thread call for audioSwitch selectDevice

@@ -57,7 +57,7 @@ constructor(private val context: Context) : AudioHandler { @@ -57,7 +57,7 @@ constructor(private val context: Context) : AudioHandler {
57 override fun start() { 57 override fun start() {
58 if (audioSwitch == null) { 58 if (audioSwitch == null) {
59 handler.removeCallbacksAndMessages(null) 59 handler.removeCallbacksAndMessages(null)
60 - handler.post { 60 + handler.postAtFrontOfQueue {
61 val switch = AudioSwitch( 61 val switch = AudioSwitch(
62 context = context, 62 context = context,
63 loggingEnabled = loggingEnabled, 63 loggingEnabled = loggingEnabled,
@@ -73,7 +73,7 @@ constructor(private val context: Context) : AudioHandler { @@ -73,7 +73,7 @@ constructor(private val context: Context) : AudioHandler {
73 73
74 override fun stop() { 74 override fun stop() {
75 handler.removeCallbacksAndMessages(null) 75 handler.removeCallbacksAndMessages(null)
76 - handler.post { 76 + handler.postAtFrontOfQueue {
77 audioSwitch?.stop() 77 audioSwitch?.stop()
78 audioSwitch = null 78 audioSwitch = null
79 } 79 }
@@ -86,8 +86,14 @@ constructor(private val context: Context) : AudioHandler { @@ -86,8 +86,14 @@ constructor(private val context: Context) : AudioHandler {
86 get() = audioSwitch?.availableAudioDevices ?: listOf() 86 get() = audioSwitch?.availableAudioDevices ?: listOf()
87 87
88 fun selectDevice(audioDevice: AudioDevice?) { 88 fun selectDevice(audioDevice: AudioDevice?) {
  89 + if (Looper.myLooper() == Looper.getMainLooper()) {
  90 + audioSwitch?.selectDevice(audioDevice)
  91 + } else {
  92 + handler.post {
89 audioSwitch?.selectDevice(audioDevice) 93 audioSwitch?.selectDevice(audioDevice)
90 } 94 }
  95 + }
  96 + }
91 97
92 companion object { 98 companion object {
93 private val defaultOnAudioFocusChangeListener by lazy(LazyThreadSafetyMode.NONE) { 99 private val defaultOnAudioFocusChangeListener by lazy(LazyThreadSafetyMode.NONE) {