Android воспроизводит звуки в последовательном порядке - PullRequest
0 голосов
/ 22 июня 2011

Я работаю над приложением пожаротушения, которое позволило бы мне выбирать разные тоны из списка, добавлять их в очередь, если хотите, а затем воспроизводить их в том порядке, в котором они были выбраны при нажатии кнопки воспроизведения.Я видел кое-что об использовании массива медиаплеера, но не уверен, как мне добавить звуковые файлы или ссылочные идентификаторы в массив, чтобы их можно было воспроизводить, начиная с первого выбранного звука в индексе 0 до последнего впоследний индекс.Любая помощь приветствуется.

1 Ответ

1 голос
/ 22 июня 2011

Как то так?

//
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;
}
}

Тогда все, что вам нужно в вашей деятельности, это инициировать класс игрока и вызвать 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 ) 
...