1、主题:Java Media Framework 本地玩转摄像头正文发表时间:2010-08-04 最后修改:2010-08-10 猎头职位: 上海: 上海:天会皓闻诚聘资深 Java架构师 相关文章: JMF能否扩展支持除 WAV外的格式 求求你们,千万别再说自己是 REST了 Failed to realize: com.sun.media.PlaybackEngine480457 推荐圈子: sip-communicator 更多相关推荐 1、简介The JavaTM Media Framework (JMF) is an application programming interface
2、 (API) for incorporating media data such as audio and video into Java applications and applets. It is specifically designed to take advantage of Java platform features. 这个框架是 SUN公司出的,最新更新至 2003年,用 JMF来处理图像和音频设备并直接应用到 JAVA应用程序中,虽然年代有点久远,但却还是可以用的。The JMF 2.1.1 Reference Implementation supports SunVide
3、o / SunVideoPlus capture devices on Solaris. On Windows, most capture devicesthat have VFW drivers are supported. On Linux, devices that have a Video4Linux driver are expected to work, but not extensively tested. 今天小试了下,自己的 laptop也有个摄像头,录制了一段视频到本地,第一次玩这个东西:)2、模型JMF的模型如下:主题:Java Media Framework 本地玩转摄
4、像头正文基本上是摄像头数据、文件数据、网络媒体流通过 process输出到设备、文件或网络中。Design Goals for JMF:1、Be easy to program2、Support capturing media data3、Enable the development of media streaming and conferencing applications in Java4、Enable advanced developers and technology providers to implement custom solutions based on the exis
5、ting API and easily integrate new features with the existing framework5、Provide access to raw media data6、Enable the development of custom, downloadable demultiplexers, codecs, effects processors, multiplexers, and renderers (JMF plug-ins)Design Goals for RTP:(RTP 是 JMF数据流在网络上的传输协议)1、Enable the deve
6、lopment of media streaming and conferencing applications in Java主题:Java Media Framework 本地玩转摄像头正文2、Support media data reception and transmission using RTP and RTCP3、Support custom packetizer and depacketizer plug-ins through the JMF 2.0 plug-in architecture.4、Be easy to program 3、媒体数据展现Streaming Med
7、ia媒体流,可以存储为多种文件格式或通过 HTTP,RTP等协议传输在网络上。Media PresentationMost time-based media is audio or video data that can be presented through output devices such as speakers and monitors. Such devices are the most common destination for media data output. Media streams can also be sent to other destinations-f
8、or example, saved to a file or transmitted across the network. An output destination for media data is sometimes referred to as a data sink.Media Data Storage and Transmission A DataSink is used to read media data from a DataSource and render the media to some destination-generally a destination oth
9、er than a presentation device. A particular DataSink might write data to a file, write data across the network, or function as an RTP broadcaster.主题:Java Media Framework 本地玩转摄像头正文DataSink用来做数据存储和传输。开始 presenting time-based mediaPresenting Time-Based MediaTo present time-based media such as audio or
10、video with JMF, you use a Player. Playback can be controlled programmatically, or you can display a control-panel component that enables the user to control playback interactively. If you have several media streams that you want to play, you need to use a separate Player for each one. to play them i
11、n sync, you can use one of the Player objects to control the operation of the others.基本上是说播放实时多媒体需要用到 Player,且每个单独的媒体需要单独的Player。代码如下:Java代码 1. / create processor 2. ProcessorModel processorModel = new ProcessorModel(mixedDataSource, outputFormat, outputType); 3. / 创建 model,类似 JTable控件的JModel,The Pr
12、ocessorModel defines the input and output requirements for the Processor 4. Processor processor = null; 5. try 6. 7. processor = Manager.createRealizedProcessor(processorModel);/ 创建 model,类似 JTable控件的 JModel 8. 9. catch (IOException e) Stdout.logAndAbortException(e); 主题:Java Media Framework 本地玩转摄像头正
13、文10. catch (NoProcessorException e) Stdout.logAndAbortException(e); 11. catch (CannotRealizeException e) Stdout.logAndAbortException(e); 创建 processor后,可以装进不同的 compont里来 present。For example, you can call getControls to determine if a Player supports the CachingControl interface. Java代码 1. Control con
14、trols = player.getControls(); 2. 3. for (int i = 0; i controls.length; i+) 4. 5. if (controlsi instanceof CachingControl) 6. cachingControl = (CachingControl) controlsi; 7. 8. JMF提供了很多“播放器”,当然也提供了基本的控制,比如播放、停止以及播放信息等等。控制产生事件。Responding to Media EventsControllerListener is an asynchronous interface f
15、or handling events generated by Controller objects.for example:Java代码 主题:Java Media Framework 本地玩转摄像头正文1. if (event instanceof EventType) 2. . 3. else if (event instanceof OtherEventType) 4. . 5. 6. if (event instanceof EventType) . else if (event instanceof OtherEventType) . JMF提供 ControllerAdapter
16、:Java代码 1. player.addControllerListener(new ControllerAdapter() 2. public void endOfMedia(EndOfMediaEvent e) 3. Controller controller = e.getSource(); 4. controller.stop(); 5. controller.setMediaTime(new Time(0); 6. controller.deallocate(); 7. 8. ) ControllerAdapter automatically dispatches the even
17、t to the appropriate event method, filtering out the events that youre notinterested in.自动为您分发事件并过滤不需要的事件。There is a movie in Applet.展现到 appletJava代码 1. import java.applet.*; 2. import java.awt.*; 3. import .*; 4. import javax.media.*; 5. 6. public class PlayerApplet extends Applet implements Contro
18、llerListener 7. Player player = null; 8. public void init() 主题:Java Media Framework 本地玩转摄像头正文9. setLayout(new BorderLayout(); 10. String mediaFile = getParameter(“FILE“); 11. try 12. URL mediaURL = new URL(getDocumentBase(), mediaFile); 13. player = Manager.createPlayer(mediaURL); 14. player.addCont
19、rollerListener(this); 15. 16. catch (Exception e) 17. System.err.println(“Got exception “+e); 18. 19. 20. public void start() 21. player.start(); 22. 23. public void stop() 24. player.stop(); 25. player.deallocate(); 26. 27. public void destroy() 28. player.close(); 29. 30. public synchronized void
20、controllerUpdate(ControllerEvent event) 31. if (event instanceof RealizeCompleteEvent) 32. Component comp; 33. if (comp = player.getVisualComponent() != null) 34. add (“Center“, comp); 35. if (comp = player.getControlPanelComponent() != null) 36. add (“South“, comp); 37. validate(); 38. 39. 40. 主题:J
21、ava Media Framework 本地玩转摄像头正文Presenting RTP Media StreamsCreating a Player for RTP session.Java代码 1. String url= “rtp:/224.144.251.104:49150/audio/1“; 2. 3. MediaLocator mrl= new MediaLocator(url); 4. 5. if (mrl = null) 6. System.err.println(“Cant build MRL for RTP“); 7. return false; 8. 9. 10. / Cr
22、eate a player for this rtp session 11. try 12. player = Manager.createPlayer(mrl); 13. catch (NoPlayerException e) 14. System.err.println(“Error:“ + e); 15. return false; 16. catch (MalformedURLException e) 17. System.err.println(“Error:“ + e); 18. return false; 19. catch (IOException e) 20. System.
23、err.println(“Error:“ + e); 21. return false; 22. 4、媒体数据处理Converting Media Data from One Format to Another你可以调整媒体的格式。Specifying the Media Destination主题:Java Media Framework 本地玩转摄像头正文你可以指定录像保存的方式,比如输出到文件, 输出到另一个 player。Saving captured media data to a fileJava代码 1. DataSink sink; 2. MediaLocator dest =
24、 new MediaLocator(“file:/newfile.wav“); 3. try 4. sink = Manager.createDataSink(p.getDataOutput(), dest); 5. sink.open(); 6. sink.start(); 7. catch (Exception) 5、扩展 JMFCustom JMF plug-ins can be used seamlessly with Processors that support the plug-in API. After you implement your plug-in, you need
25、to install it and register it with the PlugInManager to make it available to plug-in compatible Processors.Implementing a Codec or Effect Plug-InCodec plug-ins are used to decode compressed media data, convert media data from one format to another, or encode raw media data into a compressed format.用
26、户可以自行扩展压缩格式,以及格式转换。Implementing a Renderer Plug-In主题:Java Media Framework 本地玩转摄像头正文It is a single-input processing component with no output. Renderer plug-ins read data from a DataSource and typically present the media data to the user, but can also be used to provide access to the processed media d
27、ata for use by another application or device.To make a custom plug-in available to a Processor through the TrackControl interface, you need to register it with the PlugInManagerJava代码 1. / Name of the new plugin 2. string GainPlugin = new String(“COM.mybiz.media.GainEffect“); 3. 4. / Supported input
28、 Formats 5. Format supportedInputFormats = new Format 6. new AudioFormat( 7. AudioFormat.LINEAR, 8. Format.NOT_SPECIFIED, 9. 16, 10. Format.NOT_SPECIFIED, 11. AudioFormat.LITTLE_ENDIAN, 12. AudioFormat.SIGNED, 13. 16, 14. Format.NOT_SPECIFIED, 15. Format.byteArray 16. 17. ) 18.; 19. 20./ Supported o
29、utput Formats 21.Format supportedOutputFormats = new Format 22. new AudioFormat( 23. AudioFormat.LINEAR, 24. Format.NOT_SPECIFIED, 25. 16, 26. Format.NOT_SPECIFIED, 主题:Java Media Framework 本地玩转摄像头正文27. AudioFormat.LITTLE_ENDIAN, 28. AudioFormat.SIGNED, 29. 16, 30. Format.NOT_SPECIFIED, 31. Format.by
30、teArray 32. ) 33.; 34. 35./ Add the new plug-in to the plug-in registry 36.PlugInManager.addPlugIn(GainPlugin, supportedInputFormats, 37. supportedOutputFormats, EFFECT); 38. 39./ Save the changes to the plug-in registry 40.PlugInMmit(); Implementing a Protocol Data SourceA DataSource is an abstract
31、ion of a media protocol-handler. You can implement new types of DataSources to support additional protocols by extending PullDataSource,Implementing a DataSinkJMF provides a default DataSink that can be used to write data to a file. Other types of DataSink classes can be implemented to facilitate writing data to the network or to other destinations.6、事例代码this test program will capture the video and audio stream from your USB camera for 10 seconds and stores it on a file, named “testcam.avi“.主题:Java Media Framework 本地玩转摄像头正文