0
我正在開發一個消防應用程序,它允許我從列表視圖中選擇不同的音調,如果您願意,可以將它們添加到隊列中,然後按照它們在選擇時的順序播放它們播放按鈕被按下。我已經看到有關使用mediaplayer數組的一些信息,但不知道如何才能將聲音文件或參考ID添加到數組,以便它們可以從索引0中的第一個選定聲音開始播放,直到最後一個聲音最後的指數。任何幫助表示讚賞。Android按順序播放聲音
我正在開發一個消防應用程序,它允許我從列表視圖中選擇不同的音調,如果您願意,可以將它們添加到隊列中,然後按照它們在選擇時的順序播放它們播放按鈕被按下。我已經看到有關使用mediaplayer數組的一些信息,但不知道如何才能將聲音文件或參考ID添加到數組,以便它們可以從索引0中的第一個選定聲音開始播放,直到最後一個聲音最後的指數。任何幫助表示讚賞。Android按順序播放聲音
像這樣的東西?
//
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)
我將不得不嘗試看看。但要更清楚一點,說我有兩個按鈕;如果按下按鈕1,聲音1將添加到數組或其他東西。然後,如果按下按鈕2,則會添加聲音2。當播放按鈕被按下時,它將循環播放聲音1,然後聲音2,然後停止。 – Heavy5Rescue
感謝您的幫助!我創建了兩個類,一個用於主要活動,另一個用於cxmediaplayer。那是對的嗎?如果是這樣,我無法通過主活動 – Heavy5Rescue
中的onclicklistener事件調用或播放聲音,我可以使其工作。我去了一個數組列表,並在每個listview點擊事件中添加了聲音文件。然後用一個循環遍歷數組並播放從索引0開始的聲音。 – Heavy5Rescue