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