Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created Sep 20, 2020
Embed
What would you like to do?
Kotlin (Android Studio) Tutorial - The Simple Music Player -
package com.codingwithsara.kotlinmusicplayer
import android.annotation.SuppressLint
import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var mp: MediaPlayer
private var totalTime: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mp = MediaPlayer.create(this, R.raw.music)
mp.isLooping = true
mp.setVolume(0.5f, 0.5f)
totalTime = mp.duration
// Volume Bar
volumeBar.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekbar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
var volumeNum = progress / 100.0f
mp.setVolume(volumeNum, volumeNum)
}
}
override fun onStartTrackingTouch(p0: SeekBar?) {
}
override fun onStopTrackingTouch(p0: SeekBar?) {
}
}
)
// Position Bar
positionBar.max = totalTime
positionBar.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
mp.seekTo(progress)
}
}
override fun onStartTrackingTouch(p0: SeekBar?) {
}
override fun onStopTrackingTouch(p0: SeekBar?) {
}
}
)
// Thread
Thread(Runnable {
while (mp != null) {
try {
var msg = Message()
msg.what = mp.currentPosition
handler.sendMessage(msg)
Thread.sleep(1000)
} catch (e: InterruptedException) {
}
}
}).start()
}
@SuppressLint("HandlerLeak")
var handler = object : Handler() {
override fun handleMessage(msg: Message) {
var currentPosition = msg.what
// Update positionBar
positionBar.progress = currentPosition
// Update Labels
var elapsedTime = createTimeLabel(currentPosition)
elapsedTimeLabel.text = elapsedTime
var remainingTime = createTimeLabel(totalTime - currentPosition)
remainingTimeLabel.text = "-$remainingTime"
}
}
fun createTimeLabel(time: Int): String {
var timeLabel = ""
var min = time / 1000 / 60
var sec = time / 1000 % 60
timeLabel = "$min:"
if (sec < 10) timeLabel += "0"
timeLabel += sec
return timeLabel
}
fun playBtnClick(v: View) {
if (mp.isPlaying) {
// Stop
mp.pause()
playBtn.setBackgroundResource(R.drawable.play)
} else {
// Start
mp.start()
playBtn.setBackgroundResource(R.drawable.stop)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment