Hi guys I want to play a certain mp3 file when a text is clicked. For example, I clicked the word \"Nicholas\", the app have to play nicholas.mp3...
Sorry for my mes
When you create the mPlayer
object you should pass it the Context, which is in your case PlayWord.this
.
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
You need to pass in a context instance into MediaPlayer.create method:
MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.aaanicholas);
Also, after the create()
call, prepare is already executed, so you don't need to execute it explicitly, just invoke start()
right after create()
.
val mediaPlayer = MediaPlayer.create(this,R.raw.music)
mediaPlayer.isLooping=true
mediaPlayer.start()
music_on_off_button.setOnClickListener {
if (mediaPlayer.isPlaying){
mediaPlayer.pause()
}else{
mediaPlayer.start()
}
}
You can know the music playing or not by using isPlaying() method