我正在尝试在游戏中一次播放两个波形声音(背景音乐和效果)。我首先使用Java中的另一个音频处理程序构造了这段代码,该处理程序将处理声音的播放,停止和循环。这种结构只会播放背景音乐或效果,但一次只能播放一次。我环顾互联网,并被告知要使用javax.sound.sampled.Clip处理声音,因此重用了相同的构造(播放,停止,循环),但将其切换为使用javax.sound.sampled.Clip。现在我完全迷路了。从到目前为止的内容来看,我已经完成了所有正确的操作,并且在eclipse编辑器中没有出现任何错误,但是当我运行它时,我遇到了两个错误之一。在Eclipse中(在Linux上运行),抛出LineUnavailableException。在Eclipse中(在Windows 7上运行),我在此代码的loop()部分中得到了java.lang.NullPointerException。如果您可以告诉我我做错了什么,或者向我指出一些相关文档,我将不胜感激。我假设我的代码可以处理异常,但是我不确定。如果您发现其他任何令人毛骨悚然的代码失误,请让我知道我正在努力成为最好的程序员,并真的感谢 build 性的批评。感谢您的时间。

    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;

    /**
     * Handles play, pause, and looping of sounds for the game.
     * @author Tyler Thomas
     *
     */
    public class Sound {
        private Clip myClip;
        public Sound(String fileName) {
                try {
                    File file = new File(fileName);
                    if (file.exists()) {
                        Clip myClip = AudioSystem.getClip();
                        AudioInputStream ais = AudioSystem.getAudioInputStream(file.toURI().toURL());
                        myClip.open(ais);
                    }
                    else {
                        throw new RuntimeException("Sound: file not found: " + fileName);
                    }
                }
                catch (MalformedURLException e) {
                    throw new RuntimeException("Sound: Malformed URL: " + e);
                }
                catch (UnsupportedAudioFileException e) {
                    throw new RuntimeException("Sound: Unsupported Audio File: " + e);
                }
                catch (IOException e) {
                    throw new RuntimeException("Sound: Input/Output Error: " + e);
                }
                catch (LineUnavailableException e) {
                    throw new RuntimeException("Sound: Line Unavailable: " + e);
                }
        }
        public void play(){
            myClip.setFramePosition(0);  // Must always rewind!
            myClip.loop(0);
            myClip.start();
        }
        public void loop(){
            myClip.loop(Clip.LOOP_CONTINUOUSLY);
        }
        public void stop(){
            myClip.stop();
        }
    }

最佳答案

我能够使代码正常工作,并且现在对Clips有了更好的了解。对我最有帮助的页面是http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html,它可以分解所有内容并帮助我查看我在哪里犯了错误。这是我最后的工作代码。和以前一样,如果您看到任何可怕的错误或逻辑或样式方面的问题,请告诉我。

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * Handles playing, stoping, and looping of sounds for the game.
 * @author Tyler Thomas
 *
 */
public class Sound {
    private Clip clip;
    public Sound(String fileName) {
        // specify the sound to play
        // (assuming the sound can be played by the audio system)
        // from a wave File
        try {
            File file = new File(fileName);
            if (file.exists()) {
                AudioInputStream sound = AudioSystem.getAudioInputStream(file);
             // load the sound into memory (a Clip)
                clip = AudioSystem.getClip();
                clip.open(sound);
            }
            else {
                throw new RuntimeException("Sound: file not found: " + fileName);
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: " + e);
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: " + e);
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: " + e);
        }
        catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
        }

    // play, stop, loop the sound clip
    }
    public void play(){
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }
    public void loop(){
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public void stop(){
            clip.stop();
        }
    }

关于java - 使用javax.sound.sampled.Clip播放,循环播放和停止游戏中的多种声音。意外错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11919009/

10-10 17:15
查看更多