Reputation: 1267
I am developing an app where i want to play two mp3 files simultaneously one as background music and want to control the sound of each player separately. The file size is 5 mb each
i have done with main audio file but when i try to play second file with it it throws error
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);
}
}
IS it possible to play both the files at same time and control the volume of each separately?
Upvotes: 3
Views: 3838
Reputation: 230
All the comments I have read suggest that SoundPool is not applicable to playing long sounds. You have to get a bit suspicious of OS quality when every one gets an error message with the exact same parameters, else it is an amazing coincidence.
Upvotes: 6