From 8ee2abf760038d7b5907c0a352969bde5feb568d Mon Sep 17 00:00:00 2001 From: Nils Date: Sun, 3 Sep 2017 13:44:14 +0200 Subject: [PATCH] Move object animator into sound button --- .../main/java/de/mlte/soundboard/MainActivity.kt | 100 ++++++++++----------- .../main/java/de/mlte/soundboard/SoundButton.kt | 21 +++++ 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/de/mlte/soundboard/MainActivity.kt b/app/src/main/java/de/mlte/soundboard/MainActivity.kt index 02c78c3..2143daa 100644 --- a/app/src/main/java/de/mlte/soundboard/MainActivity.kt +++ b/app/src/main/java/de/mlte/soundboard/MainActivity.kt @@ -19,6 +19,10 @@ import java.io.BufferedOutputStream class MainActivity : AppCompatActivity() { + private val buttons = ArrayList() + private var player: MediaPlayer? = null + private var playing = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -27,57 +31,6 @@ class MainActivity : AppCompatActivity() { loadPreferences() - var player: MediaPlayer? = null - val progressBar = findViewById(R.id.progress_bar) - val objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000) - objectAnimator.interpolator = LinearInterpolator() - progressBar.max = 1000 - objectAnimator.addUpdateListener({ valueAnimator -> - val progress = valueAnimator.animatedValue as Int - progressBar.progress = progress - }) - var playing = false - - val btn = findViewById(R.id.text_view_button) - btn.setOnClickListener { - if (playing) { - player?.let { mp -> - if (mp.isPlaying) { - mp.stop() - } - mp.reset() - mp.release() - } - objectAnimator.cancel() - playing = false - progressBar.progress = 0 - } else { - val file = getFileStreamPath("audio") - if (file.exists()) { - val mp = MediaPlayer.create(this, Uri.fromFile(file)) - mp.setOnCompletionListener { - progressBar.progress = 0 - mp.reset() - mp.release() - playing = false - } - mp.start() - player = mp - playing = true - - progressBar.progress = 0 - objectAnimator.setDuration(mp.duration.toLong()).start() - } - } - } - - btn.setOnLongClickListener { - val intent = Intent(baseContext, EditActivity::class.java) - intent.putExtra("caption", btn.text) - startActivityForResult(intent, 1234) - - true - } } private fun duplicateButton() { @@ -89,7 +42,50 @@ class MainActivity : AppCompatActivity() { for (row in 0..1) { for (col in 0..1) { val soundButton = SoundButton(this, col, row) + buttons.add(soundButton) parent.addView(soundButton) + + soundButton.btn.setOnClickListener { + if (playing) { + player?.let { mp -> + if (mp.isPlaying) { + mp.stop() + } + mp.reset() + mp.release() + } + for (button in buttons) { + button.objectAnimator.cancel() + button.progressBar.progress = 0 + } + playing = false + } else { + val file = getFileStreamPath("audio") + 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() + } + } + } + + soundButton.btn.setOnLongClickListener { + val intent = Intent(baseContext, EditActivity::class.java) + intent.putExtra("caption", soundButton.btn.text) + startActivityForResult(intent, 1234) + + true + } } } } @@ -137,4 +133,6 @@ 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 bac36c1..d89499a 100644 --- a/app/src/main/java/de/mlte/soundboard/SoundButton.kt +++ b/app/src/main/java/de/mlte/soundboard/SoundButton.kt @@ -1,11 +1,19 @@ 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 +import android.widget.TextView class SoundButton : FrameLayout { + val progressBar: ProgressBar + val btn: TextView + val objectAnimator: ObjectAnimator + constructor(context: Context, col: Int, row: Int) : super(context) { View.inflate(context, R.layout.layout_button, this) @@ -15,5 +23,18 @@ class SoundButton : FrameLayout { params.width = 0 params.height = 0 layoutParams = params + + progressBar = findViewById(R.id.progress_bar) + btn = findViewById(R.id.text_view_button) + + objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000) + objectAnimator.interpolator = LinearInterpolator() + progressBar.max = 1000 + objectAnimator.addUpdateListener({ valueAnimator -> + val progress = valueAnimator.animatedValue as Int + progressBar.progress = progress + }) } + + } \ No newline at end of file