just reset the mediaplayer in SelectAlarmSoundDialog, dont recreate it

This commit is contained in:
tibbi 2018-07-13 12:22:47 +02:00
parent 86f99b961b
commit 25f27d801c
2 changed files with 32 additions and 25 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 16 propMinSdkVersion = 16
propTargetSdkVersion = propCompileSdkVersion propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1 propVersionCode = 1
propVersionName = '4.4.13' propVersionName = '4.4.14'
kotlin_version = '1.2.51' kotlin_version = '1.2.51'
support_libs = '27.1.1' support_libs = '27.1.1'
} }

View file

@ -29,7 +29,7 @@ class SelectAlarmSoundDialog(val activity: BaseSimpleActivity, val currentUri: S
private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null) private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null)
private var systemAlarmSounds = ArrayList<AlarmSound>() private var systemAlarmSounds = ArrayList<AlarmSound>()
private var yourAlarmSounds = ArrayList<AlarmSound>() private var yourAlarmSounds = ArrayList<AlarmSound>()
private var mediaPlayer = MediaPlayer() private var mediaPlayer: MediaPlayer? = null
private val config = activity.baseConfig private val config = activity.baseConfig
private val dialog: AlertDialog private val dialog: AlertDialog
@ -45,8 +45,8 @@ class SelectAlarmSoundDialog(val activity: BaseSimpleActivity, val currentUri: S
addYourAlarms() addYourAlarms()
dialog = AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.setOnDismissListener { mediaPlayer.stop() } .setOnDismissListener { mediaPlayer?.stop() }
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() }) .setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.create().apply { .create().apply {
activity.setupDialogStuff(view, this) activity.setupDialogStuff(view, this)
@ -102,8 +102,9 @@ class SelectAlarmSoundDialog(val activity: BaseSimpleActivity, val currentUri: S
} }
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
private fun alarmClicked(alarmSound: AlarmSound) = when { private fun alarmClicked(alarmSound: AlarmSound) {
alarmSound.uri == SILENT -> mediaPlayer.stop() when {
alarmSound.uri == SILENT -> mediaPlayer?.stop()
alarmSound.id == ADD_NEW_SOUND_ID -> { alarmSound.id == ADD_NEW_SOUND_ID -> {
val action = if (isKitkatPlus()) Intent.ACTION_OPEN_DOCUMENT else Intent.ACTION_GET_CONTENT val action = if (isKitkatPlus()) Intent.ACTION_OPEN_DOCUMENT else Intent.ACTION_GET_CONTENT
Intent(action).apply { Intent(action).apply {
@ -117,11 +118,16 @@ class SelectAlarmSoundDialog(val activity: BaseSimpleActivity, val currentUri: S
dialog.dismiss() dialog.dismiss()
} }
else -> try { else -> try {
mediaPlayer.stop() mediaPlayer?.reset()
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer().apply { mediaPlayer = MediaPlayer().apply {
setAudioStreamType(audioStream) setAudioStreamType(audioStream)
setDataSource(activity, Uri.parse(alarmSound.uri))
isLooping = loopAudio isLooping = loopAudio
}
}
mediaPlayer?.apply {
setDataSource(activity, Uri.parse(alarmSound.uri))
prepare() prepare()
start() start()
} }
@ -129,6 +135,7 @@ class SelectAlarmSoundDialog(val activity: BaseSimpleActivity, val currentUri: S
activity.showErrorToast(e) activity.showErrorToast(e)
} }
} }
}
private fun removeAlarmSound(alarmSound: AlarmSound) { private fun removeAlarmSound(alarmSound: AlarmSound) {
val token = object : TypeToken<ArrayList<AlarmSound>>() {}.type val token = object : TypeToken<ArrayList<AlarmSound>>() {}.type