Android Sound Playback Mediaplay Soundpool

Source: Internet
Author: User

First, create a new raw folder within the Res folder and store the sound file inside the raw.

Mediaplay Method:

Add within the OnCreate (Bundle saveinstancestate) method

"Mediaplay mp=mediaplayer.create (Context,r.raw.backsound);"//For initializing Mediapaly

Mediaplayer.create (context context, int resid)

Mdeiaplayer.create (Context Context,uri Uri)

And then

Mp.start ();//play sound with Mediaplay

Mp.pause ();//Stop playback

Mp.isplaying ();//used to determine if Mediaplay is playing

Soundpool method

Always put the sound inside the HashMap

SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);

Rate is the playback speed, the slowest is 0.5, the fastest is 2

Loop for Play Count

 PackageCom.example.sound;ImportJava.util.HashMap;ImportAndroid.media.AudioManager;ImportAndroid.media.MediaPlayer;ImportAndroid.media.SoundPool;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsOnclicklistener {Button bt1,bt2,bt3,bt4;    TextView TV;    MediaPlayer MP;    Soundpool sp; HashMap<Integer,Integer>SPM; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                               Initsounds (); //Initialize SoundInit ();//control references and listening} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }        voidinit () {TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1); BT1=(Button) Findviewbyid (R.id.button1); BT2=(Button) Findviewbyid (R.id.button2); BT3=(Button) Findviewbyid (R.id.button3); BT4=(Button) Findviewbyid (R.ID.BUTTON4); Bt1.setonclicklistener ( This); Bt2.setonclicklistener ( This); Bt3.setonclicklistener ( This); Bt4.setonclicklistener ( This); }    voidInitsounds ()//Initialize Sound{MP=mediaplayer.create ( This, R.raw.futa);//Initialize MediaPlayersp=NewSoundpool (4,audiomanager.stream_music,100); SPM=NewHashmap<integer,integer>(); Spm.put (1,sp.load ( This, r.raw.ding,1)); }    voidPlaySoundintSoundintLoop//play a sound with Soundpool{Audiomanager Mgr= (Audiomanager) This. Getsystemservice (Context.audio_service); floatStreamvolumecurrent=Mgr.getstreamvolume (Audiomanager.stream_music); floatstreamvolumemax=Mgr.getstreammaxvolume (Audiomanager.stream_music); floatvolume=streamvolumecurrent/Streamvolumemax; Sp.play (Spm.get (sound), Volume,volume,1,LOOP,1F);//Play Sound} @Override Public voidOnClick (View v) {//Implementing individual Click events//TODO auto-generated Method Stub        Switch(V.getid ()) { CaseR.id.button1: {tv.settext ("Play sound with MediaPlayer"); if(!mp.isplaying ())//If no music is playing now{Mp.start ();}//Play Music             Break; }         CaseR.id.button2: {tv.settext ("Pause MediaPlayer Play sound"); if(Mp.isplaying ())//If the music is not playing now{Mp.pause ();}//Stop Music             Break; }         CaseR.id.button3: {tv.settext ("Play sound with Soundpool");  This. PlaySound (1, 0);//Play Music             Break; }         Caser.id.button4: {tv.settext ("Pause Soundpool Play sound"); Sp.pause (1);//Pause Music             Break; }        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.