使用kotlin语言写一个android的音乐播放器
时间: 2023-06-19 22:07:24 浏览: 257
好的,下面是一个简单的 Android 音乐播放器示例,使用 Kotlin 语言编写:
首先,需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
```
然后,在 MainActivity.kt 文件中添加以下代码:
```kotlin
import android.media.MediaPlayer
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException
class MainActivity : AppCompatActivity() {
private var mediaPlayer: MediaPlayer? = null
private var isPlaying = false
private var isPaused = false
private var currentSongIndex = 0
private var songList = mutableListOf<String>()
private lateinit var seekBarHandler: Handler
private lateinit var runnable: Runnable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Add songs to playlist
songList.add("/sdcard/Music/Song1.mp3")
songList.add("/sdcard/Music/Song2.mp3")
songList.add("/sdcard/Music/Song3.mp3")
// Initialize media player
mediaPlayer = MediaPlayer()
// Set up seek bar handler to update seek bar position
seekBarHandler = object : Handler() {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
if (mediaPlayer != null) {
val currentPosition = mediaPlayer!!.currentPosition
seek_bar.progress = currentPosition
val duration = mediaPlayer!!.duration
val progress = (currentPosition.toFloat() / duration.toFloat() * 100).toInt()
seek_bar_percent.text = "$progress%"
if (mediaPlayer!!.isPlaying) {
runnable = Runnable { sendMessageDelayed(obtainMessage(0), 1000) }
postDelayed(runnable, 1000)
}
}
}
}
// Set up seek bar listener to seek to specific position
seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
if (fromUser) {
mediaPlayer?.seekTo(progress)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
seekBarHandler.removeCallbacks(runnable)
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
seekBarHandler.removeCallbacks(runnable)
mediaPlayer?.seekTo(seekBar.progress)
runnable = Runnable { seekBarHandler.sendEmptyMessage(0) }
seekBarHandler.postDelayed(runnable, 1000)
}
})
// Set up play button listener to play or pause current song
btn_play.setOnClickListener {
if (!isPlaying) {
if (isPaused) {
mediaPlayer?.start()
isPaused = false
} else {
playSong(currentSongIndex)
}
isPlaying = true
btn_play.setImageResource(R.drawable.ic_pause)
} else {
mediaPlayer?.pause()
isPaused = true
isPlaying = false
btn_play.setImageResource(R.drawable.ic_play)
}
}
// Set up next button listener to play next song in playlist
btn_next.setOnClickListener {
if (currentSongIndex < songList.size - 1) {
currentSongIndex++
} else {
currentSongIndex = 0
}
playSong(currentSongIndex)
}
// Set up previous button listener to play previous song in playlist
btn_prev.setOnClickListener {
if (currentSongIndex > 0) {
currentSongIndex--
} else {
currentSongIndex = songList.size - 1
}
playSong(currentSongIndex)
}
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
seekBarHandler.removeCallbacks(runnable)
}
private fun playSong(index: Int) {
try {
mediaPlayer?.reset()
mediaPlayer?.setDataSource(songList[index])
mediaPlayer?.prepare()
mediaPlayer?.start()
seek_bar.max = mediaPlayer!!.duration
seekBarHandler.sendEmptyMessage(0)
isPlaying = true
btn_play.setImageResource(R.drawable.ic_pause)
} catch (e: IOException) {
Log.e("MainActivity", "Error playing song: ${e.message}")
}
}
}
```
这个示例实现了以下功能:
- 播放/暂停当前歌曲;
- 播放下一首歌曲;
- 播放上一首歌曲;
- 显示歌曲进度,并允许拖动进度条到指定位置。
请注意,这只是一个简单的示例,实际的音乐播放器可能需要更复杂的功能。
阅读全文