Play sound on button click android

后端 未结 11 1278
清酒与你
清酒与你 2020-11-29 16:10

How do I get a button to play a sound from raw when click? I just created a button with id button1, but whatever code I write, all is wrong.

imp         


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 16:43

    public class MainActivity extends AppCompatActivity {
    
        public void clickMe (View view) {
    
            MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx);
            mp.start();
    
        }
    

    create a button with a method could be called when the button pressed (onCreate),

    then create a variable for (MediaPlayer) class with the path of your file

    MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx);
    

    finally run start method in that class

    mp.start();
    

    the file will run when the button pressed, hope this was helpful!

提交回复
热议问题