There are two main ways of doing this:
Add a sound file to your project's Res/raw directory.
1, MediaPlayer play general audio
private void PlaySound () {final MediaPlayer MediaPlayer = mediaplayer.create (activity, r.raw.bubble); Mediaplayer.start (); Mediaplayer.setoncompletionlistener (New Mediaplayer.oncompletionlistener () {//Broadcast completed event @Override public void Oncompletion (MediaPlayer arg0) { mediaplayer.release ();}} ); }
2, Soundpool play short sound effects (maximum can only apply for 1M of memory space)
private void Playsounds () {//Specifies the maximum number of audio streams for the sound pool is 10, with a sound quality of 5 soundpool pool = new Soundpool (audiomanager.stream_system , 5); Load the audio stream, return the ID in the pool final int sourceid = pool.load (activity, r.raw.bubble, 0); Pool.setonloadcompletelistener (New Onloadcompletelistener () {public void Onloadcomplete (Soundpool SoundPool, int SampleID, int status) {//Play audio, second parameter is left channel volume, third parameter is right channel volume,//fourth parameter is priority, fifth parameter is loop number, 0 does not loop, 1 loop;//sixth parameter is rate, rate Minimum 0.5 up to 2, 1 for normal speed soundpool.play (SourceID, 2, 2, 0, 0, 1);});}
Android Play Beep