3
我正在开发一个应用程序,我想同时播放两个mp3文件作为背景音乐,并希望分别控制每个播放器的声音。文件大小为5 MB每个AudioCache堆大小溢出问题请求大小:1053184,最大大小:1048576
我有主音频文件来完成,但是当我尝试播放第二个文件,它会抛出错误
SoundManager mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1,R.raw.music);
mSoundManager.addSound(2,R.raw.mentalafslapning);
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
class SoundManager
{
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
private Vector<Integer> mAvailibleSounds = new Vector<Integer>();
private Vector<Integer> mKillSoundQueue = new Vector<Integer>();
private Handler mHandler = new Handler();
public SoundManager(){}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index, int SoundID)
{
mAvailibleSounds.add(Index);
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
// dont have a sound for this obj, return.
if(mAvailibleSounds.contains(index)){
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int soundId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
mKillSoundQueue.add(soundId);
// schedule the current sound to stop after set milliseconds
mHandler.postDelayed(new Runnable() {
public void run() {
if(!mKillSoundQueue.isEmpty()){
mSoundPool.stop(mKillSoundQueue.firstElement());
}
}
}, 3000);
}
}
,才有可能发挥无论是在同一时间的文件和分别控制每个音量?
由于Dalvik堆大小上的这种荒谬的任意限制,播放很多短小的声音甚至都没用。这就像他们不希望人们为Android开发游戏。 – 2012-06-28 09:58:48
我一直在Android上开发多年,现在我可以放心地说它是一个非常糟糕的平台。 – 2015-07-24 14:53:54