我正在开发一个记忆游戏,在这个游戏中,我想在开始游戏时播放背景音乐。
我使用了声音池类,并成功地执行了它,但我在播放时遇到了问题。我添加了两分钟的.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 .in 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);
}
});发布于 2011-12-20 16:45:02
Soundpool不是用于播放背景音乐的。它有许多限制,如文件大小,加载和播放时间。它主要用于音效。使用MediaPlayer类播放背景音乐。
https://stackoverflow.com/questions/8572707
复制相似问题