| @@ -14,36 +14,51 @@ class MainActivity : AppCompatActivity() { | |||||
| super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||||
| setContentView(R.layout.activity_main) | setContentView(R.layout.activity_main) | ||||
| val mp = MediaPlayer.create(this, R.raw.splash) | |||||
| var player: MediaPlayer? = null | |||||
| val progressBar = findViewById<ProgressBar>(R.id.progress_bar) | val progressBar = findViewById<ProgressBar>(R.id.progress_bar) | ||||
| var timer = Timer() | var timer = Timer() | ||||
| mp.setOnCompletionListener { | |||||
| timer.cancel() | |||||
| progressBar.progress = 0 | |||||
| } | |||||
| var playing = false | |||||
| val btn = findViewById<TextView>(R.id.text_view_button) | val btn = findViewById<TextView>(R.id.text_view_button) | ||||
| btn.setOnClickListener { | btn.setOnClickListener { | ||||
| if (mp.isPlaying) { | |||||
| mp.stop() | |||||
| mp.prepare() | |||||
| if (playing) { | |||||
| player?.let { mp -> | |||||
| if (mp.isPlaying) { | |||||
| mp.stop() | |||||
| } | |||||
| mp.reset() | |||||
| mp.release() | |||||
| } | |||||
| playing = false | |||||
| timer.cancel() | timer.cancel() | ||||
| progressBar.progress = 0 | progressBar.progress = 0 | ||||
| } else { | } else { | ||||
| val mp = MediaPlayer.create(this, R.raw.splash) | |||||
| mp.setOnCompletionListener { | |||||
| timer.cancel() | |||||
| progressBar.progress = 0 | |||||
| mp.reset() | |||||
| mp.release() | |||||
| playing = false | |||||
| } | |||||
| mp.start() | |||||
| player = mp | |||||
| playing = true | |||||
| progressBar.max = mp.duration | progressBar.max = mp.duration | ||||
| progressBar.progress = 0 | |||||
| timer = Timer() | timer = Timer() | ||||
| val timerTask = timerTask { | val timerTask = timerTask { | ||||
| runOnUiThread { | runOnUiThread { | ||||
| progressBar.progress = mp.currentPosition | |||||
| println(mp.currentPosition) | |||||
| if (playing && mp.isPlaying) { | |||||
| if (mp.currentPosition > progressBar.progress) { | |||||
| progressBar.progress = mp.currentPosition | |||||
| } | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| timer.schedule(timerTask, 40, 40) | timer.schedule(timerTask, 40, 40) | ||||
| mp.start() | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||