3

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?

skolima
  • 31,963
  • 27
  • 115
  • 151
Mahesh
  • 1,257
  • 1
  • 14
  • 24

1 Answers1

6

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.

Squiggles
  • 230
  • 5
  • 16
  • 3
    It's not even useful for playing lots of short sounds, because of this ridiculous arbitrary limit on the Dalvik heap size. It's like they don't WANT people to develop games for Android. – James M Jun 28 '12 at 09:58
  • I've been developing on Android for many years now, I can safely say it's a very badly designed platform. – Oliver Dixon Jul 24 '15 at 14:53