From babddb00e00e686649b192a3f1b62ad7d7f94175 Mon Sep 17 00:00:00 2001 From: Paul McLean Date: Thu, 6 Feb 2020 14:59:24 -0700 Subject: [PATCH] Fix erroneous restart on initial device list notification issue: #768 --- .../sample/drumthumper/DrumThumperActivity.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/samples/drumthumper/src/main/java/com/google/oboe/sample/drumthumper/DrumThumperActivity.kt b/samples/drumthumper/src/main/java/com/google/oboe/sample/drumthumper/DrumThumperActivity.kt index 908ab5fce..9acd0db0d 100644 --- a/samples/drumthumper/src/main/java/com/google/oboe/sample/drumthumper/DrumThumperActivity.kt +++ b/samples/drumthumper/src/main/java/com/google/oboe/sample/drumthumper/DrumThumperActivity.kt @@ -36,6 +36,9 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen private var mDrumPlayer = DrumPlayer() + private val mUseDeviceChangeFallback = true + private var mDevicesInitialized = false + private var mDeviceListener: DeviceListener = DeviceListener() init { @@ -45,8 +48,13 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen inner class DeviceListener: AudioDeviceCallback() { override fun onAudioDevicesAdded(addedDevices: Array ) { - Toast.makeText(applicationContext, "Added Device", Toast.LENGTH_LONG).show() - resetOutput() + // Note: This will get called when the callback is installed. + if (mDevicesInitialized) { + // This is not the initial callback, so devices have changed + Toast.makeText(applicationContext, "Added Device", Toast.LENGTH_LONG).show() + resetOutput() + } + mDevicesInitialized = true } override fun onAudioDevicesRemoved(removedDevices: Array ) { @@ -57,11 +65,10 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen fun resetOutput() { if (mDrumPlayer.getOutputReset()) { // the (native) stream has been reset by the onErrorAfterClose() callback - mDrumPlayer.clearOutputReset(); + mDrumPlayer.clearOutputReset() } else { // give the (native) stream a chance to close it. - val timer = Timer("stream restart timer", false); - + val timer = Timer("stream restart timer", false) // schedule a single event timer.schedule(3000) { if (!mDrumPlayer.getOutputReset()) { @@ -70,7 +77,6 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen } } } - } } @@ -88,7 +94,9 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen override fun onResume() { super.onResume() - mAudioMgr!!.registerAudioDeviceCallback(mDeviceListener, null) + if (mUseDeviceChangeFallback) { + mAudioMgr!!.registerAudioDeviceCallback(mDeviceListener, null) + } // UI setContentView(R.layout.drumthumper_activity)