我遇到的问题是我在一周的大部分时间里一直在努力,并尝试了至少10种不同的实现,但似乎都失败了。必须有一些我不理解的东西。
我正在使用jmf通过rtp传输音频。问题在于客户端玩家永远不会意识到代码块,因此也没有任何东西可以玩。
发射器的代码如下:
import java.io.File;
import java.io.IOException;
import javax.media.DataSink;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoProcessorException;
import javax.media.NotRealizedError;
import javax.media.Processor;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
public class RTPTransmitter
{
/**
* @param args
*/
public static void main(String[] args)
{
File f = new File("streamtest.wav");
Format format;
format = new AudioFormat(AudioFormat.ULAW_RTP, 8000, 8, 1);
Processor processor = null;
try
{
processor = Manager.createProcessor(f.toURI().toURL());
} catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
} catch (NoProcessorException e)
{
e.printStackTrace();
System.exit(-1);
}
// configure the processor
processor.configure();
while (processor.getState() != Processor.Configured)
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
processor.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP));
TrackControl track[] = processor.getTrackControls();
boolean encodingOk = false;
// Go through the tracks and try to program one of them to
// output gsm data.
for (int i = 0; i < track.length; i++)
{
if (!encodingOk && track[i] instanceof FormatControl)
{
if (((FormatControl) track[i]).setFormat(format) == null)
{
track[i].setEnabled(false);
} else
{
encodingOk = true;
}
} else
{
// we could not set this track to gsm, so disable it
track[i].setEnabled(false);
}
}
//realize the processor
processor.realize();
while(processor.getState() != processor.Realized){
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// At this point, we have determined where we can send out
// gsm data or not.
if (encodingOk)
{
// get the output datasource of the processor and exit
// if we fail
DataSource ds = null;
try
{
ds = processor.getDataOutput();
} catch (NotRealizedError e)
{
e.printStackTrace();
System.exit(-1);
}
// hand this datasource to manager for creating an RTP
// datasink our RTP datasink will multicast the audio
try
{
String url = "rtp://127.0.0.1:8000/audio/1";
MediaLocator m = new MediaLocator(url);
DataSink d = Manager.createDataSink(ds, m);
d.open();
d.start();
System.out.println("Starting processor");
processor.start();
System.out.println("Processor Started");
Thread.sleep(30000);
} catch (Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
}
}
接收器的代码是:
import java.io.IOException;
import java.net.MalformedURLException;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
public class RTPReceiver
{
/**
* @param args
*/
public static void main(String[] args)
{
String url = "rtp://127.0.0.1:8000/audio/1";
MediaLocator mrl = new MediaLocator(url);
// Create a player for this rtp session
Player player = null;
try
{
player = Manager.createPlayer(mrl);
} catch (NoPlayerException e)
{
e.printStackTrace();
System.exit(-1);
} catch (MalformedURLException e)
{
e.printStackTrace();
System.exit(-1);
} catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
if (player != null)
{
System.out.println("Player created.");
player.realize();
// wait for realizing
while (player.getState() != Player.Realized)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Starting player");
player.start();
} else
{
System.err.println("Player won't create.");
System.exit(-1);
}
System.out.println("Exiting.");
}
}
变送器启动正常,一切都已启动,一切似乎都有效。所以我启动接收器,它只是循环
while (player.getState() != Player.Realized)
令我非常沮丧的是,这是一个简单的测试用例,这些文件可以直接从示例中进行调整。此外,它们尽可能简单,但它们似乎仍无法正常工作。
任何帮助将不胜感激! 谢谢你!
答案 0 :(得分:4)
public AudioRTPRecv() {
MediaLocator mrl= new MediaLocator("rtp://192.168.1.100:49151/audio/1");
// Create a player for this rtp session
try {
player = Manager.createPlayer(mrl);
} catch (Exception e) {
System.err.println("Error:" + e);
return;
}
if (player != null) {
player.addControllerListener(this);
player.realize();
}
}
public synchronized void controllerUpdate(ControllerEvent ce) {
System.out.println(ce);
if(ce instanceof TransitionEvent) {
if (((TransitionEvent)ce).getCurrentState() == Processor.Realized) {
player.start();
System.out.println("starting player now");
}
}
}
这样做的另一个好处是,controllerUpdate方法中的print语句可以让您了解玩家的最新动态。
答案 1 :(得分:3)
我也常常遇到同样的问题。它坚持“实现”状态。你的程序只需要一点改变。你知道在阅读解决方案后你会敲打你的桌子:D
即,键入您的IP地址,而不是键入“127.0.0.1”。多数民众赞成!
然后运行它。