Browse Source

Continue playback animation after rotation

pull/1/head
Malte Schmitz 7 years ago
parent
commit
8787fe1add
2 changed files with 85 additions and 30 deletions
  1. +84
    -19
      app/src/main/java/de/mlte/soundboard/MainActivity.kt
  2. +1
    -11
      app/src/main/java/de/mlte/soundboard/SoundButton.kt

+ 84
- 19
app/src/main/java/de/mlte/soundboard/MainActivity.kt View File

@@ -1,5 +1,6 @@
package de.mlte.soundboard package de.mlte.soundboard


import android.animation.ValueAnimator
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@@ -11,6 +12,7 @@ import android.support.v7.widget.Toolbar
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.GridLayout import android.widget.GridLayout
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
@@ -19,6 +21,8 @@ class MainActivity : AppCompatActivity() {
private val buttons = ArrayList<SoundButton>() private val buttons = ArrayList<SoundButton>()
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
private var playing = false private var playing = false
private var playingButton: SoundButton? = null
private var playingAnimator: ValueAnimator? = null


override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -31,6 +35,53 @@ class MainActivity : AppCompatActivity() {
organizeButtons() 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<GridLayout>(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 { override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater val inflater = menuInflater
inflater.inflate(R.menu.main, menu) inflater.inflate(R.menu.main, menu)
@@ -85,28 +136,13 @@ class MainActivity : AppCompatActivity() {
mp.reset() mp.reset()
mp.release() mp.release()
} }
for (button in buttons) {
button.objectAnimator.cancel()
playingButton?.let { button ->
button.progressBar.progress = 0 button.progressBar.progress = 0
} }
playing = false playing = false
playingAnimator?.cancel()
} else { } 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) { private fun editButton(soundButton: SoundButton) {
val intent = Intent(baseContext, EditActivity::class.java) val intent = Intent(baseContext, EditActivity::class.java)
val parent = findViewById<GridLayout>(R.id.grid_layout) val parent = findViewById<GridLayout>(R.id.grid_layout)
@@ -206,5 +271,5 @@ class MainActivity : AppCompatActivity() {
} }
} }
} }
} }

+ 1
- 11
app/src/main/java/de/mlte/soundboard/SoundButton.kt View File

@@ -1,9 +1,7 @@
package de.mlte.soundboard package de.mlte.soundboard


import android.animation.ObjectAnimator
import android.content.Context import android.content.Context
import android.view.View import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.GridLayout import android.widget.GridLayout
import android.widget.ProgressBar import android.widget.ProgressBar
@@ -12,7 +10,6 @@ import android.widget.TextView
class SoundButton : FrameLayout { class SoundButton : FrameLayout {
val progressBar: ProgressBar val progressBar: ProgressBar
val textView: TextView val textView: TextView
val objectAnimator: ObjectAnimator
var soundId: Long = 0 var soundId: Long = 0
var fileName: String = "" var fileName: String = ""


@@ -20,15 +17,8 @@ class SoundButton : FrameLayout {
View.inflate(context, R.layout.layout_button, this) View.inflate(context, R.layout.layout_button, this)


progressBar = findViewById<ProgressBar>(R.id.progress_bar) progressBar = findViewById<ProgressBar>(R.id.progress_bar)
textView = findViewById<TextView>(R.id.text_view_button)

objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, 1000)
objectAnimator.interpolator = LinearInterpolator()
progressBar.max = 1000 progressBar.max = 1000
objectAnimator.addUpdateListener({ valueAnimator ->
val progress = valueAnimator.animatedValue as Int
progressBar.progress = progress
})
textView = findViewById<TextView>(R.id.text_view_button)
} }


fun move(col: Int, row: Int) { fun move(col: Int, row: Int) {


Loading…
Cancel
Save