Implement sounds in an android application

后端 未结 5 1796
遥遥无期
遥遥无期 2020-12-28 18:38

I am developing a game application in android. I have designed all the views and implemented all the functionality. Now in the last screen I have to play sounds in android.

5条回答
  •  滥情空心
    2020-12-28 19:40

    By using Media Player you can achieve this.

    here is the steps::

    1.- Create a new folder called "raw" inside "debug" folder. To create it right clic on "debug" folder > new > directory > and in name it "raw" :

    To add files just drag the .wav/.mp3 files to "raw" folder.

    2.- Import media player:

    import android.media.MediaPlayer;

    3.- Declare MediaPlayer global variable(s):

    public MediaPlayer mp1;
    

    4.- Inside onCreate method, asign the corresponding sounds:

    mp1 = MediaPlayer.create(MainActivity.this, raw.my_sound_name);
    

    5.- Finally, you can use methods...

    mp1.start() / mp1.stop() / mp1.pause()

提交回复
热议问题