开发者

Android play sounds in sequential order

I'm working on a firefighting app that would allow me to select different tones from a listview, add them to a queue if you will, and then play them back in th开发者_如何学Goe order they were selected when the play button is pressed. I've seen something about using a mediaplayer array, but not sure how would I go about adding a sound files or reference IDs to an array so that they can be played back starting with the first selected sound in index 0 to the last one in the last index. Any help is appreciated.


Something like this ?

//
import android.media.AudioManager;
import android.media.SoundPool;
import android.app.Activity;
//
import java.util.HashMap;
//
import us.kristjansson.android.R;

public class CxMediaPlayer
{
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();

// Constructor
public CxMediaPlayer( Activity pContext )
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

// 0-9 Buttons
mSounds.put( R.raw.button_1, this.mShortPlayer.load(pContext, R.raw.button_1, 1) );
mSounds.put( R.raw.button_2, this.mShortPlayer.load(pContext, R.raw.button_2, 1) );
mSounds.put( R.raw.button_3, this.mShortPlayer.load(pContext, R.raw.button_3, 1) );
mSounds.put( R.raw.button_4, this.mShortPlayer.load(pContext, R.raw.button_4, 1) );
mSounds.put( R.raw.button_5, this.mShortPlayer.load(pContext, R.raw.button_5, 1) );
mSounds.put( R.raw.button_6, this.mShortPlayer.load(pContext, R.raw.button_6, 1) );
mSounds.put( R.raw.button_7, this.mShortPlayer.load(pContext, R.raw.button_7, 1) );

// Others
mSounds.put( R.raw.delete_5, this.mShortPlayer.load(pContext, R.raw.correct_answer, 1) );
mSounds.put( R.raw.delete_5, this.mShortPlayer.load(pContext, R.raw.wrong_answer, 1) );
}

// Plays the passed preloaded resource
public void playShortResource( int piResource )
{
int iSoundId = mSounds.get( piResource );
this.mShortPlayer.play( iSoundId, 0.99f, 0.99f, 0, 0, 1 );
}

// Cleanup
public void Release()
{
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}

Then all you need in your Activity is to initiate the player class and call playShortResource when you need a sound played. Your resources should be available in the res/raw directory.

// The media player – OnCreate
mxMediaPlayer = new CxMediaPlayer( this );
// Play the desired sound – OnClick
mxMediaPlayer.playShortResource(  R.raw.button_1 );
// Make sure to release resources when done – OnDestroy
mxMediaPlayer.Release();

and in your case add them to array, and play in a loop

toPlay.Add( R.raw.button_1 ) 
toPlay.Add( R.raw.button_3 ) 
toPlay.Add( R.raw.button_7 ); 

Foreach( item in toPlay list ) 
  mxMediaPlayer.playShortResource( item ) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜