简体   繁体   English

Java 声音不播放

[英]Java Sound not Playing

I know this problem has been queried previously, and I have looked at the responses and tried to implement them, but so far nobody's solutions seem to be working for me.我知道这个问题之前已经被询问过,我已经查看了响应并尝试实施它们,但到目前为止,似乎没有人的解决方案对我有用。 Please can anyone have a look at my code and help me figure out why the program ends instantly, and no sound is played....?请任何人都可以看看我的代码并帮助我弄清楚为什么程序会立即结束,并且没有声音播放......? thanks in advance提前致谢

import javax.sound.sampled.*;
import java.io.*;
import java.net.URL;

public class SoundPlayer {

    public SoundPlayer() {
    }

    public static void play(String file) {
        File sound = new File(file);
        try {
            String url = sound.toURI().toURL().toString();
            System.out.println(url);
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new URL(url)));
            clip.start();
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SoundPlayer player = new SoundPlayer();
        player.play("sounds/1.wav");

    }
}

OK, so the answer is to include好的,所以答案是包括

Thread.sleep(1000000);

When you execute a file (a.wav sound file in your case) it is active as long as your code is beign executed so when the execution is over it stops in your case the only code to be executed is to play the.wav file and then the execution is over the sound plays but stops so fast.当您执行文件(在您的情况下为 a.wav 声音文件)时,只要您的代码被执行,它就处于活动状态,因此当执行结束时,它在您的情况下停止,唯一要执行的代码是播放 .wav 文件然后执行结束,声音播放但停止得太快了。

The solution as you suggested Thread.sleep(time) is there to keep the execution for the time chosen and then it stops and the sound file with it.您建议的解决方案Thread.sleep(time)可以在选择的时间内保持执行,然后停止并随之停止声音文件。 You can use a while loop to hold the execution running for as long as the there is code to execute and that means the audio file will play along with it (you have used clip.loop(Clip.LOOP_CONTINUOUSLY); )只要有代码要执行,您就可以使用while循环来保持执行运行,这意味着音频文件将随之播放(您已使用clip.loop(Clip.LOOP_CONTINUOUSLY);

Your program is an example of multi-threaded coding.您的程序是多线程编码的示例。 The command clip.start() launches a new, concurrently running thread and then proceeds to the next line of code without pause.命令clip.start()启动一个新的并发运行的线程,然后继续执行下一行代码而不会暂停。 Your thread then sets the loop flag, and finishes.然后您的线程设置循环标志,并完成。

Meanwhile, the concurrent "play" thread is executing.同时,并发的“播放”线程正在执行。 But this thread has been given "daemon" status.但是这个线程被赋予了“守护进程”状态。 Regular threads will keep a program running until they are finished, but "daemon" status threads will allow themselves to be halted if there are no regular threads alive.常规线程将保持程序运行直到它们完成,但如果没有常规线程处于活动状态,“守护”状态线程将允许自己暂停。 So, when the "main" thread is done, the audio-playback thread also quits.因此,当“主”线程完成时,音频播放线程也退出。

API info about threads . API 有关线程的信息。

When a Clip launches a new thread with the start() method, the thread is automatically given daemon status.Clip使用start()方法启动一个新线程时,该线程会自动获得daemon状态。

It baffles me, though, that I cannot find any API documentation about the status of the new thread launched by Clip .不过,令我感到困惑的是,我找不到任何关于Clip启动的新线程状态的 API 文档。 Nor is there mention of this in Oracle's Playing Back Audio tutorial. Oracle 的播放音频教程中也没有提到这一点。 So you, and countless other people using the Clip object for the first time can hardly be faulted for being mystified when this happens.因此,您和无数其他第一次使用Clip object 的人在发生这种情况时几乎不会因为被迷惑而感到困惑。

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

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