微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Monte Media Library 用电脑音频录制屏幕

如何解决使用 Monte Media Library 用电脑音频录制屏幕

我正在尝试让 Monte Media Library 将我的屏幕和音频从我的计算机录制到 AVI 文件中。您可以在下面的 startVideoCapture 方法中的代码中看到我尝试过的格式。我尝试按照演示创建格式并尝试在线查找示例但没有成功。有人知道用音频录制屏幕的技巧吗?

import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.FormatKeys.KeyFrameIntervalKey;
import static org.monte.media.FormatKeys.MIME_AVI;
import static org.monte.media.FormatKeys.MediaTypeKey;
import static org.monte.media.FormatKeys.MimeTypeKey;
import static org.monte.media.VideoFormatKeys.CompressorNameKey;
import static org.monte.media.VideoFormatKeys.DepthKey;
import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;
import static org.monte.media.VideoFormatKeys.QualityKey;

import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
//import java.nio.ByteOrder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

//import org.monte.media.AudioFormatKeys;
import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
import org.monte.media.Registry;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;

public class MonteTest {

    private static ScreenRecorder screenRecorder;

    public static void main(String args[]) {
        JFrame frame = new JFrame("Screen Recorder");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        JButton button1 = new JButton("Start Recording");
        JButton button2 = new JButton("Stop Recording");
        button2.setEnabled(false);

        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                startRecording();
                button1.setEnabled(false);
                button2.setEnabled(true);
            }
        });
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                stopRecording();
                button1.setEnabled(true);
                button2.setEnabled(false);
            }
        });

        JPanel contentPane = new JPanel();

        contentPane.add(button1);
        contentPane.add(button2);
        frame.setContentPane(contentPane);
        frame.setVisible(true);
    }

    public static void startRecording() {
        try {
            startVideoCapture(0,400,400);
        } catch (Exception e) {
            e.printstacktrace();
        }
    }

    public static void stopRecording() {
        if (screenRecorder == null) {
            return;
        }
        try {
            stopVideoCapture();
        } catch (IOException e) {
            e.printstacktrace();
        }
    }

    private static void startVideoCapture(int x,int y,int width,int height) throws IOException,AWTException {

        UiScreenRecorderConfiguration config = new UiScreenRecorderConfiguration();
        config.setCfg(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
        config.setArea(new Rectangle(x,y,width,height));
        config.setFileFormat(new Format(MediaTypeKey,MediaType.FILE,MimeTypeKey,MIME_AVI));
        config.setScreenFormat(new Format(MediaTypeKey,MediaType.VIDEO,EncodingKey,ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,CompressorNameKey,DepthKey,24,FrameRateKey,Rational.valueOf(15),QualityKey,1.0f,KeyFrameIntervalKey,15 * 60));
        config.setMouseFormat(null);
        config.setAudioFormat(null);
        config.setMovieFolder(new File(""));

        // These formats do not work.
        //config.setAudioFormat(new Format(MediaTypeKey,MediaType.AUdio,AudioFormatKeys.ENCODING_QUICKTIME_TWOS_PCM,new Rational(48000,1),AudioFormatKeys.SampleSizeInBitsKey,16,AudioFormatKeys.ChannelsKey,2,AudioFormatKeys.SampleRateKey,AudioFormatKeys.SignedKey,true,AudioFormatKeys.ByteOrderKey,ByteOrder.BIG_ENDIAN));
        //config.setAudioFormat(new Format(MediaTypeKey,Rational.valueOf(22050),8,1));


        screenRecorder = new UiScreenRecorder(config);
        screenRecorder.start();
    }

    private static void stopVideoCapture() throws IOException {
        try {
            screenRecorder.stop();
        } catch (Exception e) {
            e.printstacktrace();
        }
    }

    public static class UiScreenRecorder extends ScreenRecorder {

        public UiScreenRecorder(UiScreenRecorderConfiguration config) throws IOException,AWTException {
            super(config.cfg,config.area,config.fileFormat,config.screenFormat,config.mouseFormat,config.audioFormat,config.movieFolder);
        }

        @Override
        protected File createMovieFile(Format fileFormat) throws IOException {
            if (!movieFolder.exists()) {
                movieFolder.mkdirs();
            } else if (!movieFolder.isDirectory()) {
                throw new IOException("\"" + movieFolder + "\" is not a directory.");
            }
            File movieFile = new File(movieFolder,"screenrecording - " + new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat));
            System.out.println("Screen recording file: " + movieFile.getAbsolutePath());
            return movieFile;
        }
    }

    public static class UiScreenRecorderConfiguration {
        private GraphicsConfiguration cfg;
        private Rectangle area;
        private Format fileFormat;
        private Format screenFormat;
        private Format mouseFormat;
        private Format audioFormat;
        private File movieFolder;

        public GraphicsConfiguration getCfg() {
            return cfg;
        }

        public void setCfg(GraphicsConfiguration cfg) {
            this.cfg = cfg;
        }

        public Rectangle getArea() {
            return area;
        }

        public void setArea(Rectangle area) {
            this.area = area;
        }

        public Format getFileFormat() {
            return fileFormat;
        }

        public void setFileFormat(Format fileFormat) {
            this.fileFormat = fileFormat;
        }

        public Format getScreenFormat() {
            return screenFormat;
        }

        public void setScreenFormat(Format screenFormat) {
            this.screenFormat = screenFormat;
        }

        public Format getMouseFormat() {
            return mouseFormat;
        }

        public void setMouseFormat(Format mouseFormat) {
            this.mouseFormat = mouseFormat;
        }

        public Format getAudioFormat() {
            return audioFormat;
        }

        public void setAudioFormat(Format audioFormat) {
            this.audioFormat = audioFormat;
        }

        public File getMovieFolder() {
            return movieFolder;
        }

        public void setMovieFolder(File movieFolder) {
            this.movieFolder = movieFolder;
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。