简体   繁体   English

Java jLayer GUI 应用程序在播放音乐时冻结

[英]Java jLayer GUI Application freezes while playing the music

I'm working on a Java project that requires background music so I'm using jLayer (MP3 library for the Java Platform) for better applicability and performance for my project.我正在处理一个需要背景音乐的 Java 项目,因此我使用 jLayer(Java 平台的 MP3 库)来提高我的项目的适用性和性能。

I need to play the music with the help of JFileChooser so that the user can easily run own music from the menu bar.我需要在JFileChooser的帮助下播放音乐,以便用户可以轻松地从菜单栏中运行自己的音乐。

But the whole GUI application freezes whenever I'm trying to run the any random music.但是每当我尝试运行任何随机音乐时,整个 GUI 应用程序都会冻结。

Here's the code snippet of my Application这是我的应用程序的代码片段

This is the Image of the Menubar where "Choose your own Music" is added as the Menu Item这是菜单栏的图像,其中添加了“选择自己的音乐”作为菜单项

Can anyone please help me fixing me this code?任何人都可以帮我修复这个代码吗?

The whole GUI application freezes whenever i'm trying to run the any random Music from the menu bar with the help of "JFileChooser"每当我尝试在“JFileChooser”的帮助下从菜单栏中运行任何随机音乐时,整个 GUI 应用程序都会冻结

Code executed from a listener is executed on the Event Dispatch Thread (EDT) .从侦听器执行的代码在Event Dispatch Thread (EDT) This Thread is responsible for responding to events and repainting the GUI.该线程负责响应事件并重新绘制 GUI。 If you execute a long task (like playing music) then the GUI can't respond to events.如果您执行一个较长的任务(如播放音乐),则 GUI 无法响应事件。

You need to start your music player on a separate Thread .您需要在单独的Thread上启动音乐播放器。 One way to do this is to use a SwingWorker which provides additional functionality which is helpful when using Swing.一种方法是使用SwingWorker ,它提供了在使用 Swing 时有用的附加功能。

Read the tutorial on Concurrency in Swing for more information.有关更多信息,请阅读Swing 中的并发性教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM