how to play a sound on click of button

后端 未结 4 1472
梦毁少年i
梦毁少年i 2021-01-15 13:16

i am trying to play sound file on the emulator on click of button but i am getting message \"application play audio has stopped unexpectedly\"

my codes are:

4条回答
  •  我在风中等你
    2021-01-15 14:01

    Move the creation of the MediaPlayer instance to the onCreate method. This will make your application run:

    // creating instance of media player
    MediaPlayer mp; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mp = MediaPlayer.create(this, R.raw.ljudman__grenade);
    

    Also, if you're trying to start a new instance of your activity upon the button click I believe this is the correct way to do it:

    startActivity(new Intent(p1.this, p1.class));
    

提交回复
热议问题