Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-声音偶尔会以非常大且不规则的音量播放_Java_Audio_Javax.sound.sampled - Fatal编程技术网

Java-声音偶尔会以非常大且不规则的音量播放

Java-声音偶尔会以非常大且不规则的音量播放,java,audio,javax.sound.sampled,Java,Audio,Javax.sound.sampled,因此,我正在用Java制作一个游戏,并使用以下代码来播放声音效果(枪声、爆炸声等)。然而,偶尔,声音效果会以非常不规则的音量播放,比我预期的声音大得多,即使它通常以标准音量播放 当我同时播放很多声音时,这种情况更为常见,有时一个声音片段的音量会比其他声音的音量大得多,这种差异非常明显 我已录制了问题的简短音频剪辑: 如果有人能帮助我们弄清楚为什么会发生这种情况,我们将不胜感激 以下是我的声音课程中使用的代码: import java.io.File; import javax.sound.sam

因此,我正在用Java制作一个游戏,并使用以下代码来播放声音效果(枪声、爆炸声等)。然而,偶尔,声音效果会以非常不规则的音量播放,比我预期的声音大得多,即使它通常以标准音量播放

当我同时播放很多声音时,这种情况更为常见,有时一个声音片段的音量会比其他声音的音量大得多,这种差异非常明显

我已录制了问题的简短音频剪辑:

如果有人能帮助我们弄清楚为什么会发生这种情况,我们将不胜感激

以下是我的声音课程中使用的代码:

import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.*;

public class sound{ 
    static File sound;
    static boolean muted = false; // This should explain itself
    static float volume = (float)100.0; // This is the volume that goes from 0 to 100
    static float pan = (float)0.0; // The balance between the speakers 0 is both sides and it goes from -1 to 1
    static double seconds = (double)0.0; // The amount of seconds to wait before the sound starts playing
    static boolean looped_forever = false; // It will keep looping forever if this is true
    static int loop_times = 0; // Set the amount of extra times you want the sound to loop (you don't need to have looped_forever set to true)
    static int loops_done = 0; // When the program is running this is counting the times the sound has looped so it knows when to stop

    final static Runnable playSound = new Runnable(){ //This Thread/Runnabe is for playing the sound
        public void run(){
            try{
                // Check if the audio file is a .wav file
                if (sound.getName().toLowerCase().contains(".wav")){
                    AudioInputStream stream = AudioSystem.getAudioInputStream(sound);

                    AudioFormat format = stream.getFormat();

                    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED){
                        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                                format.getSampleRate(),
                                format.getSampleSizeInBits() * 2,
                                format.getChannels(),
                                format.getFrameSize() * 2,
                                format.getFrameRate(),
                                true);

                        stream = AudioSystem.getAudioInputStream(format, stream);
                    }

                    SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class,stream.getFormat(),(int) (stream.getFrameLength() * format.getFrameSize()));

                    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                    line.open(stream.getFormat());
                    line.start();

                    // Set Volume
                    FloatControl volume_control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
                    volume_control.setValue((float) (Math.log(volume / 100.0f) / Math.log(10.0f) * 20.0f));

                    // Mute
                    BooleanControl mute_control = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
                    mute_control.setValue(muted);

                    FloatControl pan_control = (FloatControl) line.getControl(FloatControl.Type.PAN);
                    pan_control.setValue(pan);

                    long last_update = System.currentTimeMillis();
                    double since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;

                    // Wait the amount of seconds set before continuing
                    while (since_last_update < seconds){
                        since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;
                    }

                    int num_read = 0;
                    byte[] buf = new byte[line.getBufferSize()];

                    while ((num_read = stream.read(buf, 0, buf.length)) >= 0){
                        int offset = 0;

                        while (offset < num_read){
                            offset += line.write(buf, offset, num_read - offset);
                        }
                    }

                    line.drain();
                    line.stop();
                    line.close();

                    if (looped_forever){
                        new Thread(playSound).start();
                    }
                    else if (loops_done < loop_times){
                        loops_done++;
                        new Thread(playSound).start();
                    }
                }
            }
            catch (Exception ex) { ex.printStackTrace(); }
        }
    };
}

只是一种预感,你知道声音是否同时播放了两次吗?让它在你播放音乐时打印出时间戳+声音名称sound@phflack很抱歉反应太晚。是的,我试过了,它似乎播放的声音没有超出预期的数量。我下班回家后会进一步研究这个问题,我需要开始为我正在进行的项目研究播放声音anyway@phflack非常感谢:)运行起来有点困难,没有办法让它播放声音吗?另外,
static Runnable playSound
部分在我看来很可疑,只是直觉,你知道声音是否同时播放了两次吗?让它在你播放音乐时打印出时间戳+声音名称sound@phflack很抱歉反应太晚。是的,我试过了,它似乎播放的声音没有超出预期的数量。我下班回家后会进一步研究这个问题,我需要开始为我正在进行的项目研究播放声音anyway@phflack非常感谢:)运行起来有点困难,没有办法让它播放声音吗?另外,
static Runnable playSound
部分在我看来也很可疑
sound.sound=new File("mySoundFile.wav");
new Thread(sound.playSound).start();