Skip to content

Commit

Permalink
Fix erroneous restart on initial device list notification
Browse files Browse the repository at this point in the history
issue: #768
  • Loading branch information
android-usb committed Feb 7, 2020
1 parent c4d7e73 commit babddb0
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,8 +48,13 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen

inner class DeviceListener: AudioDeviceCallback() {
override fun onAudioDevicesAdded(addedDevices: Array<AudioDeviceInfo> ) {
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<AudioDeviceInfo> ) {
Expand All @@ -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()) {
Expand All @@ -70,7 +77,6 @@ class DrumThumperActivity : AppCompatActivity(), TriggerPad.DrumPadTriggerListen
}
}
}

}
}

Expand All @@ -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)
Expand Down

0 comments on commit babddb0

Please sign in to comment.