ボタンのグループに割り当てられたたくさんのサウンドがあり、それらを再生する必要があります。私のサウンドはすべてアセットフォルダにあります。ただし、動作しません。目的は 、assetFodlerからロードし、そのサウンドを再生することです。プロジェクトのコード例を作成します。
//set up audio player
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//getting files lists from asset folder
aMan = this.getAssets();
try {
filelist = aMan.list("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
コード行をあまり持たないようにするために、サウンドをロードして再生するための基本的な手順を作成しました。
public void loadSound (String strSound, int stream) {
try {
stream= mSoundPool.load(aMan.openFd(strSound), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}
ご覧のとおり、file(stringName)とstreamIDを渡します。
最後に、これが私がそれをどのように使うかです:
case R.id.button1:
//if button was clicked two or more times, when play is still on im doing stop
mSoundPool.stop(mStream1);
loadSound(filelist[0],mStream1);
break;
プロジェクトを実行しても何も起こらず、logcatは次のように言います。
12-09 10:38:34.851: W/SoundPool(17331): sample 2 not READY
どんな助けでもいただければ幸いです。
UPD1:このようにすると、loadSoundプロシージャがなくても正常に機能し、次のコードがonCreateになります。
//load fx
try {
mSoundPoolMap.put(RAW_1_1, mSoundPool.load(aMan.openFd(filelist[0]), 1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
およびOnclickボタン:
//resourcePlayer.stop();
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
コード行をあまり多くしたくないので、見栄えを良くしたかった