使用javax.sound.sampled.Clip播放,循环和停止游戏中的多个声音。意外错误 [英] Using javax.sound.sampled.Clip to play, loop, and stop mutiple sounds in a game. Unexpected Errors

查看:297
本文介绍了使用javax.sound.sampled.Clip播放,循环和停止游戏中的多个声音。意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在游戏中一次播放两个wav声音(背景音乐和效果)。我首先使用Java中的另一个音频处理程序构建了这个代码块,它将处理播放,停止和循环播放声音。这种结构将会播放背景音乐或效果,但只能一次播放。我在互联网环顾四周,被告知要使用javax.sound.sampled.Clip处理声音,因此重复使用相同的结构(播放,停止,循环),但将其切换为使用javax.sound.sampled.Clip。现在我完全迷失了。从目前为止,我已经做了一切正确的事情,在eclipse编辑器中没有任何错误,但是当我运行它时,我会收到两个错误之一。在eclipse中(在Linux上运行)抛出一个LineUnavailableException。在eclipse中(在Windows 7上运行),我在该代码的loop()部分中得到一个java.lang.NullPointerException。如果你能告诉我我做错了什么,或者指出一些相关的文件,我将不胜感激。我假设我的代码处理例外,但我不确定。如果您看到其他可怕的代码错误,请让我知道我正在努力成为我最好的程序员,真正感谢建设性批评。谢谢你的时间。

I'm trying to play two wav sounds at once during a game (Background Music and an effect). I first constructed this chunk of code using another audio handler in java which would handle the playing, stopping, and looping of the sound. This construct would play the background music or effect but only one at a time. I looked around the internet and was told to use javax.sound.sampled.Clip to handle the sounds so reused the same construct(play, stop, loop) but switched it to use javax.sound.sampled.Clip. Now I'm completely lost. From what I have read so far I've done everything correct and get no errors in the eclipse editor, but when I run it I get one of two errors. In eclipse (running on Linux) a LineUnavailableException is thrown. In eclipse (running on windows 7) I get a java.lang.NullPointerException in the loop() section of this code. If you could show me what I'm doing wrong or point me to some relevant documentation I'd appreciate it. I'm assuming its something with my code that handles Exceptions but I'm not sure. If you see any other hideous code missteps please let me know I'm striving the be the best programmer I can and really appreciate constructive criticism. Thank you for your time.

    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();
        }
    }


推荐答案

能够获得代码的工作,现在更好地了解剪辑。帮助我最多的页面是 http://www3.ntu .edu.sg / home / ehchua / programming / java / J8c_PlayingSound.html 它打破了一切,帮助我看到我犯错误。这是我最后的工作代码。像以前一样,如果你看到任何可怕的错误或超过目标的逻辑或风格让我知道。

I was able to get the code working and now have a better understanding of Clips. The page that helped me the most was http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html it breaks everything down and helped me see where I made mistakes. Here is my final working code. As before if you see any horrible errors or over sights in logic or style let me know.

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();
        }
    }

这篇关于使用javax.sound.sampled.Clip播放,循环和停止游戏中的多个声音。意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆