You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用最新版MonteMedia ScreenRecorder在Java录屏报错,求解决方案

解决MonteMedia ScreenRecorder 17.1版本"找不到文件格式Writer"的问题

问题核心原因

  1. 输出路径错误:必须指定具体带后缀的文件路径,而非目录
  2. 旧版格式配置与新版库不兼容,编码参数或格式构造逻辑需要调整
  3. 测试代码存在语法错误(Thread.sleep后缺分号)

修复步骤及代码

1. 修正录屏工具类代码

调整格式参数适配新版库,确保文件路径指向具体文件:

import org.monte.media.math.Rational;
import org.monte.media.screenrecorder.ScreenRecorder;
import org.monte.media.av.Format;
import org.monte.media.av.FormatKeys;
import org.monte.media.av.FormatKeys.MediaType;
import static org.monte.media.av.codec.video.VideoFormatKeys.*;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class Screencorder {

    private Screencorder() {}

    public static ScreenRecorder startRecording(String filePath) throws IOException, AWTException {
        GraphicsConfiguration gc = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration();

        // 适配新版的AVI文件格式定义
        Format fileFormat = new Format(FormatKeys.MediaTypeKey, MediaType.FILE, FormatKeys.MimeTypeKey, MIME_AVI);
        
        // 使用兼容的无压缩RGB编码,避免编码器兼容性问题
        Format screenFormat = new Format(FormatKeys.MediaTypeKey, MediaType.VIDEO,
                EncodingKey, ENCODING_AVI_UNCOMPRESSED_RGB,
                DepthKey, 24,
                FrameRateKey, Rational.valueOf(15),
                QualityKey, 1.0f);
        
        // 鼠标录制格式保持不变
        Format mouseFormat = new Format(FormatKeys.MediaTypeKey, MediaType.VIDEO,
                EncodingKey, ScreenRecorder.ENCODING_BLACK_CURSOR,
                FrameRateKey, Rational.valueOf(30));

        // 传入具体文件路径而非目录
        ScreenRecorder screenRecorder = new ScreenRecorder(gc, null,
                fileFormat, screenFormat, mouseFormat,
                null, new File(filePath));

        screenRecorder.start();
        return screenRecorder;
    }

    public static void stopRecording(ScreenRecorder screenRecorder) throws IOException {
        screenRecorder.stop();
    }
}

2. 修正测试代码

指定具体输出文件,补全语法错误:

import org.junit.jupiter.api.Test;
import org.monte.media.screenrecorder.ScreenRecorder;

class ScreenRecordingTest {

    @Test
    void recordingTest() throws Exception {
        // 必须指定带.avi后缀的具体文件路径
        ScreenRecorder recorder = Screencorder.startRecording("/tmp/recording.avi");

        Thread.sleep(2000); // 补充分号

        Screencorder.stopRecording(recorder);
    }
}

可选优化

如果需要压缩的AVI文件,可替换屏幕录制编码为MJPEG:

Format screenFormat = new Format(FormatKeys.MediaTypeKey, MediaType.VIDEO,
        EncodingKey, ENCODING_AVI_MJPEG,
        FrameRateKey, Rational.valueOf(15),
        QualityKey, 0.8f);

内容的提问来源于stack exchange,提问作者Anna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 20:51:00