[英]How to play a different sound file from res>raw
Under class I have在课下我有
private lateinit var mp: MediaPlayer
Under override I have在覆盖下我有
mp = MediaPlayer.create(this, R.raw.a1)
Function buttonPlayClick
which is also buttonPlay
函数
buttonPlayClick
也是buttonPlay
fun buttonPlayClick(v: View)
{
if (mp.isPlaying)
{
mp.pause()
buttonPlay.text = "PLAY"
} else {
mp.start()
buttonPlay.text = "PAUSE"
}
}
I am using the code below to point to a sound file我使用下面的代码指向一个声音文件
mp = MediaPlayer.create(this, R.raw.a1)
Currently, the command is pointing to a sound file called a1 in res>raw当前,该命令指向 res>raw 中名为 a1 的声音文件
I am learning Kotlin bit by bit and I am trying to play a different sound file using the command below (changed a1 to x)我正在一点一点地学习 Kotlin,我正在尝试使用以下命令播放不同的声音文件(将 a1 更改为 x)
mp = MediaPlayer.create(this, R.raw.x)
I was hoping that at some point in the app I can define x = a1
or a2
or a3
to play different sounds files but it does not work like that.我希望在应用程序的某个时候我可以定义
x = a1
或a2
或a3
来播放不同的声音文件,但它不能那样工作。 I also noticed that the sound files cannot just be an integer value.我还注意到声音文件不能只是一个整数值。 The sound files are very short (3 to 10 seconds)
声音文件很短(3 到 10 秒)
Thanks for all the help!感谢所有的帮助!
You can do something like this :你可以这样做:
var a1 = R.raw.a1
var a2 = R.raw.a2
var a3 = R.raw.a3
Or you can do it with JAVA
like this :或者你可以像这样用
JAVA
来做到这一点:
int setMusic(String mMusic){
return this.getResources().getIdentifier(mMusic, "raw", this.getPackageName());
}
and call it like this :并这样称呼它:
mp = MediaPlayer.create(this, setMusic("a1"))
or或者
mp = MediaPlayer.create(this, setMusic("a2"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.