JMF网页视频语音聊天

工程文件下载地址:http://download.csdn.net/source/3378150

 本文地址: http://mzhx-com.iteye.com/blog/1098698

  效果图如下:

 

 

 

三个类 源代码如下:

 

Java代码   收藏代码
  1. package vidioPlay;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.io.IOException;  
  5. import java.net.InetAddress;  
  6. import java.util.Vector;  
  7.   
  8. import javax.media.CaptureDeviceInfo;  
  9. import javax.media.Codec;  
  10. import javax.media.Control;  
  11. import javax.media.Controller;  
  12. import javax.media.ControllerClosedEvent;  
  13. import javax.media.ControllerEvent;  
  14. import javax.media.ControllerListener;  
  15. import javax.media.Format;  
  16. import javax.media.IncompatibleSourceException;  
  17. import javax.media.Manager;  
  18. import javax.media.MediaLocator;  
  19. import javax.media.NoProcessorException;  
  20. import javax.media.Owned;  
  21. import javax.media.Player;  
  22. import javax.media.Processor;  
  23. import javax.media.cdm.CaptureDeviceManager;  
  24. import javax.media.control.QualityControl;  
  25. import javax.media.control.TrackControl;  
  26. import javax.media.format.AudioFormat;  
  27. import javax.media.format.VideoFormat;  
  28. import javax.media.protocol.ContentDescriptor;  
  29. import javax.media.protocol.DataSource;  
  30. import javax.media.protocol.PushBufferDataSource;  
  31. import javax.media.protocol.PushBufferStream;  
  32. import javax.media.protocol.SourceCloneable;  
  33. import javax.media.rtp.RTPManager;  
  34. import javax.media.rtp.SendStream;  
  35. import javax.media.rtp.SessionAddress;  
  36. import javax.media.rtp.rtcp.SourceDescription;  
  37. import javax.swing.JFrame;  
  38.   
  39. public class MediaTransmit {  
  40.   
  41.     private String ipAddress;  
  42.     private int portBase;  
  43.     private MediaLocator audioLocator = null, vedioLocator = null;  
  44.     private Processor audioProcessor = null;  
  45.     private Processor videoProcessor = null;  
  46.     private DataSource audioDataLocal = null, videoDataLocal = null;  
  47.     private DataSource audioDataOutput = null, videoDataOutput = null;  
  48.     private RTPManager rtpMgrs[];  
  49.     private DataSource mediaData = null;  
  50.     private DataSource dataLocalClone = null;  
  51.   
  52.     private PlayPane playFrame;  
  53.   
  54.     public MediaTransmit(String ipAddress, String pb) {  
  55.         this.ipAddress = ipAddress;  
  56.         Integer integer = Integer.valueOf(pb);  
  57.         if (integer != null) {  
  58.             this.portBase = integer.intValue();  
  59.         }  
  60.         // /  
  61.         playFrame = new PlayPane();  
  62.         JFrame jf = new JFrame("视频实例");  
  63.   
  64.         jf.add(playFrame);  
  65.         jf.pack();  
  66.         jf.setLocationRelativeTo(null);  
  67.         jf.setDefaultCloseOperation(3);  
  68.         jf.setVisible(true);  
  69.         //   
  70.         Vector<CaptureDeviceInfo> video = CaptureDeviceManager  
  71.                 .getDeviceList(new VideoFormat(null));  
  72.         Vector<CaptureDeviceInfo> audio = CaptureDeviceManager  
  73.                 .getDeviceList(new AudioFormat(AudioFormat.LINEAR, 44100162));  
  74.         // MediaLocator mediaLocator = new  
  75.         // MediaLocator("file:/C:/纯音乐 - 忧伤还是快乐.mp3");  
  76.         if (audio != null && audio.size() > 0) {  
  77.             audioLocator = ((CaptureDeviceInfo) audio.get(0)).getLocator();  
  78.             if ((audioProcessor = createProcessor(audioLocator)) != null) {  
  79.                 audioDataLocal = mediaData;  
  80.                 audioDataOutput = audioProcessor.getDataOutput();  
  81.             }  
  82.         } else {  
  83.             System.out.println("******错误:没有检测到您的音频采集设备!!!");  
  84.         }  
  85.         // /  
  86.         if (video != null && video.size() > 0) {  
  87.             vedioLocator = ((CaptureDeviceInfo) video.get(0)).getLocator();  
  88.             if ((videoProcessor = createProcessor(vedioLocator)) != null) {  
  89.                 videoDataLocal = mediaData;  
  90.                 videoDataOutput = videoProcessor.getDataOutput();  
  91.             }  
  92.         } else {  
  93.             System.out.println("******错误:没有检测到您的视频设备!!!");  
  94.         }  
  95.         // /  
  96.         final DataSource[] dataSources = new DataSource[2];  
  97.         dataSources[0] = audioDataLocal;  
  98.         dataSources[1] = videoDataLocal;  
  99.         try {  
  100.             DataSource dsLocal = Manager.createMergingDataSource(dataSources);  
  101.             playFrame.localPlay(dsLocal);  
  102.         } catch (IncompatibleSourceException e) {  
  103.             e.printStackTrace();  
  104.             return;  
  105.         }  
  106.         // 远程传输  
  107.         dataSources[1] = audioDataOutput;  
  108.         dataSources[0] = videoDataOutput;  
  109.   
  110.         try {  
  111.             DataSource dsoutput = Manager.createMergingDataSource(dataSources);  
  112.             createTransmitter(dsoutput);  
  113.         } catch (IncompatibleSourceException e) {  
  114.             e.printStackTrace();  
  115.             return;  
  116.         }  
  117.         audioProcessor.start();  
  118.         videoProcessor.start();  
  119.     }  
  120.   
  121.     private Processor createProcessor(MediaLocator locator) {  
  122.         Processor processor = null;  
  123.         if (locator == null)  
  124.             return null;  
  125.         // 通过设备定位器得到数据源,  
  126.         try {  
  127.             mediaData = Manager.createDataSource(locator);  
  128.             // 创建可克隆数据源  
  129.             mediaData = Manager.createCloneableDataSource(mediaData);  
  130.             // 克隆数据源,用于传输到远程  
  131.             dataLocalClone = ((SourceCloneable) mediaData).createClone();  
  132.         } catch (Exception e) {  
  133.             e.printStackTrace();  
  134.             return null;  
  135.         }  
  136.         try {  
  137.             processor = javax.media.Manager.createProcessor(dataLocalClone);  
  138.   
  139.         } catch (NoProcessorException npe) {  
  140.             npe.printStackTrace();  
  141.             return null;  
  142.         } catch (IOException ioe) {  
  143.             return null;  
  144.         }  
  145.         boolean result = waitForState(processor, Processor.Configured);  
  146.         if (result == false)  
  147.             return null;  
  148.   
  149.         TrackControl[] tracks = processor.getTrackControls();  
  150.         if (tracks == null || tracks.length < 1)  
  151.             return null;  
  152.         ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);  
  153.         processor.setContentDescriptor(cd);  
  154.         Format supportedFormats[];  
  155.         Format chosen;  
  156.         boolean atLeastOneTrack = false;  
  157.         for (int i = 0; i < tracks.length; i++) {  
  158.             if (tracks[i].isEnabled()) {  
  159.                 supportedFormats = tracks[i].getSupportedFormats();  
  160.                 if (supportedFormats.length > 0) {  
  161.                     if (supportedFormats[0instanceof VideoFormat) {  
  162.                         chosen = checkForVideoSizes(tracks[i].getFormat(),  
  163.                                 supportedFormats[0]);  
  164.                     } else  
  165.                         chosen = supportedFormats[0];  
  166.                     tracks[i].setFormat(chosen);  
  167.                     System.err  
  168.                             .println("Track " + i + " is set to transmit as:");  
  169.                     System.err.println("  " + chosen);  
  170.                     atLeastOneTrack = true;  
  171.                 } else  
  172.                     tracks[i].setEnabled(false);  
  173.             } else  
  174.                 tracks[i].setEnabled(false);  
  175.         }  
  176.   
  177.         if (!atLeastOneTrack)  
  178.             return null;  
  179.         result = waitForState(processor, Controller.Realized);  
  180.         if (result == false)  
  181.             return null;  
  182.         setJPEGQuality(processor, 0.5f);  
  183.   
  184.         return processor;  
  185.     }  
  186.   
  187.     private String createTransmitter(DataSource dataOutput) {  
  188.         PushBufferDataSource pbds = (PushBufferDataSource) dataOutput;  
  189.         PushBufferStream pbss[] = pbds.getStreams();  
  190.         System.out.println("pbss.length:" + pbss.length);  
  191.         rtpMgrs = new RTPManager[pbss.length];  
  192.         SendStream sendStream;  
  193.         int port;  
  194.         // SourceDescription srcDesList[];  
  195.   
  196.         for (int i = 0; i < pbss.length; i++) {  
  197.             try {  
  198.                 rtpMgrs[i] = RTPManager.newInstance();  
  199.   
  200.                 port = portBase + 2 * i;  
  201.                 SessionAddress localAddr = new SessionAddress(  
  202.                         InetAddress.getLocalHost(), port);  
  203.                 SessionAddress destAddr = new SessionAddress(  
  204.                         InetAddress.getByName(ipAddress), port);  
  205.                 rtpMgrs[i].initialize(localAddr);  
  206.                 rtpMgrs[i].addTarget(destAddr);  
  207.                 System.out.println("Created RTP session: "  
  208.                         + InetAddress.getLocalHost() + " " + port);  
  209.                 sendStream = rtpMgrs[i].createSendStream(dataOutput, i);  
  210.                 sendStream.start();  
  211.             } catch (Exception e) {  
  212.                 e.printStackTrace();  
  213.                 return e.getMessage();  
  214.             }  
  215.         }  
  216.   
  217.         return null;  
  218.     }  
  219.     Format checkForVideoSizes(Format original, Format supported) {  
  220.   
  221.         int width, height;  
  222.         Dimension size = ((VideoFormat) original).getSize();  
  223.         Format jpegFmt = new Format(VideoFormat.JPEG_RTP);  
  224.         Format h263Fmt = new Format(VideoFormat.H263_RTP);  
  225.   
  226.         if (supported.matches(jpegFmt)) {  
  227.             width = (size.width % 8 == 0 ? size.width  
  228.                     : (int) (size.width / 8) * 8);  
  229.             height = (size.height % 8 == 0 ? size.height  
  230.                     : (int) (size.height / 8) * 8);  
  231.         } else if (supported.matches(h263Fmt)) {  
  232.             if (size.width < 128) {  
  233.                 width = 128;  
  234.                 height = 96;  
  235.             } else if (size.width < 176) {  
  236.                 width = 176;  
  237.                 height = 144;  
  238.             } else {  
  239.                 width = 352;  
  240.                 height = 288;  
  241.             }  
  242.         } else {  
  243.             return supported;  
  244.         }  
  245.   
  246.         return (new VideoFormat(nullnew Dimension(width, height),  
  247.                 Format.NOT_SPECIFIED, null, Format.NOT_SPECIFIED))  
  248.                 .intersects(supported);  
  249.     }  
  250.     void setJPEGQuality(Player p, float val) {  
  251.   
  252.         Control cs[] = p.getControls();  
  253.         QualityControl qc = null;  
  254.         VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG);  
  255.         for (int i = 0; i < cs.length; i++) {  
  256.   
  257.             if (cs[i] instanceof QualityControl && cs[i] instanceof Owned) {  
  258.                 Object owner = ((Owned) cs[i]).getOwner();  
  259.                 if (owner instanceof Codec) {  
  260.                     Format fmts[] = ((Codec) owner)  
  261.                             .getSupportedOutputFormats(null);  
  262.                     for (int j = 0; j < fmts.length; j++) {  
  263.                         if (fmts[j].matches(jpegFmt)) {  
  264.                             qc = (QualityControl) cs[i];  
  265.                             qc.setQuality(val);  
  266.                             System.err.println("- Setting quality to " + val  
  267.                                     + " on " + qc);  
  268.                             break;  
  269.                         }  
  270.                     }  
  271.                 }  
  272.                 if (qc != null)  
  273.                     break;  
  274.             }  
  275.         }  
  276.     }  
  277.     private Integer stateLock = new Integer(0);  
  278.     private boolean failed = false;  
  279.   
  280.     Integer getStateLock() {  
  281.         return stateLock;  
  282.     }  
  283.   
  284.     void setFailed() {  
  285.         failed = true;  
  286.     }  
  287.   
  288.     private synchronized boolean waitForState(Processor p, int state) {  
  289.         p.addControllerListener(new StateListener());  
  290.         failed = false;  
  291.         if (state == Processor.Configured) {  
  292.             p.configure();  
  293.         } else if (state == Processor.Realized) {  
  294.             p.realize();  
  295.         }  
  296.         while (p.getState() < state && !failed) {  
  297.             synchronized (getStateLock()) {  
  298.                 try {  
  299.                     getStateLock().wait();  
  300.                 } catch (InterruptedException ie) {  
  301.                     return false;  
  302.                 }  
  303.             }  
  304.         }  
  305.   
  306.         if (failed)  
  307.             return false;  
  308.         else  
  309.             return true;  
  310.     }  
  311.     class StateListener implements ControllerListener {  
  312.   
  313.         public void controllerUpdate(ControllerEvent ce) {  
  314.   
  315.             if (ce instanceof ControllerClosedEvent)  
  316.                 setFailed();  
  317.   
  318.             if (ce instanceof ControllerEvent) {  
  319.                 synchronized (getStateLock()) {  
  320.                     getStateLock().notifyAll();  
  321.                 }  
  322.             }  
  323.         }  
  324.     }  
  325.     public static void main(String[] args) {  
  326.         String[] strs = { "localhost""9994" };  
  327.         new MediaTransmit(strs[0], strs[1]);  
  328.     }  
  329. }  

 

 

 

Java代码   收藏代码
  1. package vidioPlay;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Component;  
  5. import java.awt.Dimension;  
  6. import java.awt.Panel;  
  7. import java.net.InetAddress;  
  8. import java.util.Vector;  
  9.   
  10. import javax.media.ControllerErrorEvent;  
  11. import javax.media.ControllerEvent;  
  12. import javax.media.ControllerListener;  
  13. import javax.media.Player;  
  14. import javax.media.RealizeCompleteEvent;  
  15. import javax.media.bean.playerbean.MediaPlayer;  
  16. import javax.media.control.BufferControl;  
  17. import javax.media.format.FormatChangeEvent;  
  18. import javax.media.protocol.DataSource;  
  19. import javax.media.rtp.Participant;  
  20. import javax.media.rtp.RTPControl;  
  21. import javax.media.rtp.RTPManager;  
  22. import javax.media.rtp.ReceiveStream;  
  23. import javax.media.rtp.ReceiveStreamListener;  
  24. import javax.media.rtp.SessionListener;  
  25. import javax.media.rtp.event.ByeEvent;  
  26. import javax.media.rtp.event.NewParticipantEvent;  
  27. import javax.media.rtp.event.NewReceiveStreamEvent;  
  28. import javax.media.rtp.event.ReceiveStreamEvent;  
  29. import javax.media.rtp.event.RemotePayloadChangeEvent;  
  30. import javax.media.rtp.event.SessionEvent;  
  31. import javax.media.rtp.event.StreamMappedEvent;  
  32. import javax.swing.JFrame;  
  33.   
  34. import net.sf.fmj.media.rtp.RTPSocketAdapter;  
  35.   
  36.   
  37. public class MediaReceive implements ReceiveStreamListener, SessionListener,  
  38.         ControllerListener {  
  39.     String sessions[] = null;  
  40.     RTPManager mgrs[] = null;  
  41.   
  42.     boolean dataReceived = false;  
  43.     Object dataSync = new Object();  
  44.     private PlayPane playFrame;  
  45.   
  46.     public MediaReceive(String sessions[]) {  
  47.         this.sessions = sessions;  
  48.     }  
  49.   
  50.     protected void initialize() {  
  51.         playFrame = new PlayPane();  
  52.         JFrame jf = new JFrame("视频实例");  
  53.   
  54.         jf.add(playFrame);  
  55.         jf.pack();  
  56.         jf.setLocationRelativeTo(null);  
  57.         jf.setDefaultCloseOperation(3);  
  58.         jf.setVisible(true);  
  59.         try {  
  60.             // 每一个session对应一个RTPManager  
  61.             mgrs = new RTPManager[sessions.length];  
  62.             // 创建播放窗口的向量vector  
  63.   
  64.             SessionLabel session = null;  
  65.   
  66.             // Open the RTP sessions.  
  67.             // 针对每一个会话对象进行ip、port和ttl的解析  
  68.             for (int i = 0; i < sessions.length; i++) {  
  69.   
  70.                 // Parse the session addresses.  
  71.                 // 进行会话对象的解析,得到ip、port和ttl  
  72.                 try {  
  73.                     session = new SessionLabel(sessions[i]);  
  74.                 } catch (IllegalArgumentException e) {  
  75.                     System.err  
  76.                             .println("Failed to parse the session address given: "  
  77.                                     + sessions[i]);  
  78.                     // return false;  
  79.                 }  
  80.   
  81.                 System.err.println("  - Open RTP session for: addr: "  
  82.                         + session.addr + " port: " + session.port + " ttl: "  
  83.                         + session.ttl);  
  84.                 // 这对本条会话对象创建RTPManager  
  85.                 mgrs[i] = (RTPManager) RTPManager.newInstance();  
  86.                 mgrs[i].addSessionListener(this);  
  87.                 mgrs[i].addReceiveStreamListener(this);  
  88.   
  89.                 // Initialize the RTPManager with the RTPSocketAdapter  
  90.                 // 将本机ip和端口号加入RTP会话管理  
  91.                 System.out.println("session.addr:" + session.addr);  
  92.                 mgrs[i].initialize(new RTPSocketAdapter(InetAddress  
  93.                         .getByName(session.addr), session.port, session.ttl));  
  94.                 BufferControl bc = (BufferControl) mgrs[i]  
  95.                         .getControl("javax.media.control.BufferControl");  
  96.                 if (bc != null)  
  97.                     bc.setBufferLength(350);  
  98.             }  
  99.   
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.         }  
  103.     }  
  104.   
  105.     /** 
  106.      * Close the players and the session managers. 
  107.      */  
  108.     protected void close() {  
  109.   
  110.         // close the RTP session.  
  111.         for (int i = 0; i < mgrs.length; i++) {  
  112.             if (mgrs[i] != null) {  
  113.                 mgrs[i].removeTargets("Closing session from AVReceive3");  
  114.                 mgrs[i].dispose();  
  115.                 mgrs[i] = null;  
  116.             }  
  117.         }  
  118.     }  
  119.   
  120.     /** 
  121.      * SessionListener. 
  122.      */  
  123.     @SuppressWarnings("deprecation")  
  124.     public synchronized void update(SessionEvent evt) {  
  125.   
  126.         if (evt instanceof NewParticipantEvent) {  
  127.             Participant p = ((NewParticipantEvent) evt).getParticipant();  
  128.             System.err.println("  - A new participant had just joined: " + p);  
  129.         }  
  130.     }  
  131.   
  132.     /** 
  133.      * ReceiveStreamListener 
  134.      */  
  135.     public synchronized void update(ReceiveStreamEvent evt) {  
  136.   
  137.         RTPManager mgr = (RTPManager) evt.getSource();  
  138.         Participant participant = evt.getParticipant(); // could be null.  
  139.         ReceiveStream stream = evt.getReceiveStream(); // could be null.  
  140.   
  141.         if (evt instanceof RemotePayloadChangeEvent) {  
  142.   
  143.             System.err.println("  - Received an RTP PayloadChangeEvent.");  
  144.             System.err.println("Sorry, cannot handle payload change.");  
  145.             // System.exit(0);  
  146.   
  147.         }  
  148.   
  149.         else if (evt instanceof NewReceiveStreamEvent) {  
  150.             System.out.println("evt instanceof NewReceiveStreamEvent");  
  151.             try {  
  152.                 stream = ((NewReceiveStreamEvent) evt).getReceiveStream();  
  153.                 final DataSource data = stream.getDataSource();  
  154.   
  155.                 // Find out the formats.  
  156.                 RTPControl ctl = (RTPControl) data  
  157.                         .getControl("javax.media.rtp.RTPControl");  
  158.                 if (ctl != null) {  
  159.                     System.err.println("  - Recevied new RTP stream: "  
  160.                             + ctl.getFormat());  
  161.                 } else  
  162.                     System.err.println("  - Recevied new RTP stream");  
  163.   
  164.                 if (participant == null)  
  165.                     System.err  
  166.                             .println("      The sender of this stream had yet to be identified.");  
  167.                 else {  
  168.                     System.err.println("      The stream comes from: "  
  169.                             + participant.getCNAME());  
  170.                 }  
  171.   
  172.                 // create a player by passing datasource to the Media Manager  
  173.                 new Thread() {  
  174.                     public void run() {  
  175.                         playFrame.remotePlay(data);  
  176.                     }  
  177.                 }.start();  
  178.                 // Player p = javax.media.Manager.createPlayer(data);  
  179.                 // if (p == null)  
  180.                 // return;  
  181.                 //  
  182.                 // p.addControllerListener(this);  
  183.                 // p.realize();  
  184.                 // PlayerWindow pw = new PlayerWindow(p, stream);  
  185.                 // playerWindows.addElement(pw);  
  186.   
  187.                 // Notify intialize() that a new stream had arrived.  
  188.                 synchronized (dataSync) {  
  189.                     dataReceived = true;  
  190.                     dataSync.notifyAll();  
  191.                 }  
  192.   
  193.             } catch (Exception e) {  
  194.                 System.err.println("NewReceiveStreamEvent exception "  
  195.                         + e.getMessage());  
  196.                 return;  
  197.             }  
  198.   
  199.         }  
  200.   
  201.         else if (evt instanceof StreamMappedEvent) {  
  202.             System.out.println("evt instanceof StreamMappedEvent");  
  203.             stream = ((StreamMappedEvent) evt).getReceiveStream();  
  204.             if (stream != null && stream.getDataSource() != null) {  
  205.                 DataSource ds = stream.getDataSource();  
  206.                 // Find out the formats.  
  207.                 RTPControl ctl = (RTPControl) ds  
  208.                         .getControl("javax.media.rtp.RTPControl");  
  209.                 System.err.println("  - The previously unidentified stream ");  
  210.                 if (ctl != null)  
  211.                     System.err.println("      " + ctl.getFormat());  
  212.                 System.err.println("      had now been identified as sent by: "  
  213.                         + participant.getCNAME());  
  214.                 System.out.println("ds == null" + (ds == null));  
  215.             }  
  216.         }  
  217.   
  218.         else if (evt instanceof ByeEvent) {  
  219.   
  220.             System.err.println("  - Got \"bye\" from: "  
  221.                     + participant.getCNAME());  
  222.   
  223.         }  
  224.   
  225.     }  
  226.   
  227.     /** 
  228.      * ControllerListener for the Players. 
  229.      */  
  230.     public synchronized void controllerUpdate(ControllerEvent ce) {  
  231.   
  232.         Player p = (Player) ce.getSourceController();  
  233.   
  234.         if (p == null)  
  235.             return;  
  236.   
  237.     }  
  238.   
  239.     /** 
  240.      * A utility class to parse the session addresses. 
  241.      */  
  242.     class SessionLabel {  
  243.   
  244.         public String addr = null;  
  245.         public int port;  
  246.         public int ttl = 1;  
  247.   
  248.         SessionLabel(String session) throws IllegalArgumentException {  
  249.   
  250.             int off;  
  251.             String portStr = null, ttlStr = null;  
  252.   
  253.             if (session != null && session.length() > 0) {  
  254.                 while (session.length() > 1 && session.charAt(0) == '/') {  
  255.                     session = session.substring(1);  
  256.                 }  
  257.                 off = session.indexOf('/');  
  258.                 if (off == -1) {  
  259.                     if (!session.equals(""))  
  260.                         addr = session;  
  261.                 } else {  
  262.                     addr = session.substring(0, off);  
  263.                     session = session.substring(off + 1);  
  264.                     off = session.indexOf('/');  
  265.                     if (off == -1) {  
  266.                         if (!session.equals(""))  
  267.                             portStr = session;  
  268.                     } else {  
  269.                         portStr = session.substring(0, off);  
  270.                         session = session.substring(off + 1);  
  271.                         off = session.indexOf('/');  
  272.                         if (off == -1) {  
  273.                             if (!session.equals(""))  
  274.                                 ttlStr = session;  
  275.                         } else {  
  276.                             ttlStr = session.substring(0, off);  
  277.                         }  
  278.                     }  
  279.                 }  
  280.             }  
  281.   
  282.             if (addr == null)  
  283.                 throw new IllegalArgumentException();  
  284.   
  285.             if (portStr != null) {  
  286.                 try {  
  287.                     Integer integer = Integer.valueOf(portStr);  
  288.                     if (integer != null)  
  289.                         port = integer.intValue();  
  290.                 } catch (Throwable t) {  
  291.                     throw new IllegalArgumentException();  
  292.                 }  
  293.             } else  
  294.                 throw new IllegalArgumentException();  
  295.   
  296.             if (ttlStr != null) {  
  297.                 try {  
  298.                     Integer integer = Integer.valueOf(ttlStr);  
  299.                     if (integer != null)  
  300.                         ttl = integer.intValue();  
  301.                 } catch (Throwable t) {  
  302.                     throw new IllegalArgumentException();  
  303.                 }  
  304.             }  
  305.         }  
  306.     }  
  307.       
  308.   
  309.     public static void main(String argv[]) {  
  310.         String[] strs = { "125.221.165.126/9994""125.221.165.126/9996" };  
  311.         MediaReceive avReceive = new MediaReceive(strs);  
  312.         avReceive.initialize();  
  313.   
  314.     }  
  315. }  

 

 

 

Java代码   收藏代码
  1. package vidioPlay;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Color;  
  5. import java.awt.Component;  
  6. import java.awt.Dimension;  
  7. import java.awt.FlowLayout;  
  8. import java.awt.Graphics;  
  9. import java.awt.Rectangle;  
  10. import java.awt.event.ActionEvent;  
  11. import java.awt.event.ActionListener;  
  12. import java.io.IOException;  
  13.   
  14. import javax.media.ControllerEvent;  
  15. import javax.media.ControllerListener;  
  16. import javax.media.DataSink;  
  17. import javax.media.NoPlayerException;  
  18. import javax.media.Player;  
  19. import javax.media.Processor;  
  20. import javax.media.protocol.DataSource;  
  21. import javax.swing.ImageIcon;  
  22. import javax.swing.JButton;  
  23. import javax.swing.JPanel;  
  24.   
  25. public class PlayPane extends JPanel {  
  26.     private ImageIcon videoReqIcon = new ImageIcon("videoReq.jpg");  
  27.     private ImageIcon VideolocalIcon = new ImageIcon("localVideo.jpg");  
  28.     private boolean isViewBigPlaying = false;  
  29.     private boolean isViewSmallPlaying = false;  
  30.     private JPanel viewBigPane;  
  31.     private JPanel viewSmallPane;  
  32.     private JPanel controlPane;  
  33.   
  34.     private JButton closeButton;  
  35.   
  36.     private boolean localPlay = false;  
  37.     private boolean remotePlay = false;  
  38.   
  39.     private DataSource localData;  
  40.     private DataSource remoteData;  
  41.   
  42.     private boolean isViewRun = true;  
  43.     private boolean isShow = true;  
  44.     //  
  45.     private Player localPlayer = null;  
  46.     private Player remotePlayer = null;  
  47.     //  
  48.     private Processor videotapeProcessor = null;  
  49.     private Player videotapePlayer = null;  
  50.     private DataSink videotapeFileWriter;  
  51.   
  52.     public PlayPane() {  
  53.         this.setLayout(new BorderLayout());  
  54.         // 视图面板  
  55.         viewBigPane = new JPanel() {  
  56.             public void paintComponent(Graphics g) {  
  57.                 super.paintComponent(g);  
  58.                 if (!isViewBigPlaying) {  
  59.                     g.drawImage(videoReqIcon.getImage(), 11,  
  60.                             videoReqIcon.getIconWidth(),  
  61.                             videoReqIcon.getIconHeight(), null);  
  62.   
  63.                     g.drawRect(getSmallPlayRec().x - 1,  
  64.                             getSmallPlayRec().y - 1,  
  65.                             getSmallPlayRec().width + 1,  
  66.                             getSmallPlayRec().height + 1);  
  67.                 } else {  
  68.   
  69.                 }  
  70.             }  
  71.         };  
  72.         viewBigPane.setBackground(Color.black);  
  73.         this.add(viewBigPane, BorderLayout.CENTER);  
  74.         viewBigPane.setLayout(null);  
  75.         // ///  
  76.         viewSmallPane = new JPanel() {  
  77.             public void paintComponent(Graphics g) {  
  78.                 super.paintComponent(g);  
  79.                 if (!isViewSmallPlaying) {  
  80.                     g.drawImage(VideolocalIcon.getImage(), 00null);  
  81.                 } else {  
  82.   
  83.                 }  
  84.             }  
  85.         };  
  86.         viewSmallPane.setBounds(getSmallPlayRec());  
  87.         viewBigPane.add(viewSmallPane);  
  88.         viewSmallPane.setLayout(null);  
  89.   
  90.         // 控制面板组件  
  91.         closeButton = new JButton("挂断");  
  92.         controlPane = new JPanel();  
  93.         controlPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 00));  
  94.         controlPane.add(closeButton);  
  95.         this.add(controlPane, BorderLayout.SOUTH);  
  96.         closeButton.addActionListener(new ActionListener() {  
  97.             public void actionPerformed(ActionEvent e) {  
  98.                 if (localPlayer != null) {  
  99.                     localPlayer.stop();  
  100.                 }  
  101.                 if (remotePlayer != null) {  
  102.                     remotePlayer.stop();  
  103.                 }  
  104.                 if (videotapePlayer != null) {  
  105.                     videotapePlayer.stop();  
  106.                 }  
  107.                 if (videotapeProcessor != null) {  
  108.                     videotapeProcessor.stop();  
  109.                 }  
  110.                 if (videotapeFileWriter != null) {  
  111.                     try {  
  112.                         videotapeFileWriter.stop();  
  113.                         videotapeFileWriter.close();  
  114.                     } catch (IOException e1) {  
  115.                     }  
  116.                 }  
  117.             }  
  118.   
  119.         });  
  120.         // this.setMinimumSize(new Dimension(videoReqIcon.getIconWidth()+2,  
  121.         // 241));  
  122.         // this.setPreferredSize(new Dimension(videoReqIcon.getIconWidth()+2,  
  123.         // 241));  
  124.   
  125.     }  
  126.   
  127.     public Dimension getMinimumSize() {  
  128.         System.out  
  129.                 .println("controlPane.getHeight():" + controlPane.getHeight());  
  130.         return new Dimension(videoReqIcon.getIconWidth() + 2,  
  131.                 videoReqIcon.getIconHeight() + controlPane.getHeight());  
  132.     }  
  133.   
  134.     public Dimension getPreferredSize() {  
  135.         System.out  
  136.                 .println("controlPane.getHeight():" + controlPane.getHeight());  
  137.         return new Dimension(videoReqIcon.getIconWidth() + 2,  
  138.                 videoReqIcon.getIconHeight()  
  139.                         + controlPane.getPreferredSize().height);  
  140.     }  
  141.   
  142.     public void localPlay(DataSource dataSource) {  
  143.         this.setLocalData(dataSource);  
  144.         try {  
  145.             localPlayer = javax.media.Manager.createPlayer(dataSource);  
  146.   
  147.             localPlayer.addControllerListener(new ControllerListener() {  
  148.                 public void controllerUpdate(ControllerEvent e) {  
  149.   
  150.                     if (e instanceof javax.media.RealizeCompleteEvent) {  
  151.                         Component comp = null;  
  152.                         comp = localPlayer.getVisualComponent();  
  153.                         if (comp != null) {  
  154.                             // 将可视容器加到窗体上  
  155.                             comp.setBounds(00, VideolocalIcon.getIconWidth(),  
  156.                                     VideolocalIcon.getIconHeight());  
  157.                             viewSmallPane.add(comp);  
  158.                         }  
  159.                         viewBigPane.validate();  
  160.                     }  
  161.                 }  
  162.             });  
  163.             localPlayer.start();  
  164.             localPlay = true;  
  165.         } catch (NoPlayerException e1) {  
  166.             e1.printStackTrace();  
  167.         } catch (IOException e1) {  
  168.             e1.printStackTrace();  
  169.         }  
  170.     }  
  171.   
  172.     private Rectangle getSmallPlayRec() {  
  173.         int bigShowWidth = videoReqIcon.getIconWidth();  
  174.         int bigShowHeight = videoReqIcon.getIconHeight();  
  175.         int smallShowWidth = VideolocalIcon.getIconWidth();  
  176.         int smallShowHeight = VideolocalIcon.getIconHeight();  
  177.         return new Rectangle(bigShowWidth - smallShowWidth - 2, bigShowHeight  
  178.                 - smallShowHeight - 2, smallShowWidth, smallShowHeight);  
  179.     }  
  180.   
  181.     public void remotePlay(DataSource dataSource) {  
  182.         this.setLocalData(dataSource);  
  183.         remotePlay = true;  
  184.         try {  
  185.             remotePlayer = javax.media.Manager.createPlayer(dataSource);  
  186.   
  187.             remotePlayer.addControllerListener(new ControllerListener() {  
  188.                 public void controllerUpdate(ControllerEvent e) {  
  189.   
  190.                     if (e instanceof javax.media.RealizeCompleteEvent) {  
  191.                         Component comp;  
  192.                         if ((comp = remotePlayer.getVisualComponent()) != null) {  
  193.                             // 将可视容器加到窗体上  
  194.                             comp.setBounds(11, videoReqIcon.getIconWidth(),  
  195.                                     videoReqIcon.getIconHeight());  
  196.                             viewBigPane.add(comp);  
  197.                         }  
  198.                         viewBigPane.validate();  
  199.                     }  
  200.                 }  
  201.             });  
  202.             remotePlayer.start();  
  203.             remotePlay = true;  
  204.         } catch (NoPlayerException e1) {  
  205.             e1.printStackTrace();  
  206.         } catch (IOException e1) {  
  207.             e1.printStackTrace();  
  208.         }  
  209.     }  
  210.   
  211.     public void closeViewUI() {  
  212.         isShow = false;  
  213.     }  
  214.   
  215.     public boolean isViewRunning() {  
  216.         return isViewRun;  
  217.     }  
  218.   
  219.     public boolean isShowing() {  
  220.         return isShow;  
  221.     }  
  222.   
  223.     public void localReady() {  
  224.         localPlay = true;  
  225.     }  
  226.   
  227.     public void remoteReady() {  
  228.         remotePlay = true;  
  229.     }  
  230.   
  231.     public boolean isRemotePlay() {  
  232.         return remotePlay;  
  233.     }  
  234.   
  235.     public void setRemotePlay(boolean remotePlay) {  
  236.         this.remotePlay = remotePlay;  
  237.     }  
  238.   
  239.     public DataSource getRemoteData() {  
  240.         return remoteData;  
  241.     }  
  242.   
  243.     public void setRemoteData(DataSource remoteData) {  
  244.         this.remoteData = remoteData;  
  245.     }  
  246.   
  247.     public boolean isLocalPlay() {  
  248.         return localPlay;  
  249.     }  
  250.   
  251.     public void setLocalPlay(boolean localPlay) {  
  252.         this.localPlay = localPlay;  
  253.     }  
  254.   
  255.     public DataSource getLocalData() {  
  256.         return localData;  
  257.     }  
  258.   
  259.     public void setLocalData(DataSource localData) {  
  260.         this.localData = localData;  
  261.     }  
  262.   
  263. }  

 

 

之后操作如下:

1、安装JMF软件(如果不想安装,就请阅读我转载一篇文章):

2、新建工程将源代码加入工程src下

3、导入第三方包jmf.jar,和fmj.jar(如果网上找不到,请加入该群83945749)

4、运行的时候先运行MediaTransmit类,后运行MediaReceive类

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值