Playing multiple songs with MediaPlayer at the same time: only one is really playing

前端 未结 5 1896
甜味超标
甜味超标 2021-01-02 04:44

I need help with playing multiple audio tracks at the same time in Android.

I am supposed to play three audio tracks at the exact same

5条回答
  •  天涯浪人
    2021-01-02 04:54

    Well, I believe I found what I could name "a temporary solution".

    MediaPlayer track1 = MediaPlayer.create(this, R.raw.track1);
    track1.start();
    MediaPlayer track2 = MediaPlayer.create(this, R.raw.track2);
    track2.start();
    MediaPlayer track3 = MediaPlayer.create(this, R.raw.track3);
    track3.start();
    

    The problem with this is that the create() method creates a noticeable gap when the songs are being played.

    So this is not it.

    After a while, I tried the following :

    MediaPlayer track1 = MediaPlayer.create(this, R.raw.track1);
    track1.start();
    track1.pause();
    MediaPlayer track2 = MediaPlayer.create(this, R.raw.track2);
    track2.start();
    track2.pause();
    MediaPlayer track3 = MediaPlayer.create(this, R.raw.track3);
    track3.start();
    track3.pause();
    
    // resuming...
    track1.start(); 
    track2.start();
    track3.start();
    

    That whay, the songs are more synchronous. I can still hear a very small gap but it is way better. I found this solution quite strange as well as ugly.

    Unless someone has another idea, I will stick with that solution.

提交回复
热议问题