You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

JavaFX MediaPlayer 无法稳定播放MP4文件的问题排查求助

JavaFX MediaPlayer 无法稳定播放MP4文件的问题排查求助

问题描述

我写了一个非常简单的JavaFX视频播放器,但有时运行时它就是无法正常播放指定的MP4文件,出现不稳定的播放失败情况。

源代码(已省略导入语句)

public class App extends Application {

@Override
public void start(Stage stage) {
    videoPlayer(stage);
}

public static void main(String[] args) {
    launch();
}

private void videoPlayer(Stage stage) {
    BorderPane pane = new BorderPane();
    File mediaFile = new File("src/main/resources/Lost   End Credits.mp4");
    MediaView moviePlayer;
    MediaPlayer player = new MediaPlayer(new Media(mediaFile.toURI().toString()));
    moviePlayer = new MediaView(player);
    moviePlayer.setPreserveRatio(true);
    moviePlayer.fitWidthProperty().bind(pane.widthProperty());
    moviePlayer.fitHeightProperty().bind(pane.heightProperty());
    player.setAutoPlay(true);
    player.setCycleCount(MediaPlayer.INDEFINITE);
    pane.getChildren().add(moviePlayer);
    player.play();
    Scene scene = new Scene(pane, 600, 600);
    stage.setScene(scene);
    stage.show();
}

}

Maven依赖配置

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>14</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
</dependencies>

Maven插件配置

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <release>11</release>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.6</version>
        <executions>
            <execution>
                <id>default-cli</id>
                <configuration>
                    <mainClass>org.example.App</mainClass>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

问题现象

我多次运行程序后发现,部分运行实例(比如第2、8、10次)无法显示视频——黑屏表示视频在正常播放,灰屏则说明视频完全没有加载出来。

我的尝试与期望

我已经用自己掌握的JavaFX知识尽可能做了调试,但还是找不到问题根源。希望有人能帮我指出我忽略的细节(大概率和并发逻辑相关),解释清楚导致这个不稳定播放问题的原因。

备注:内容来源于stack exchange,提问作者michaelNXT

火山引擎 最新活动