简体   繁体   English

Java MP3Player通过使用Javazoom播放和暂停

[英]Java MP3Player by using Javazoom play and pause

I'm making a simple MP3Player in Java. 我正在用Java制作一个简单的MP3Player。 I have managed to play a .mp3 file but when I start playing the whole program freezes and I can't click any buttons in the program. 我设法播放.mp3文件,但是当我开始播放整个程序冻结时,我无法点击程序中的任何按钮。 I need help making the pause button, or any other button work after I start playing. 我开始播放后需要帮助制作暂停按钮或任何其他按钮。 This is my code: 这是我的代码:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.InputStream;
    import java.net.URL;
    import javazoom.jl.player.advanced.AdvancedPlayer;
    import javax.swing.*;

    public class MP3Player extends JFrame{

    public MP3Player(){
        JPanel jpBottom = new JPanel();
        JButton btnPlay = new JButton("Play");
        JButton btnPause = new JButton("Pause");

        jpBottom.add(btnPause);
        jpBottom.add(btnPlay);

        Container cp = this.getContentPane();
        BorderLayout bl = new BorderLayout();
        cp.setLayout(bl);
        cp.add(jpBottom, BorderLayout.SOUTH);

        btnPlay.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        Play("file:///C://a.mp3");
                    }
                }
        );

        this.setVisible(true);
        this.setSize(250, 100);
        this.setTitle("MP3 Player");
        this.setLocation(100, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void Play(String path){
        try{
            URL url = new URL(path);
            InputStream in = url.openStream();
            //Player pl = new Player(in);
            //pl.play();
            AdvancedPlayer pl = new AdvancedPlayer(in);

            pl.getPlayBackListener();
            pl.play();             
        }
        catch(Exception e){
            System.out.println("Feil: "+e);
        }
    }

    public static void main(String[] args) {
        MP3Player n = new MP3Player();
    }
}

You should call Play() method in a separate thread and also have a good understanging of Multithreding in Swing . 你应该在一个单独的线程中调用Play()方法,并且在Swing中Multithreding有很好的理解。 At least you should read about SwingUtilities.invokeLater() 至少你应该读一下SwingUtilities.invokeLater()

I need help making the pause button, .. 我需要帮助制作暂停按钮,..

The method to pause the player is stop() . 暂停播放器的方法是stop() But that code creates the AdvancedPlayer local to a method. 但该代码在方法中创建了本地的AdvancedPlayer Instead, the class needs to declare an AdvancedPlayer attribute and reference that class attribute in the actionPerformed() method. 相反,该类需要声明一个AdvancedPlayer属性并在actionPerformed()方法中引用该类属性。

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

相关问题 如何使用JavaZOOM JLayer库播放/暂停mp3文件? - How to Play/Pause a mp3 file using the JavaZOOM JLayer library? Java MP3播放器-播放,暂停和搜寻与jLayer无关 - Java MP3 player - play, pause & seek not working as intended with jLayer 使用javazoom的播放器时出现问题 - Problems using javazoom's Player 模块 java 的问题“模块 'mp3player' 从 'javafx.graphics' 和 'javafx.graphics' 读取 package 'javafx.animation'” - Problem with modules java “Module 'mp3player' reads package 'javafx.animation' from both 'javafx.graphics' and 'javafx.graphics'” 尝试使用javazoom播放MP3文件,并显示错误消息“无法加载资源'sfd.ser” - Trying to play a MP3 file using javazoom and getting an error saying “unable to load resource 'sfd.ser” jlayer.javazoom播放器无法停止mp3 - jlayer.javazoom player can not stop mp3 Javazoom MP3 Player在Netbeans IDE外部无法使用 - Javazoom MP3 Player does not work outside of Netbeans IDE Java:控制台在使用 javazoom 时停止工作 - Java: Console stops working when using javazoom 使用javax.media.Player与javazoom.jl.player.Player播放广播流 - Playing radiostreams using javax.media.Player vs. javazoom.jl.player.Player 声音不会从jar中播放,而只会在我从NetBeans中运行时播放。 (Javazoom播放器) - Sound will not play from jar, but only when i run it from NetBeans. (Javazoom player)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM