0

I am making a game in LibGDX and I need to play 2 melodies at the same time. However on Android loading and playing 2 melodies does not work. Here is a small test I did. I even tried using the LibGDX AssetManager but still no success.

Music test1;
Music test2;

AssetManager manager = new AssetManager();

@Override public void create()
{
    manager.load(Melody.REVERSION.getPath(), Music.class);
    manager.load(Melody.COLLAPSE.getPath(), Music.class);

    while (!manager.update()) {}
    manager.finishLoading();

    test1 = manager.get(Melody.REVERSION.getPath());
    test2 = manager.get(Melody.COLLAPSE.getPath());

    test1.setVolume(1);
    test1.play();
    test2.setVolume(1);
    test2.play();
}
BananyaDev
  • 583
  • 5
  • 13
  • Possibly related: http://gamedev.stackexchange.com/questions/104893/sounds-overlapping-when-playing-too-many-at-the-same-time and http://stackoverflow.com/questions/15742812/android-gdx-sound-performance-issue – Morrison Chang Feb 13 '17 at 21:20

1 Answers1

0

I'm currently using SoundPool library. With this I can reproduce different sound effects simultaneously but I do not know if it's the best method for music.

Post related: Play sound using soundpool example

EDIT Using MediaPlayer and CyclicBarrier:

public enum MP_COMMAND {
    START,
    STOP,
    PAUSE
}

/**
 * Uses threads to execute synced commands for the current video media player and 
 * background music player in tandem.
 */
public void syncedCommand(MediaPlayer player1, MediaPlayer player2, MP_COMMAND command) {
    final CyclicBarrier commandBarrier = new CyclicBarrier(2);
    new Thread(new SyncedCommandService(commandBarrier, player1, command)).start();
    new Thread(new SyncedCommandService(commandBarrier, player2, command)).start();
}

/**
 * Inner class that starts a given media player synchronously
 * with other threads utilizing SyncedStartService
 */
private class SyncedCommandService implements Runnable {
    private final CyclicBarrier              mCommandBarrier;
    private       MediaPlayerTest.MP_COMMAND mCommand;
    private       MediaPlayer                mMediaPlayer;

    public SyncedCommandService(CyclicBarrier barrier, MediaPlayer player, MediaPlayerTest.MP_COMMAND command) {
        mCommandBarrier = barrier;
        mMediaPlayer = player;
        mCommand = command;
    }

    @Override public void run() {
        try {
            mCommandBarrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }

        switch (mCommand) {
            case START:
                mMediaPlayer.start();
                break;

            case STOP:
                mMediaPlayer.stop();
                break;

            case PAUSE:
                mMediaPlayer.pause();
                break;

            default:
                break;
        }
    }
}

To play both songs call this:

MediaPlayer mCurrentVideoPlayer = MediaPlayer.create(this, R.raw.track1);
MediaPlayer mBackgroundMusic = MediaPlayer.create(this, R.raw.track2);

syncedCommand(mCurrentVideoPlayer, mBackgroundMusic, MP_COMMAND.START);

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

Community
  • 1
  • 1
Santi
  • 1
  • 3
  • Sound and Music are different in LibGDX. Sound is fully loaded into memory and there can be any number of instances of it playing. However, Sound only works for files below 1 MB. You generally use Sound for sound effects, like a gun shot for example, I however need to use Music. Music works by streaming the contents of the music file. It uses MediaPlayer on the Android backend. Music is used for longer songs that need to play. I need to run 2 songs simultaneously, not 2 sounds. – BananyaDev Feb 13 '17 at 22:15
  • Ahh ok, you can try using MediaPlayer, I read this: http://stackoverflow.com/questions/26379441/playing-multiple-songs-with-mediaplayer-at-the-same-time-only-one-is-really-pla maybe can help you. – Santi Feb 13 '17 at 22:35
  • If you perform MP_COMMAND.START and quickly perform a MP_COMMAND.PAUSE, how do you cancel the last action if it did not finished all threads? – Billyjoker Dec 18 '17 at 11:19