无法播放音乐的声音,没有Java的错误

发布于 2025-02-04 20:43:47 字数 1655 浏览 2 评论 0原文

我正在尝试运行自己的音乐,但它行不通。不会再在日食中出现错误,但是声音没有播放。这是我的代码,我

public class Music extends Thread {
    private Player player;
    private boolean isLoop;
    private File file;
    private FileInputStream fis;
    private BufferedInputStream bis;
    
    public Music(String name, boolean isLoop)
    {
        try {
            this.isLoop = isLoop;
            //Find the link to the file and play, save the file to the buffer
            file = new File(Main.class.getResource("/music/" + name).toURI());
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            player = new Player(bis);
        } catch(Exception e){
            System.out.println("No music");
        }
    }
    //Get the time of the music, how long it is played 
    public int getTime() {
        if(player == null)
        {
            return 0;
        }
        return player.getPosition();
    }
    //Stop the music played
    public void close() {
        isLoop = false;
        player.close();
        this.interrupt();
    }
    @Override
    public void run() {
        try {
            player.play();
            do {
                player.play();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                player = new Player(bis);
            } while(isLoop);
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

也有我的设置

谢谢您的帮助

I am trying to run the music that I have, but it does not work. There is no error showing up in the eclipse any more, but the sound is not played. This is my code that I have

public class Music extends Thread {
    private Player player;
    private boolean isLoop;
    private File file;
    private FileInputStream fis;
    private BufferedInputStream bis;
    
    public Music(String name, boolean isLoop)
    {
        try {
            this.isLoop = isLoop;
            //Find the link to the file and play, save the file to the buffer
            file = new File(Main.class.getResource("/music/" + name).toURI());
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            player = new Player(bis);
        } catch(Exception e){
            System.out.println("No music");
        }
    }
    //Get the time of the music, how long it is played 
    public int getTime() {
        if(player == null)
        {
            return 0;
        }
        return player.getPosition();
    }
    //Stop the music played
    public void close() {
        isLoop = false;
        player.close();
        this.interrupt();
    }
    @Override
    public void run() {
        try {
            player.play();
            do {
                player.play();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                player = new Player(bis);
            } while(isLoop);
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Also this my setting I have
enter image description here

Thank you for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野生奥特曼 2025-02-11 20:43:47

您的问题从这里开始 - > file = new File(main.class.getResource(“/music/” + name).touri());

不能将嵌入的资源引用为file> file ,因为那不是。而是直接使用类#GetResourCeasStream

接下来,您应该避免从thread延伸,线程不是重新输入的,也就是说,一旦停止,就无法重新启动它们。而是实现运行,例如...

public class Music implements Runnable {
    private String name;
    private Player player;
    private boolean isLoop;

    private Thread playerThread;

    public Music(String name, boolean isLoop) {
        this.isLoop = isLoop;
        this.name = name;
    }
    //Get the time of the music, how long it is played 

    public int getTime() {
        if (player == null) {
            return 0;
        }
        return player.getPosition();
    }
    //Stop the music played

    public void stop() {
        isLoop = false;
        if (player == null) {
            return;
        }
        player.close();
        playerThread = null;
    }

    public void waitFor() throws InterruptedException {
        if (playerThread == null) {
            return;
        }
        playerThread.join();
    }

    public void play() throws InterruptedException {
        if (playerThread != null) {
            stop();
            waitFor();
        }
        playerThread = new Thread(this);
        playerThread.start();
    }

    protected void playAudio() throws IOException, JavaLayerException {
        try (BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream("/music/" + name))) {
            player = new Player(bis);
            player.play();
        }
    }

    @Override
    public void run() {
        try {
            do {
                System.out.println("Start playing");
                playAudio();
                System.out.println("All done");
            } while (isLoop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        stop();
    }
}

Your problem starts here -> file = new File(Main.class.getResource("/music/" + name).toURI());

An embedded resource can't be reference as a File, because, well, it's not. Instead use Class#getResourceAsStream directly.

Next, you should avoid extending from Thread, thread's are not re-entrant, that is, once stopped, you can't restart them. Instead, implement Runnable, for example...

public class Music implements Runnable {
    private String name;
    private Player player;
    private boolean isLoop;

    private Thread playerThread;

    public Music(String name, boolean isLoop) {
        this.isLoop = isLoop;
        this.name = name;
    }
    //Get the time of the music, how long it is played 

    public int getTime() {
        if (player == null) {
            return 0;
        }
        return player.getPosition();
    }
    //Stop the music played

    public void stop() {
        isLoop = false;
        if (player == null) {
            return;
        }
        player.close();
        playerThread = null;
    }

    public void waitFor() throws InterruptedException {
        if (playerThread == null) {
            return;
        }
        playerThread.join();
    }

    public void play() throws InterruptedException {
        if (playerThread != null) {
            stop();
            waitFor();
        }
        playerThread = new Thread(this);
        playerThread.start();
    }

    protected void playAudio() throws IOException, JavaLayerException {
        try (BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream("/music/" + name))) {
            player = new Player(bis);
            player.play();
        }
    }

    @Override
    public void run() {
        try {
            do {
                System.out.println("Start playing");
                playAudio();
                System.out.println("All done");
            } while (isLoop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        stop();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文