我有一项活动,我想播放背景音乐。我该怎么办?
答案 0 :(得分:1)
首先,您可以创建一个文件夹来保存您的音频文件。此文件夹的名称为:raw
,因此您可以将声音粘贴到那里,当您开始活动时,您就可以开始播放歌曲了。示例:
首先,声明你的变量:
var mMediaPlayer: MediaPlayer? = null
现在,您可以开始播放声音了:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (mMediaPlayer == null) { //mMediaPkayer is your variable
mMediaPlayer = MediaPlayer.create(this, R.raw.water) //raw is the folder where you have the audio files or sounds, water is the audio file (is a example right)
mMediaPlayer!!.isLooping = true //to repeat again n again
mMediaPlayer!!.start() //to start the sound
}else mMediaPlayer!!.start()
}