Android: Play mp3 file from raw resource on click of a TextView

前端 未结 4 1289
挽巷
挽巷 2020-12-03 05:11

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

相关标签:
4条回答
  • 2020-12-03 05:20

    When you create the mPlayer object you should pass it the Context, which is in your case PlayWord.this.

    0 讨论(0)
  • 2020-12-03 05:20
    MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
        mPlayer.start();
    
    0 讨论(0)
  • 2020-12-03 05:21

    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().

    0 讨论(0)
  • 2020-12-03 05:39
    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

    0 讨论(0)
提交回复
热议问题