最近在学习有关jmf的知识,在用jmf捕获声音时出现的问题,代码如下:
- package jmf;
- import java.awt.Component;
- import java.util.Vector;
- import javax.media.*;
- import javax.media.format.AudioFormat;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class CaptureAudio2 extends JFrame implements ControllerListener{
- Player player = null;
- MediaLocator locator = null;
- CaptureDeviceInfo captureInfo ;
- private JPanel panel;
- Component com1;
- Component com2;
- public CaptureAudio2()
- {
- Vector<CaptureDeviceInfo> deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.LINEAR,44100,16,2));
- captureInfo = (CaptureDeviceInfo)deviceList.get(0);
- locator = captureInfo.getLocator();
- }
- public void createComponent() {
- setTitle("视频信号");
- // addWindowListener(new WinClose());
- //setBounds(100, 100, 400, 400);
- panel = new JPanel();
- if ((com1 = player.getVisualComponent()) != null) {
- panel.add(com1);
- }
- if((com2=player.getControlPanelComponent())!=null)
- panel.add(com2);
- add(panel);
- pack();
- setVisible(true);
- }
- public void play()
- {
- try {
- player = Manager.createPlayer(locator);
- player.addControllerListener(this);
- player.realize();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- public void controllerUpdate(ControllerEvent e)
- {
- if(e instanceof RealizeCompleteEvent)
- {
- player.prefetch();
- createComponent();
- }
- if(e instanceof PrefetchCompleteEvent)
- player.start();
- }
- public static void main(String[] args)
- {
- CaptureAudio2 audio = new CaptureAudio2();
- audio.play();
- }
- }
运行后的视图如下:
不过之前我用jmf捕获音频并保存到本地是可以捕获到的,但我是用手机开非常大的声音才勉强可以捕获到,现在我是用MP3的耳塞在做声源,用电脑接收后直接播放出来,结果就是没声音。但我看哪个Media Properties 和 PlugIn Viewer 貌似又是启动的捕获过程。不知道为什么,问同学也没有人知道,老师也不是很懂这个......只能来51cto求助了..
转载于:https://blog.51cto.com/5075496/876204