?? audiopanel.java~2~
字號:
package audiopanel;import java.awt.*;import javax.media.bean.playerbean.*;import javax.swing.*;import java.awt.event.*;import com.ibm.media.bean.multiplayer.*;import com.sun.media.jsdt.*;import java.lang.Object;import java.awt.Component;import java.lang.String;import java.awt.event.*;import java.applet.Applet;import javax.media.bean.playerbean.*;import javax.media.protocol.*;import javax.media.protocol.DataSource;import javax.media.format.*;import javax.media.control.TrackControl;import javax.media.control.QualityControl;import javax.media.*;import java.io.*;import java.util.*;import java.util.Vector;import javax.media.Time;import javax.media.MediaLocator;import javax.media.protocol.*;import java.io.IOException;import java.awt.Dimension;import com.sun.media.ui.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: 童天網絡</p> * @author unascribed * @version 1.0 */public class audioPanel extends JPanel implements ChatDebugFlags ,BufferTransferHandler{ String hostname = "192.168.0.6"; String name; int hostport = 6677; String sessionType = "socket"; boolean isteacher=true; private Session session; public ChatClient client; public Channel channel; private ChatConsumer chatConsumer; private boolean connected=false; dataSource dsource=new dataSource(); protected PushBufferStream []stream; boolean initflag=true;//定義createProcessor用到的變量 final protected AudioFormat audioFormat= new AudioFormat(AudioFormat.LINEAR,44100,16,1,AudioFormat.BIG_ENDIAN,AudioFormat.SIGNED);// final AudioFormat audioFormat= new AudioFormat(AudioFormat.G723_RTP,8000,8,1,AudioFormat.LITTLE_ENDIAN,AudioFormat.SIGNED); int bufferlength=4410; Vector bufdata=new Vector(); Processor processor; DataSource dataOutput; MediaLocator locator=new MediaLocator("dsound://"); MediaPlayer mp=new MediaPlayer();//結束定義createProcessor中的變量//定義custome datasource and stream 中的變量 int seqnum=0; public PushBufferDataSource pds=null; public PushBufferStream pbs[]= null;//結束 public audioPanel(){ } public audioPanel(boolean isteacher,String logname,String hostip,int port) { try { name=logname; hostname=hostip; hostport=port; this.isteacher=isteacher; jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } void jbInit() throws Exception { this.setLayout(borderLayout1); connect(); dsource.getStreams(); dsource.connect(); mp.setDataSource(dsource);// mp.setSize(100,20); mp.setVisible(true); System.out.println("Width:"+mp.getSize().getWidth()+"Height:"+mp.getSize().getHeight());// mp.start(); if(isteacher) startSpeak(); mp.start(); this.add(mp, BorderLayout.CENTER); } public void processData(Data data){ try{// System.out.println("接受到數據");// 以數組形式接受到參數 Vector datavector=new Vector(); datavector=(Vector)data.getDataAsObject(); byte[] bbyte; bbyte=(byte[])datavector.elementAt(0); String ss=(String)datavector.elementAt(1); bufdata.addElement(bbyte);// System.out.println("bufdata里的元素+1:"+bufdata.size());/* if(initflag){ System.out.println("執行初始化custom datasource工作"); try{ dsource.getStreams(); System.out.println("stream number:"+stream.length); dsource.connect();// dsource.start(); mp.setDataSource(dsource); mp.start(); initflag=false; }catch(Exception ee){ System.out.println("初始化dataSource出錯"); ee.printStackTrace(); } }*/ }catch(Exception e){ System.out.println("dataReceived出錯:"); e.printStackTrace(); } } private void connect() { System.out.println("用戶名字:"+name); String sessionType="socket"; String sessionName = "ChatSession"; boolean sessionExists = false; URLString url = null; if (ChatUser_Debug) { System.out.println("ChatUser: connect."); } if (name == null) { throw new Error("need a non-null name to connect"); } if (connected) { return; } try { try { url = URLString.createSessionURL(hostname, hostport, sessionType, sessionName); System.out.println("audioPanel: connect: checking: url: " + url); while (!sessionExists) { try { if (SessionFactory.sessionExists(url)) { System.out.println("ChatUser: connect:" + " found Session."); sessionExists = true; } } catch (NoRegistryException nre) { System.out.println("ChatUser: connect:" + " no registry: sleeping."); Thread.sleep(1000); } catch (ConnectionException ce) { System.out.println("ChatUser: connect:" + " connection exception: sleeping."); Thread.sleep(3000); } }/* Create a chat client. */ System.out.println("Creating a ChatMember..."); client = new ChatClient(name);/* Resolve the chat session. */ session = SessionFactory.createSession(client, url, true); channel = session.createChannel(client, "ChatChannel", true, true, true); chatConsumer = new ChatConsumer(client.getName(),this); //System.out.println("dkjf"); channel.addConsumer(client, chatConsumer);// System.out.println("come here"); connected = true;// nameField.setEditable(false);// setNameLabel.setEnabled(false);// signOffButton.setEnabled(true);// sayLabel.setEnabled(true); repaint(); } catch (Exception e) { System.out.print("Caught exception in "); System.out.println("ChatUser.connect: " + e); if (ChatUser_Debug) { e.printStackTrace(); } } } catch (Throwable th) { System.out.println("ChatUser: connect caught: " + th); if (ChatUser_Debug) { th.printStackTrace(); } throw new Error("ChatUser.connect failed : " + th); } } public void destroy() { if (ChatUser_Debug) { System.out.println("ChatUser: destroy."); } disconnect(); } void disconnect() { if (ChatUser_Debug) { System.out.println("ChatUser: disconnect."); } if (connected == false) { return; } try { session.close(true); } catch (Exception e) { System.out.println("Caught exception while trying to " + "disconnect from chat server: " + e); if (ChatUser_Debug) { e.printStackTrace(); } } connected = false; } public void startTransferData(){ pds = (PushBufferDataSource)dataOutput; pbs=pds.getStreams();// mp.start(); for(int i=0; i<pbs.length; i++){ System.out.println("((PushBufferStream[])(pds.getStreams()))[i]="+((PushBufferStream[])(pds.getStreams()))[i]); if( ((PushBufferStream[])pbs)[i].getFormat() instanceof AudioFormat ){ pbs[i].setTransferHandler(this); break; } } try{ pds.start(); }catch(Exception ee){ System.out.println(ee); } } public void startSpeak(){ String strerror=createProcessor(); System.out.println(strerror); startTransferData(); } public void stopSpeak(){ try { //int ccount = 0;// mp.stop(); pds.stop();// processor.stop(); } catch (Exception ab){ System.out.println(ab); } } public void exitSpeak(){ try{ mp.stop(); mp.deallocate(); mp=null; pds.stop(); pds.disconnect(); processor.stop(); processor.deallocate(); processor.close(); processor=null; }catch(Exception e){ System.out.println(e); } } private String createProcessor() { if (locator == null) return "Locator is null"; DataSource ds; try { ds = javax.media.Manager.createDataSource(locator); } catch (Exception e) { System.out.println(e+e.getMessage()); return "Couldn't create DataSource"; } // Try to create a processor to handle the input media locator try { processor = javax.media.Manager.createProcessor(ds);// processor = javax.media.Manager.createProcessor(ds); } catch (NoProcessorException npe) { System.out.println(npe+npe.getMessage()); return "Couldn't create processor"; } catch (IOException ioe) { System.out.println(ioe+ioe.getMessage()); return "IOException creating processor"; } // Wait for it to configure boolean result = waitForState(processor, Processor.Configured); if (result == false) return "Couldn't configure processor"; // Get the tracks from the processor TrackControl [] tracks = processor.getTrackControls(); // Do we have atleast one track? if (tracks == null || tracks.length < 1) return "Couldn't find tracks in processor"; // Set the output content descriptor to RAW_RTP // This will limit the supported formats reported from // Track.getSupportedFormats to only valid RTP formats. ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW); processor.setContentDescriptor(cd); Format supported[]; Format chosen; boolean atLeastOneTrack = false; // Program the tracks. for (int i = 0; i < tracks.length; i++) { Format format = tracks[i].getFormat();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -