Android Play sound, one is Soundpool, one is MediaPlayer
Soundpool is suitable for playback of sound effects that reflect high speed requirements, such as in-game explosion sound
Mediaplay for longer sound effects, such as in-game background music
Let's do an example, one is Chang.ogg, the other is duan.wav.
These two sound effect files, I was from my game directory in the laughter of the lake ol in the search out. You can also search for *.ogg,*.wav under your game folder
Place these two files in the Res/raw directory, and if the raw directory does not exist, create it
Define Activity_main.xml, put a TextView and 4 buttons inside
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.ssln.sound.MainActivity" > <TextViewAndroid:id= "@+id/tvmsg"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "No sound is played" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "SoundPlayer Play" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "SoundPlayer Stop" /> <ButtonAndroid:id= "@+id/button3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "MediaPlayer Play" /> <ButtonAndroid:id= "@+id/button4"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "MediaPlayer Stop" /></LinearLayout>
Then we implement the Click button to play and stop the sound in Mainactivity.java
Package Com.ssln.sound;import java.util.hashmap;import android.app.activity;import android.content.Context; Import Android.media.audiomanager;import Android.media.mediaplayer;import Android.media.soundpool;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.TextView;public Class Mainactivity extends Activity implements View.onclicklistener {//4 buttons and one TextView private button Btnsoundstar T, Btnsoundstop, Btnmediastart, btnmediastop; Private TextView tvmsg; Private MediaPlayer MPlayer; Private Soundpool Msound; Private HashMap<Integer, Integer>Soundpoolmap; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initsounds (); Setcontentview (R.layout.activity_main); Btnsoundstart = (Button) Findviewbyid (R.id.button1); Btnsoundstop = (Button) Findviewbyid (R.id.button2); Btnmediastart = (Button) Findviewbyid (R.id.button3); Btnmediastop = (Button) Findviewbyid (R.ID.BUTTON4); Tvmsg = (TextView) Findviewbyid (r.id.tvmsg); Btnsoundstart.setonclicklistener (this); Btnsoundstop.setonclicklistener (this); Btnmediastart.setonclicklistener (this); Btnmediastop.setonclicklistener (this); @Override public void OnClick (View v) {if (v = = Btnsoundstart) {this. PlaySound (1, 0); Tvmsg.settext ("Soundpool play"); } else if (v = = btnsoundstop) {msound.pause (1); Tvmsg.settext ("Soundpool suspended"); }else if (V==btnmediastart) {
if (!mplayer.isplaying ()) Mplayer.start (); Tvmsg.settext ("MediaPlayer play"); }else if (v==btnmediastop) {
if (mplayer.isplaying ()) Mplayer.pause (); Tvmsg.settext ("MediaPlayer suspended"); }}/** * Initialize sound * */private void Initsounds () {//set playback sound MPlayer = Mediaplayer.create (This, R.raw.chang); The first parameter is the maximum number of simultaneous data streams, the second data stream type, and the third is the sound quality msound = new Soundpool (4, Audiomanager.stream_music, 100); Soundpoolmap = new HashMap<Integer, Integer>(); Soundpoolmap.put (1, Msound.load (this, R.raw.duan, 1)); You can continue to put the audio file in the back}/** * Soundpool play * * @param sound * Play first * @param loop * Whether to loop */private void PlaySound (int sound, int loop) {Audiomanager mgr = (Audiomanager) This . Getsystemservice (Context.audio_service); Gets the current volume of the system sound float currentvolume = Mgr.getstreamvolume (Audiomanager.stream_music); Gets the maximum volume of the system sound float maxvolume = Mgr.getstreammaxvolume (Audiomanager.stream_music); Gets the percentage of current volume float volume = Currentvolume/maxvolume; The first parameter is the sound effect ID, the second is the left channel volume, the third is the right channel volume, the fourth is the priority of the stream, the minimum is 0, the fifth is whether to loop playback, sixth playback speed (1.0 = Normal playback, range 0.5-2.0) Msound.play (soundpoolmap . get (sound), volume, volume, 1, loop, 1f); }}
The program works as follows:
Click on the button experiment Bar ~~!
Android Sound Playback