Hello everybody,
I am having a great problem in my class project. I have to play multiple files in different output ports of the sound card same time. Explanation: Suppose i hav 1 speaker and 1 headphone connected to my pc. Now i hav to play 2 files in such a way that 1 song is played in speaker and other in headphone and the songs will be played in such a way that 1 user can hear only from speaker or headphone only 1 specific file.
I used javax.sound.sampled as my base programming. I could play multiple files in same time but i don't know how to play the songs in different ports of my sound card.
If any one can help me please do so.
Thanks
--------SHIMUL

Recommended Answers

All 5 Replies

Something tells me you cant.

I don't believe you can, sound cards don't use different ports for speakers and headphones. Plug in headphones and I'm pretty sure the speakers get shut off.

You are right, its either speakers or headphones, not both at the same time.

But i was thinking. Assuming you have a multi channel sound card (5.1 or such) wouldn't it be possible to write one music for the front two channels and another for the rear 2 chanels? You could plug your speakers/headphones into these ports respectively to get the desired results.

Now im not saying i know how to write this, im just stating in theory.

sounds ok, in theory. I have a 5.1 and 7.1 audio cards, they both have either the 1-digital out that connects to all the speakers at once through another system, or uses 1-plug for front and 1 for rear channels. So in that sense, I guess you could.

You'd have to write to 2 separate SourceDataLine's. I can see in the API you have the option of different ports, but the list looks like generic ones that are on almost every audio cards. There's got to be a method that retrieves a list of ports from the card that you can write out to. I've dealt a little with java audio, but not on this level.

Line out = AudioSystem.getLine(Port.Info.LINE_OUT);

I dunno, I'm drawing a blank at this point.

hello ,
i tried to get the different line from audio system. this is how i can play multiple files but still i cannot direct them to specific sound ports like head phone or speakers or front or rear.
the code is given below for yor understanding me.
BYE
shimul

import java.io.File;
import java.io.IOException;
import java.io.*;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Clip;
import javax.sound.sampled.*;



import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.media.control.*;



class playFile1  implements Runnable
{
int EXTERNAL_BUFFER_SIZE=176400;
File file1=new File("file1.wav");
AudioInputStream audioInputStream = null;


SourceDataLine line = null;


Thread t;
int nBytesRead = 0;
byte[]abData =  new byte[EXTERNAL_BUFFER_SIZE];


playFile1()
{
initialize();
t=new Thread(this,"playFile");
t.start();
}
public void run()
{
line.start();
while (nBytesRead>=0)
{


try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);


}
catch (IOException e)
{
e.printStackTrace();
}
if(nBytesRead>=0)
{


int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}



}
void initialize()
{
try
{
audioInputStream = AudioSystem.getAudioInputStream(file1);


}
catch (Exception e)
{


e.printStackTrace();
System.exit(1);
}
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);



try
{
//AudioSystem.get
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);


}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
}
}


class playFile2 implements Runnable
{


File file2=new File("file2.wav");
AudioInputStream audioInputStream2 = null;
SourceDataLine line2 = null;


Thread t;


int EXTERNAL_BUFFER_SIZE=176400;
int nBytesRead2=0;
byte[]abData2 =     new byte[EXTERNAL_BUFFER_SIZE];


playFile2()
{
initialize();
t=new Thread(this,"playFile2");
t.start();
}


void initialize()
{
try
{


audioInputStream2 = AudioSystem.getAudioInputStream(file2);
}
catch (Exception e)
{


e.printStackTrace();
System.exit(1);
}
AudioFormat audioFormat2 = audioInputStream2.getFormat();



DataLine.Info info2 = new DataLine.Info(SourceDataLine.class,audioFormat2);



try
{
//AudioSystem.get
//Line l=AudioSystem.getLine(Port.Info.SPEAKER);
//line2=(SourceDataLine)l;
//line2.
line2 = (SourceDataLine) AudioSystem.getLine(info2);
line2.open(audioFormat2);


}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}



}



public void run()
{
line2.start();
while(nBytesRead2>=0)
{
try
{
nBytesRead2 = audioInputStream2.read(abData2, 0, abData2.length);
}
catch (Exception e)
{
e.printStackTrace();
}
if(nBytesRead2>=0)
{
int nBytesWritten2 = line2.write(abData2, 0, nBytesRead2);
//System.out.println ("song 2 written actually "+nBytesWritten2);
}
}
}



}



public class testSnd
{
public static void main(String[] args)
{
playFile1 pf1=new playFile1();
playFile2 pf2=new playFile2();


}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.