使用以下代码时
Media media = new Media("video.mp4");
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mv = new MediaView(mediaPlayer);
mediaPlayer.setAutoPlay(true);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
视频播放很棒。但是,在调试代码的其他一些区域时,我注意到媒体播放器似乎在疯狂地创建线程。调试器显示
Daemon Thread [Thread-X](Running)
其中 X 不断增长。看起来线程正在被破坏,但计数在一分钟内创建了超过 1000 个线程。
如果我使用相同的设置,但仅用 mp3 替换 mp4,则不会发生这种情况。
有什么想法吗?
最佳答案
好吧,这不是错误,而是默认 设计 的JavaFX MediaPlayer
.很遗憾,但我们需要等到开发团队决定替换当前的 GLib
用于渲染视频。
基里尔·基里琴科(Kirill Kirichenko)在一个类似的问题上说 JIRA我引用:
You can observe many threads because we attach/detach native threads from native layer on each frame. This is by design now.
原因是他在另一个 JIRA 上给出的。我再次引用:
This happens due to calls AttachCurrentThread on the current JVM every time there is a frame. I can be fixed in the future when we switch to the newer GLib. Currently this is the safest way to render and not to crash jvm.
关于JavaFX MediaPlayer 使用 mp4 创建数百个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22713307/