update playback progress appropriately

This commit is contained in:
tibbi 2020-04-01 16:51:38 +02:00
parent d485a89aba
commit 0796147fee
1 changed files with 25 additions and 1 deletions

View File

@ -6,6 +6,8 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.media.AudioManager import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Handler
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
@ -14,9 +16,12 @@ import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.fragment_player.view.* import kotlinx.android.synthetic.main.fragment_player.view.*
import java.util.*
import kotlin.collections.ArrayList
class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
private var progressTimer = Timer()
override fun onResume() { override fun onResume() {
setupColors() setupColors()
@ -26,6 +31,8 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
player?.stop() player?.stop()
player?.release() player?.release()
player = null player = null
progressTimer.cancel()
} }
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
@ -101,9 +108,10 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
recording.id.toLong() recording.id.toLong()
) )
updateCurrentProgress(0) 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()
@ -111,6 +119,22 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
prepare() prepare()
start() start()
} }
progressTimer.cancel()
progressTimer = Timer()
progressTimer.scheduleAtFixedRate(getProgressUpdateTask(), 1000, 1000)
}
private fun getProgressUpdateTask() = object : TimerTask() {
override fun run() {
if (player != null) {
Handler(Looper.getMainLooper()).post {
val progress = player!!.currentPosition / 1000
player_progressbar.progress = progress
player_progress_current.text = progress.getFormattedDuration()
}
}
}
} }
private fun updateCurrentProgress(seconds: Int) { private fun updateCurrentProgress(seconds: Int) {