我正在開發一款記憶遊戲,其中我想在開始遊戲時播放背景音樂。在整個遊戲過程中播放背景音樂
我使用了聲音池類併成功執行了它,但我在播放時遇到問題。我添加了兩分鐘的.mp3
音頻文件,但它只播放了15秒多。
任何人都可以說我有什麼問題嗎?這是我的soundclass文件:
public class Soundclass {
private SoundPool soundpool;
private HashMap<Integer, Integer> soundpoolmap;
private AudioManager audiomanager;
private Context context;
public void initSounds(Context thecontext) {
context = thecontext;
soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundpoolmap = new HashMap<Integer, Integer>();
audiomanager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
}
public Soundclass() {
}
public void addSound(int Index, int soundID) {
soundpoolmap.put(Index, soundpool.load(context, soundID, 1));
}
public void playSound(int Index) {
float streamVolume = audiomanager
.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume
/audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1,
0, 1f);
}
public void playLoopedSound(int Index) {
float streamVolume = audiomanager
.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume
/audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1,
-1, 1f);
}
}
,我用這個類給出activitycalss。在OnCreate中功能
Button btn = (Button) findViewById(R.id.sound);
soundclass = new Soundclass();
soundclass.initSounds(getBaseContext());
soundclass.addSound(1, R.raw.jinglebells);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundclass.playSound(1);
}
});
你是什麼意思「2開發記憶遊戲」?你爲什麼使用前綴2? – Lion 2011-12-20 08:45:34
編輯您的問題 – mH16 2011-12-20 08:49:37
@Lion現在看到(我盡我最大努力使問題可讀) – 2011-12-20 09:04:00