如何在Java中播放声音(获取IOException)

时间:2019-05-17 13:09:27

标签: java user-interface javasound

我正在尝试在游戏背景下播放音乐,并且想使其循环播放,但出现此错误

java.io.IOException: could not create audio stream from input stream

我尝试使用.getResourceAsStream,但是它返回null,可能是因为它需要一个URL,所以我可能对此做错了事

我也尝试过使用this.getClass().getClassLoader().getResource,但是会产生很多运行时错误。

这是程序音乐部分的代码。我已经确认该按钮可以正常工作,单击它只会返回我上面提到的IOException。

public static void music(){
    AudioPlayer AP = AudioPlayer.player;
    AudioStream AS;
    AudioData AD;
    ContinuousAudioDataStream loop = null;

    try{
        AS = new AudioStream(new FileInputStream("theme.mp3"));
        AD = AS.getData();
        loop = new ContinuousAudioDataStream(AD);
    }catch(IOException error){
        System.out.println(error);
    }
    AP.start(loop);
}

0 个答案:

没有答案
相关问题