声音播放
This commit is contained in:
parent
5f09d0646f
commit
7376b902e9
|
|
@ -537,7 +537,7 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
fun playAlert(count: Int, volume: Float) {
|
fun playAlert(count: Int, volume: Float) {
|
||||||
Handler(Looper.getMainLooper()).post {
|
Handler(Looper.getMainLooper()).post {
|
||||||
try {
|
try {
|
||||||
stopAlert()
|
realStopAlert()
|
||||||
alertPlayCount = 0
|
alertPlayCount = 0
|
||||||
alertPlayer = android.media.MediaPlayer.create(activity, R.raw.pull_alert).apply {
|
alertPlayer = android.media.MediaPlayer.create(activity, R.raw.pull_alert).apply {
|
||||||
setVolume(volume, volume)
|
setVolume(volume, volume)
|
||||||
|
|
@ -549,10 +549,10 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
it.start()
|
it.start()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.tag("AlertPlayer").e(e, "Error repeating alert play")
|
Timber.tag("AlertPlayer").e(e, "Error repeating alert play")
|
||||||
stopAlert()
|
realStopAlert()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stopAlert()
|
realStopAlert()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
start()
|
start()
|
||||||
|
|
@ -565,19 +565,27 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopAlert() {
|
fun stopAlert() {
|
||||||
Handler(Looper.getMainLooper()).post {
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||||
try {
|
realStopAlert()
|
||||||
alertPlayer?.let { player ->
|
} else {
|
||||||
if (player.isPlaying) {
|
Handler(Looper.getMainLooper()).post {
|
||||||
player.stop()
|
realStopAlert()
|
||||||
}
|
|
||||||
player.release()
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.tag("AlertPlayer").e(e, "Error stopping alert player")
|
|
||||||
} finally {
|
|
||||||
alertPlayer = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun realStopAlert() {
|
||||||
|
try {
|
||||||
|
alertPlayer?.let { player ->
|
||||||
|
if (player.isPlaying) {
|
||||||
|
player.stop()
|
||||||
|
}
|
||||||
|
player.release()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.tag("AlertPlayer").e(e, "Error stopping alert player")
|
||||||
|
} finally {
|
||||||
|
alertPlayer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue