package com.example.myapplication1
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
private val mediaPlayer = MediaPlayer()
// val buttonm: Button = (Button)findViewById(R.id.button1)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.firstlayout)
val button1: Button = findViewById(R.id.button1)
val button2: Button = findViewById(R.id.button2)
val button3: Button = findViewById(R.id.button3)
val button4: Button = findViewById(R.id.button4)
InitMediaPlayer()
button1.setOnClickListener {
if(!mediaPlayer.isPlaying) {
mediaPlayer.start()
}
}
button2.setOnClickListener {
if(mediaPlayer.isPlaying) {
mediaPlayer.pause()
}
}
button3.setOnClickListener {
if(mediaPlayer.isPlaying) {
mediaPlayer.reset()
InitMediaPlayer()
}
}
}
private fun InitMediaPlayer()
{
val assetManger =assets
val fd = assetManger.openFd("music.mp3")
mediaPlayer.setDataSource(fd.fileDescriptor , fd.startOffset , fd.length)
mediaPlayer.prepare()
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer.stop()
mediaPlayer.release()
}
}
android简单音乐播放器
最新推荐文章于 2024-08-23 15:48:00 发布