1

I am trying to develop a Java application that is able to load and play multiple audio files with the same length, and play them simultaneously. Basically parts of a song like bass, guitar, etc..

That is a rather easy task to do, my problem is ensuring synchronicity during the playback. I've tried using javax.sound to accomplish that, but that library doesn't guarantee the synchronicity I'm looking for.

I tried something really simple like this:

public void playAll() {
    for (Clip clip : this.clips) {
        clip.start();
    }
}

Even though the .play() statement isn't executed in parallel, in some runs the audios are aligned just fine, you can't say that there is any delay. However, that doesn't happen all the time like it must be. It gets even worse when I programmatically seek forward every audio. They get totally out of sync:

public void seek(long newPositionInMicroseconds)
{
    for(Clip clip : this.clips)
    {
        clip.setMicrosecondPosition(newPositionInMicroseconds);
    }
}

I've tried looking around for different libraries that could do that. I found some libraries, however none of them seemed to guarantee syncronicity out of the box nor I could find an example implementation or well documented scenarios that could help me.

I did something similar a while back in C# and I used NAudio for that, which worked perfectly, but I couldn't find an alternative in Java.

Does anyone know of a library and/or an approach that can help me accomplish what I'm looking for?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.