加载声音资源以播放Java

时间:2018-10-17 20:20:41

标签: java audio resources

我知道有很多关于该主题的文章,但是我找不到解决问题的方法,我正在尝试加载.wav文件并播放它。

源代码:

package main;

import java.io.File;

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

public class Sounds {

public static void main(String[] args) {
    play("click.wav");
}
public static void play(String filename)
{
    try
    {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File(filename)));
        clip.start();
    }
    catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}

} 堆栈跟踪:

java.io.FileNotFoundException: click.wav (The system could not find the specified file)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at main.Sounds.play(Sounds.java:18)
at main.Sounds.main(Sounds.java:11)

文件夹层次结构:

enter image description here

0 个答案:

没有答案