adding some progress updating improvements

This commit is contained in:
tibbi 2020-04-01 17:49:05 +02:00
parent 14d8511b7b
commit ed7ff4d9fe
1 changed files with 32 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import android.os.Looper
import android.os.PowerManager import android.os.PowerManager
import android.provider.MediaStore import android.provider.MediaStore
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.SeekBar
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.activities.SimpleActivity
@ -104,6 +105,17 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
player = MediaPlayer().apply { player = MediaPlayer().apply {
setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK) setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK)
setAudioStreamType(AudioManager.STREAM_MUSIC) setAudioStreamType(AudioManager.STREAM_MUSIC)
setOnCompletionListener {
progressTimer.cancel()
player_progressbar.progress = player_progressbar.max
player_progress_current.text = player_progress_max.text
}
setOnPreparedListener {
setupProgressTimer()
player?.start()
}
} }
} }
@ -113,33 +125,46 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
recording.id.toLong() recording.id.toLong()
) )
updateCurrentProgress(0)
player_progressbar.progress = 0
player_progressbar.max = recording.duration player_progressbar.max = recording.duration
player_title.text = recording.title player_title.text = recording.title
player_progress_max.text = recording.duration.getFormattedDuration() player_progress_max.text = recording.duration.getFormattedDuration()
updateCurrentProgress(0)
player!!.apply { player!!.apply {
reset() reset()
setDataSource(context, recordingUri) setDataSource(context, recordingUri)
prepare() prepare()
start()
} }
songStateChanged(true) songStateChanged(true)
setupProgressTimer()
player_progressbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
if (fromUser) {
player?.seekTo(progress * 1000)
player_progress_current.text = progress.getFormattedDuration()
resumeSong()
}
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
} }
private fun setupProgressTimer() { private fun setupProgressTimer() {
progressTimer.cancel() progressTimer.cancel()
progressTimer = Timer() progressTimer = Timer()
progressTimer.scheduleAtFixedRate(getProgressUpdateTask(), 1000, 1000) progressTimer.scheduleAtFixedRate(getProgressUpdateTask(), 0, 1000)
} }
private fun getProgressUpdateTask() = object : TimerTask() { private fun getProgressUpdateTask() = object : TimerTask() {
override fun run() { override fun run() {
if (player != null) { if (player != null) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
val progress = player!!.currentPosition / 1000 val progress = Math.round(player!!.currentPosition / 1000.toDouble()).toInt()
updateCurrentProgress(progress) updateCurrentProgress(progress)
player_progressbar.progress = progress player_progressbar.progress = progress
} }
@ -162,11 +187,13 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
private fun pauseSong() { private fun pauseSong() {
player?.pause() player?.pause()
songStateChanged(false) songStateChanged(false)
progressTimer.cancel()
} }
private fun resumeSong() { private fun resumeSong() {
player?.start() player?.start()
songStateChanged(true) songStateChanged(true)
setupProgressTimer()
} }
private fun songStateChanged(isPlaying: Boolean) { private fun songStateChanged(isPlaying: Boolean) {