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

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

问题描述

我是一个游戏(背景音乐和效果)在试图打两个WAV声音一次。我第一次在Java中使用这将处理播放,停止其他音频处理,声音的构造循环code的这个块。该构建将播放的背景音乐或效果,但仅一次。我看了看周围的互联网,被告知使用到javax.sound.sampled.Clip中处理声音如此重复使用相同的结构(播放,停止,循环),但它切换使用javax.sound.sampled.Clip中。现在我完全丧失。从我读过到目前为止,我所做的一切正确的,并得到在Eclipse编辑器没有错误,但是当我运行它,我得到两个错误之一。在Eclipse(在Linux上运行)一个LineUnavailableException异常。在Eclipse(运行在Windows 7)我在code的循环()部分得到显示java.lang.NullPointerException。如果你能告诉我,我做错了什么,或我指向了一些相关文件我最好的AP preciate它。我假设它与我的code,处理例外的东西,但我不知道。如果你看到任何其他狰狞code失误,请让我知道我的努力是我能在最好的程序员,真正AP preciate建设性的批评。感谢您的时间。

 进口的java.io.File;
    进口java.io.IOException异常;
    进口java.net.MalformedURLException;
    进口javax.sound.sampled.AudioInputStream中;
    进口javax.sound.sampled.AudioSystem;
    javax.sound.sampled.Clip中的进口;
    进口javax.sound.sampled.LineUnavailableException;
    进口javax.sound.sampled.UnsupportedAudioFileException;    / **
     *处理播放,暂停和声音的循环进行游戏。
     * @author泰勒·托马斯
     *
     * /
    公共类声音{
        私人夹myClip;
        市民声音(字符串文件名){
                尝试{
                    档案文件=新的文件(文件名);
                    如果(file.exists()){
                        剪辑myClip = AudioSystem.getClip();
                        的AudioInputStream AIS = AudioSystem.getAudioInputStream(file.toURI()的toURL());
                        myClip.open(AIS);
                    }
                    其他{
                        抛出新的RuntimeException(声音:未找到文件:+文件名);
                    }
                }
                赶上(MalformedURLException的E){
                    抛出新的RuntimeException(声音:不正确的网址:+ E);
                }
                赶上(UnsupportedAudioFileException E){
                    抛出新的RuntimeException(声音:不支持的音频文件:+ E);
                }
                赶上(IOException异常五){
                    抛出新的RuntimeException(声音:输入/输出错误:+ E);
                }
                赶上(LineUnavailableException E){
                    抛出新的RuntimeException(声音:行不可用+ E);
                }
        }
        公共无效播放(){
            myClip.setFramePosition(0); //必须始终快退!
            myClip.loop(0);
            myClip.start();
        }
        公共无效循环(){
            myClip.loop(Clip.LOOP_CONTINUOUSLY);
        }
        公共无效停止(){
            myClip.stop();
        }
    }


解决方案

我能获得code工作,现在有了更好的了解剪辑的。帮助我的最深的是<一个页面href=\"http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html\">http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html它打破一切下来,帮我看看,我犯的错误。这是我最后的工作code。与以前一样,如果你看到任何可怕的错误或以上的逻辑和风格的景点让我知道。

 进口的java.io.File;
进口java.io.IOException异常;
进口java.net.MalformedURLException;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
javax.sound.sampled.Clip中的进口;
进口javax.sound.sampled.LineUnavailableException;
进口javax.sound.sampled.UnsupportedAudioFileException;/ **
 *处理演奏,回采和声音的循环进行游戏。
 * @author泰勒·托马斯
 *
 * /
公共类声音{
    私人夹夹;
    市民声音(字符串文件名){
        //指定播放声音
        //(假设声音可以由音频系统播放)
        //从WAVE文件
        尝试{
            档案文件=新的文件(文件名);
            如果(file.exists()){
                的AudioInputStream声音= AudioSystem.getAudioInputStream(文件);
             //加载声音到内存(剪辑)
                夹= AudioSystem.getClip();
                clip.open(音);
            }
            其他{
                抛出新的RuntimeException(声音:未找到文件:+文件名);
            }
        }
        赶上(MalformedURLException的E){
            e.printStackTrace();
            抛出新的RuntimeException(声音:不正确的网址:+ E);
        }
        赶上(UnsupportedAudioFileException E){
            e.printStackTrace();
            抛出新的RuntimeException(声音:不支持的音频文件:+ E);
        }
        赶上(IOException异常五){
            e.printStackTrace();
            抛出新的RuntimeException(声音:输入/输出错误:+ E);
        }
        赶上(LineUnavailableException E){
            e.printStackTrace();
            抛出新的RuntimeException(声音:行不可用异常错误:+ E);
        }    //播放,停止,循环的声音片段
    }
    公共无效播放(){
        clip.setFramePosition(0); //必须始终快退!
        clip.start();
    }
    公共无效循环(){
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    公共无效停止(){
            clip.stop();
        }
    }

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

解决方案

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天全站免登陆