如果帧大小> 1,则Java AudioInputStream IOException无法读取单个字节

时间:2018-08-08 16:40:00

标签: java audio audio-streaming java-io ioexception

因此,我正在编写Java游戏程序,现在正在为其添加音乐。但是,当我使用AudioInputStream时,它在我的程序上不起作用。

    VSS.getService(VSS.ServiceIds.ExtensionData)
    // the callback on the next line returns a promise, which the JavaScript engine will follow, so you don't need to nest the next `then`
        .then((dataService) => dataService.getDocuments('MyCollection2'))
        .then((docs) => {
            // keep a reference to the element instead of searching for it in each loop.
            const container = document.getElementById('items');

            // this loop will remove any existing children
            while (container.firstChild !== null) {
                container.removeChild(container.firstChild);
            }

            // `for...of` is a simpler way to iterate over a collection
            for (const doc of docs) {
                // create a `div` element
                const div = document.createElement("li");
                div.classList.add("list-group-item");
                div.style.border = "none";

                // add a text node to it
                div.appendChild(document.createTextNode(doc.name));

                // add event listeners to change its background
                div.addEventListener("mouseover", e => { div.style.background = "#e9ecef"; });
                div.addEventListener("mouseout", e => { div.style.background = "white"; });

                // add a `click` listener
                //get all the elements with calss list-group-item
                [...document.querySelectorAll('.list-group-item')].forEach(function(item) {
                    // iterate and add event lstener to each of them
                    item.addEventListener('click', function(elem) {
                        // check if any element have a class active
                        // if so then remove the class from it
                        let getElemWithClass = document.querySelector('.clicked');
                        if (getElemWithClass !== null) {
                            getElemWithClass.classList.remove('clicked');
                            getElemWithClass.classList.add('unClicked')
                        }
                        //add the active class to the element from which click event triggered
                        item.classList.add('clicked')

                    })
                })


                // add the new div to the container
                container.appendChild(div);
            }
        });

结果是错误:java.io.IOException:如果帧大小> 1,则无法读取单个字节。然后我打开了一个新程序,它工作得很好。

    public static void playMusic(String filepath) {
    InputStream music;
    try {
        music = new AudioStream(new FileInputStream(filepath));            
        AudioStream audios = new AudioStream(music);           
        AudioPlayer.player.start(audios);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Error:" + e.toString());
    }
}

}

我看了其他一些地方,但是他们没有我的情况的信息。我有多个班级都与我的主班级相连。当我运行项目时,它是从主运行的。

编辑: 我只是将代码移到了main方法的底部,现在可以使用了。但是,我仍然对为什么要解决这个问题感到好奇。

1 个答案:

答案 0 :(得分:0)

我正在写答案来解决您的问题,而不是指出问题,但是不久前,我读到这是AudioStream中的错误。

如果是虚拟声音,则无需花费太多精力,可以利用JavaFx和Java 8的优势。

static {
    JFXPanel fxPanel = new JFXPanel();
}

private static void playSound(String sound){
    // cl is the ClassLoader for the current class, ie. CurrentClass.class.getClassLoader();
    URL file = cl.getResource(sound);
    final Media media = new Media(file.toString());
    final MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.play();
}