我遇到了一個問題,我一直在努力爭取一個星期的大部分時間,並嘗試過至少10個不同的實現,並且都失敗了。必須有一些我不瞭解的東西。JMF使用RTP播放器實現
我正在使用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)
什麼挫折了我這麼多的是,這是一個簡單的測試案例和這些文件直接從實例改編。此外,他們儘可能簡單,但他們似乎仍然無法工作。
任何幫助將不勝感激! 謝謝!
你會很好地聽ControllerEvent,看看發生了什麼 – jogabonito