我正在开发一个消防应用程序,它可以让我从列表视图中选择不同的音调,如果你愿意,可以将它们添加到队列中,然后按照按下播放按钮时选择的顺序播放它们。我看过一些关于使用mediaplayer数组的东西,但是我不确定如何将声音文件或引用in添加到数组中,以便它们可以从索引0中的第一个选定声音开始播放到最后一个索引中的最后一个声音。任何帮助都是非常感谢的。
发布于 2011-06-22 04:31:26
像这样的东西?
//
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;
}
}然后,在您的活动中需要做的就是启动player类,并在需要播放声音时调用playShortResource。您的资源应该位于res/raw目录中。
// 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();在您的示例中,将它们添加到数组中,然后循环播放
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 ) https://stackoverflow.com/questions/6431479
复制相似问题