The soundpool class in Android is a class provided in the Android. Media package for playing sound files. It supports playing multiple sound files at the same time and controls the number of cycles of each file. Follow these steps:
1. Implement the onloadcomplete function of the soundpool. onloadcompletelistener Interface
2. New soundpool instance sndpool. The first parameter of the constructor specifies the maximum number of files that can be played simultaneously.
3. Set the onloadcompletelistener callback function of the sndpool.
4 sndpool. Load an audio file, also known as a stream
5. In the onloadcomplete function, determine whether the stream is successful or not. Generally, a message is sent to the activity to call the sndpool. Play stream.
6. Repeat 4 and 5. You know that you have played all the streams you want to play.
Possible problems:
1. The audio file cannot be too large. Otherwise, the heap size overflow of audiocache will be generated.
2. Be sure to play after receiving the onloadcomplete callback
3. Call sndpool. Release () in ondestroy of the program. Otherwise, the program will be opened for the second time without sound.
4 soundpool. onloadcompletelistener. Onloadcomplete (soundpool
Soundpool, int sampleid, int status) Sampleid is the same as the parameter returned by soundpool. Load.
5. The following code also encountered a problem. At the beginning, 100 was not added after the pause ID and so on. The error always occurred. It should be in conflict with the definition in Android.
Package com. Android. testsoundpool;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. message;
Import Android. View. view;
Import Android. widget. textview;
Import Android. widget. Button;
Import Android. util. log;
Import Android. Media. audiomanager;
Import Android. Media. soundpool;
Import Android. OS. Handler;
Public class testsoundpoolactivity extends activity implements view. onclicklistener,
Soundpool. onloadcompletelistener {
Static string tag = "testsoundpoolactivity ";
Textview TV;
Soundpool sndpool;
Int sndid;
Int [] streamid = new int [10];
Int streamnum = 0;
Int streamnumpause = 0;
Int [] rid = new int [] {R. raw. iremembershort1, R. raw. in_call_alarm, R. raw. down, R. raw. down, R. raw. down, R. raw. down,
R. Raw. Down, R. Raw. Down };
Private Static final int sound_load_ OK = 1;
Private Final handler mhandler = new myhandler ();
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
TV = (textview) findviewbyid (R. Id. textvbb100 );
TV. settext ("1234567 ");
Button b1 = (button) findviewbyid (R. Id. play100 );
B1.setonclicklistener (this );
B1 = (button) findviewbyid (R. Id. more100 );
B1.setonclicklistener (this );
B1 = (button) findviewbyid (R. Id. pause100 );
B1.setonclicklistener (this );
Sndpool = new soundpool (16, audiomanager. stream_music, 0 );
Sndpool. setonloadcompletelistener (this );
}
Public void ondestroy ()
{
Sndpool. Release ();
Super. ondestroy ();
}
Private class myhandler extends handler {
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case sound_load_ OK:
Streamid [streamnum] = sndpool. Play (msg. arg1, (float) 0.8, (float) 0.8, 16, 10, (float) 1.0 );
Streamnum ++;
Break;
}
}
}
Public void onloadcomplete (soundpool, int sampleid, int status ){
Message MSG = mhandler. obtainmessage (sound_load_ OK );;
MSG. arg1 = sampleid;
Mhandler. sendmessage (MSG );
}
Public void onclick (view v ){
Int id = V. GETID ();
Switch (ID ){
Case R. Id. play100:
TV. settext ("play ");
Log. V (TAG, "Play main ");
If (sndpool! = NULL)
Sndid = sndpool. Load (this, RID [streamnum], 1 );
Break;
Case R. Id. more100:
TV. settext ("more ");
Sndpool. Load (this, RID [streamnum], 1 );
Log. V (TAG, "more ");
Break;
Case R. Id. pause100:
TV. settext ("pause ");
If (streamnumpause <streamnum ){
Sndpool. Pause (streamid [streamnumpause]);
Streamnumpause ++;
}
Log. V (TAG, "pause ");
Break;
}
}
}
You can download the entire code at http://download.csdn.net/source/3497819.