アプリにさまざまなサウンドをロードする方法を見つけようとしています。以下に示すように、サウンドマネージャーはスムーズに動作しています。
package com.beeseries.contextclues;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int index, int SoundID)
{
mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
}
public void loadWritingSounds(){
mSoundPoolMap.put(0, mSoundPool.load(mContext, R.raw.writing1, 1));
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.writing2, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.writing3, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.writing4, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.writing5, 1));
}
public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
アプリのオープニング アクティビティには、サウンドを機能させるための次のコード スニペットが含まれています。
public SoundManager mSoundManager;
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.loadWritingSounds();
そして、ランダムなサウンドを再生する次のコード (これは動作しますが、オープニング アクティビティでのみ):
int b = randSound1.nextInt((whichSound.length));
mSoundManager.playSound(b);
したがって、私のジレンマは、一度にすべてのアクティビティの WritingSounds をロードできるようにする必要があるということです。現在、別のアクティビティでサウンドを再生しようとすると、コードのスニペットを追加して、サウンドマネージャーの仕事。
私は何をしますか!?