有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java声音类在Windows上听起来分层且刺耳

所以,当我在Mac上时,这个错误没有发生。然而,当我在Windows上时,我多次播放的任何声音开始听起来像是刺耳的声音,并以一种不愉快的方式相互叠加

以下是我的声音课程的相关代码:

public class NewerSound {
    private boolean stop = true;
    private boolean loopable;
    private boolean isUrl;
    private URL fileUrl;
    private Thread sound;
    private double volume = 1.0;

    public NewerSound(URL url, boolean loopable) throws UnsupportedAudioFileException, IOException {
        isUrl = true;
        fileUrl = url;
        this.loopable = loopable;
    }

    public void play() {
        stop = false;
        Runnable r = new Runnable() {
            @Override
            public void run() {
                do {
                    try {
                        AudioInputStream in;
                        if(!isUrl)
                            in = getAudioInputStream(new File(fileName));
                        else
                            in = getAudioInputStream(fileUrl);
                        final AudioFormat outFormat = getOutFormat(in.getFormat());
                        final Info info = new Info(SourceDataLine.class, outFormat);
                        try(final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
                            if(line != null) {
                                line.open(outFormat);
                                line.start();
                                AudioInputStream inputMystream = AudioSystem.getAudioInputStream(outFormat, in);
                                stream(inputMystream, line);
                                line.drain();
                                line.stop();
                            }
                        }
                    }
                    catch(UnsupportedAudioFileException | LineUnavailableException | IOException e) {
                        throw new IllegalStateException(e);
                    }
                } while(loopable && !stop);
            }
        };
        sound = new Thread(r);
        sound.start();
    }

    private AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
    }

    private void stream(AudioInputStream in, SourceDataLine line) throws IOException {
        byte[] buffer = new byte[4];
        for(int n = 0; n != -1 && !stop; n = in.read(buffer, 0, buffer.length)) {
            byte[] bufferTemp = new byte[buffer.length];
            for(int i = 0; i < bufferTemp.length; i += 2) {
                short audioSample = (short) ((short) ((buffer[i + 1] & 0xff) << 8) | (buffer[i] & 0xff));
                audioSample = (short) (audioSample * volume);
                bufferTemp[i] = (byte) audioSample;
                bufferTemp[i + 1] = (byte) (audioSample >> 8);
            }
            buffer = bufferTemp;
            line.write(buffer, 0, n);
        }
    }
}

当我使用NewerSound时,在同一个声音上多次播放时,可能会出现访问相同资源的问题。play()方法

如果需要任何其他细节,请告诉我。非常感谢:)


共 (1) 个答案

  1. # 1 楼答案

    您在方法“stream”中用于更改体积的方法存在缺陷。您有16位编码,因此需要两个字节才能导出单个音频值。在乘法之前,需要将两个字节对中的值组合起来,然后将16位结果分解回两个字节。有很多StackOverflow线程都有这样的代码

    我不知道这是否是你描述的问题的全部原因,但它肯定可能是,而且肯定需要修复