声音在Eclipse中无法播放

huangapple go评论77阅读模式
英文:

Sound not playing in Eclipse

问题

这是类SoundTest的代码:

package code;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class SoundTest {

    public static void main(String args[]) {
        File sound = new File("main//Sounds//open1.wav");
        playSound(sound);
    }

    public static void playSound(File f) {
        try {

            File file = f;
            if (file.exists()) {
                AudioInputStream audio = AudioSystem.getAudioInputStream(file);
                Clip clip = AudioSystem.getClip();
                clip.open(audio);
                clip.start();
                // clip.close();
            } else {
                System.out.println("无法找到文件");
            }

        } catch (Exception e) {
            System.out.println("未成功工作!");
        }
    }
}

当我运行它时,没有错误或异常,它从未显示“无法找到文件”或“未成功工作!”。但是我听不到声音。我是否需要在Eclipse中更改某种设置以使声音可听,还是我做错了什么?我相当确定声音文件位于正确的位置。

英文:

Here is the code for class SoundTest:


package code;


import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;


public class SoundTest {

	public static void main(String args[])
	{
		File sound = new File("main//Sounds//open1.wav");
		playSound(sound);
	}
	
	public static void playSound(File f)
	{
		try {

			File file = f;
			if(file.exists())
			{
				AudioInputStream audio = AudioSystem.getAudioInputStream(file);
				Clip clip = AudioSystem.getClip();
				clip.open(audio);
				clip.start();
				//clip.close();
			}
			else
			{
				System.out.println("Can't find file");
			}
			
			
		}
		catch(Exception e)
		{
			System.out.println("Didn't work!");
		}
	}
}

When I run it, there are no errors or exceptions, and it never says "Can't find file" or "Didn't work!". But I don't hear a sound. Do I have to change some sort of setting in Eclipse to make the sound audible, or am I doing something wrong? I'm pretty sure the sound file is located in the right place.

答案1

得分: 2

这不是 Eclipse 的问题。文件是由 Java 打开并播放的,但甚至在音频剪辑完成播放之前,程序就被终止了 - 这意味着 Java 程序没有等待剪辑完成播放。

你的方法缺少一行代码,只需要添加这一行代码,就可以得到你想要的结果,见下文:

Thread.sleep(clip.getMicrosecondLength() / 1000);

因此,在添加了这行代码之后,你的代码应该如下所示,然后你就能听到音乐的播放:

AudioInputStream audio = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
英文:

That is not eclipse problem. File is being opened and played by java, but even before that audio clip completes playing, the program is getting terminated - Meaning java program is not waiting for the clip to complete playing

Your method is missing one single line of code to give you what you want, see below

Thread.sleep(clip.getMicrosecondLength() / 1000);

So, after adding this line your code should look like this and you will hear your music

    AudioInputStream audio = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(audio);
    clip.start();
    Thread.sleep(clip.getMicrosecondLength() / 1000);

huangapple
  • 本文由 发表于 2020年5月5日 06:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定