-
Java Sound Engine has no synchronization?Play multiple audio streams
Hello, I am writing an audio mixer software in java. So far, the program checksfor avaiable mixers and selects the Java Sound Engine. Then it can load an audio file of the hard disk and play it back. Now the problem is I have changed that to be able to load more than one file. According to the Java Sound API, I can make a call to the mixer "synchronize(Line[]ılines, booleanımaintainSync)" and that will take care of playing all the audio files at the same time. Unfortunately none of the mixers available support synchronization when checking for it with "isSynchronizationSupported(Line[]ılines, booleanımaintainSync)"!
So I need to find alternative ways to start and stop the playback of my audio streams. Bellow is the code of what I have tried. unfortunately that plays one audio file after the other and not all at the same time. Any ideas on how to fix this???
Code:
public static void playAudioMix()
{
Runnable playAudioMixRunner = new Runnable()
{
int bufferSize = (int) monoFormat.getSampleRate() * monoFormat.getFrameSize();
byte buffer[] = new byte[bufferSize];
int numberOfBytesRead;
public void run()
{
isStopped = false;
isPaused = false;
for(int i = 0; i < trackCounter; i++)
{
try
{
sourceLine[i].start();
while((numberOfBytesRead = audioInputStream[i].read(buffer, 0, buffer.length)) != -1 && !isStopped && !isPaused)
{
if(numberOfBytesRead > 0)
{
sourceLine[i].write(buffer, 0, numberOfBytesRead);
}
}
if(!isPaused)
{
stopAudioMix();
}
}
catch(IOException e)
{
System.err.println("I/O problems: " + e);
}
}
}
};
Thread playThread = new Thread(playAudioMixRunner);
playThread.start();
}
THANX In advance
-
Ok...I managed to find a solution. There does not seem to be any information on that net for playing several audio streams simultaneously, so I am posting my solution for anybody else interested. keep in mind this works, but the audio can get choppy as the number of audio streams being played increases. I am comming up with a new way, which should be better, as it will use a thread for each audio stream as opposed to a thread for all tghe streams. Till then, here is a temporary solution.
Code:
public static void playAudioMix()
{
Runnable playAudioMixRunner = new Runnable()
{
int bufferSize = (int) monoFormat.getSampleRate() * monoFormat.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run()
{
isStopped = false;
isPaused = false;
for(int i = 0; i < trackCounter; i++)
{
sourceLine[i].start();
}
int bytesRead = 0;
while(bytesRead != -1 && !isStopped && !isPaused)
{
for(int i = 0; i < trackCounter; i++)
{
try
{
bytesRead = audioInputStream[i].read(buffer, 0, buffer.length);
}
catch(IOException ex)
{
System.err.println("Error with reading audioInputStream" + ex);
}
if(bytesRead >= 0)
{
int bytesWritten = sourceLine[i].write(buffer, 0, bytesRead);
}
}
}
}
};
Thread playThread = new Thread(playAudioMixRunner);
playThread.start();
}
A. Digenis
-
Thanks for posting you're solution.
ArchAngel.
O:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|