From 8787fe1add6f07b81a2f97666db5ff0c50f90255 Mon Sep 17 00:00:00 2001 From: Malte Schmitz Date: Sat, 17 Feb 2018 18:34:27 +0100 Subject: [PATCH] Continue playback animation after rotation --- .../main/java/de/mlte/soundboard/MainActivity.kt | 103 +++++++++++++++++---- .../main/java/de/mlte/soundboard/SoundButton.kt | 12 +-- 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/de/mlte/soundboard/MainActivity.kt b/app/src/main/java/de/mlte/soundboard/MainActivity.kt index 44af2cd..617d7ae 100644 --- a/app/src/main/java/de/mlte/soundboard/MainActivity.kt +++ b/app/src/main/java/de/mlte/soundboard/MainActivity.kt @@ -1,5 +1,6 @@ package de.mlte.soundboard +import android.animation.ValueAnimator import android.app.Activity import android.content.Context import android.content.Intent @@ -11,6 +12,7 @@ import android.support.v7.widget.Toolbar import android.view.Menu import android.view.MenuItem import android.view.View +import android.view.animation.LinearInterpolator import android.widget.GridLayout import java.io.BufferedInputStream import java.io.BufferedOutputStream @@ -19,6 +21,8 @@ class MainActivity : AppCompatActivity() { private val buttons = ArrayList() private var player: MediaPlayer? = null private var playing = false + private var playingButton: SoundButton? = null + private var playingAnimator: ValueAnimator? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -31,6 +35,53 @@ class MainActivity : AppCompatActivity() { organizeButtons() } + override fun onSaveInstanceState(outState: Bundle?) { + if (playing) { + player?.let { mp -> + if (mp.isPlaying) { + outState?.putBoolean("playing", true) + outState?.putInt("playingPosition", mp.currentPosition) + val parent = findViewById(R.id.grid_layout) + val index = parent.indexOfChild(playingButton) + outState?.putInt("playingIndex", index) + } + } + } + + super.onSaveInstanceState(outState) + } + + override fun onDestroy() { + if (playing) { + player?.let { mp -> + if (mp.isPlaying) { + mp.stop() + } + mp.reset() + mp.release() + } + playingAnimator?.cancel() + } + + super.onDestroy() + } + + override fun onRestoreInstanceState(savedInstanceState: Bundle?) { + super.onRestoreInstanceState(savedInstanceState) + + buttons.forEach { button -> + button.progressBar.progress = 0 + } + + savedInstanceState?.let { state -> + if (state.getBoolean("playing")) { + val button = buttons[state.getInt("playingIndex")] + val position = state.getInt("playingPosition") + startPlaying(button, position) + } + } + } + override fun onCreateOptionsMenu(menu: Menu?): Boolean { val inflater = menuInflater inflater.inflate(R.menu.main, menu) @@ -85,28 +136,13 @@ class MainActivity : AppCompatActivity() { mp.reset() mp.release() } - for (button in buttons) { - button.objectAnimator.cancel() + playingButton?.let { button -> button.progressBar.progress = 0 } playing = false + playingAnimator?.cancel() } else { - val file = getFileStreamPath("audio" + soundButton.soundId) - if (file.exists()) { - val mp = MediaPlayer.create(this, Uri.fromFile(file)) - mp.setOnCompletionListener { - soundButton.progressBar.progress = 0 - mp.reset() - mp.release() - playing = false - } - mp.start() - player = mp - playing = true - - soundButton.progressBar.progress = 0 - soundButton.objectAnimator.setDuration(mp.duration.toLong()).start() - } + startPlaying(soundButton, 0) } } @@ -116,6 +152,35 @@ class MainActivity : AppCompatActivity() { } } + private fun startPlaying(soundButton: SoundButton, position: Int) { + val file = getFileStreamPath("audio" + soundButton.soundId) + if (file.exists()) { + val mp = MediaPlayer.create(this, Uri.fromFile(file)) + mp.setOnCompletionListener { + playingAnimator?.cancel() + soundButton.progressBar.progress = 0 + mp.reset() + mp.release() + playing = false + } + mp.seekTo(position) + mp.start() + player = mp + playing = true + playingButton = soundButton + + val bar = soundButton.progressBar + val animator = ValueAnimator.ofInt(bar.max * position / mp.duration, bar.max) + animator.interpolator = LinearInterpolator() + animator.addUpdateListener { + bar.progress = animator.animatedValue as Int + } + animator.duration = mp.duration.toLong() - position + animator.start() + playingAnimator = animator + } + } + private fun editButton(soundButton: SoundButton) { val intent = Intent(baseContext, EditActivity::class.java) val parent = findViewById(R.id.grid_layout) @@ -206,5 +271,5 @@ class MainActivity : AppCompatActivity() { } } } - + } \ No newline at end of file diff --git a/app/src/main/java/de/mlte/soundboard/SoundButton.kt b/app/src/main/java/de/mlte/soundboard/SoundButton.kt index 970698b..a5e8f98 100644 --- a/app/src/main/java/de/mlte/soundboard/SoundButton.kt +++ b/app/src/main/java/de/mlte/soundboard/SoundButton.kt @@ -1,9 +1,7 @@ package de.mlte.soundboard -import android.animation.ObjectAnimator import android.content.Context import android.view.View -import android.view.animation.LinearInterpolator import android.widget.FrameLayout import android.widget.GridLayout import android.widget.ProgressBar @@ -12,7 +10,6 @@ import android.widget.TextView class SoundButton : FrameLayout { val progressBar: ProgressBar val textView: TextView - val objectAnimator: ObjectAnimator var soundId: Long = 0 var fileName: String = "" @@ -20,15 +17,8 @@ class SoundButton : FrameLayout { View.inflate(context, R.layout.layout_button, this) progressBar = findViewById(R.id.progress_bar) - textView = findViewById(R.id.text_view_button) - - objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, 1000) - objectAnimator.interpolator = LinearInterpolator() progressBar.max = 1000 - objectAnimator.addUpdateListener({ valueAnimator -> - val progress = valueAnimator.animatedValue as Int - progressBar.progress = progress - }) + textView = findViewById(R.id.text_view_button) } fun move(col: Int, row: Int) {