Wayss_S 2015-01-30 17:08 采纳率: 0%
浏览 830

Java多线程中产生声音的问题

这个我的声音类,

public class Sound extends JApplet{

boolean Baozha = false,Fire = false,Move = false;

AudioClip tankMove = java.applet.Applet.newAudioClip(Sound.class.getClassLoader().getResource("sound/myTankMove.wav"));
AudioClip tankFire = java.applet.Applet.newAudioClip(Sound.class.getClassLoader().getResource("sound/tankFire.wav"));
AudioClip tankBaozha = java.applet.Applet.newAudioClip(Sound.class.getClassLoader().getResource("sound/tankBaozha.wav"));

public void tankBaozha(){
    Baozha = true;
    new Thread(new SoundThread()).start();
    Baozha = false;
}

public void fireZidan(){
    Fire = true;
    new Thread(new SoundThread()).start();
    Fire = false;
}

public void tankMove(){
    Move = true;
    new Thread(new SoundThread()).start();
}

private class SoundThread implements Runnable{
    public void run(){
        if(Baozha){
            tankBaozha.play();
        }
        if(Fire){
            tankFire.play();
        }
        if(Move){
            tankMove.play();
        }
    }
}

}
在其他类中的调用形式为:
Sound aaa = new Sound();
aaa.fireZidan();
但是我发现声音不能同步出现,我不明白这是什么原因,另外,我还想请教如何解决用单独的线程产生声音。

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 08:27
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在 Java 中,线程是通过调用 run() 方法来执行特定任务的。在这个例子中,你的 SoundThread 类并没有实现 Runnable 接口,因此无法直接使用 Thread 对象创建新线程。要使声音播放,你需要让线程在 run() 方法中直接调用 play() 方法或播放音频文件。

    以下是一个简单的示例,展示了如何将声音播放到一个 SoundThread 实例中:

    import javax.sound.sampled.*;
    import java.awt.*;
    
    public class Sound extends JApplet {
        private static final int BUFFER_SIZE = 1024 * 5;
        private AudioInputStream audioInputStream;
        private Clip clip;
        private byte[] buffer = new byte[BUFFER_SIZE];
    
        public void init() {
            try {
                // Load the sound file into an AudioInputStream
                audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/sound/myTankMove.wav"));
    
                // Create a DataLine for recording and playback of audio
                DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioInputStream.getFormat());
                targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
    
                // Set up the line for recording
                targetDataLine.open(audioInputStream.getFormat());
    
                // Start recording
                targetDataLine.start();
    
                // Get the number of bytes in the buffer
                long bufferSizeInBytes = targetDataLine.getByteSizeInBytes(0);
                System.out.println("Buffer size: " + bufferSizeInBytes);
    
                // Initialize the loop to play the sound at the correct time
                long startTime = System.currentTimeMillis();
                while (true) {
                    // Read from the buffer until we have enough bytes to fill the line
                    while (targetDataLine.read(buffer, 0, bufferSizeInBytes) != -1) {
                        // Play the sound
                        clip.drain();
                        clip.write(buffer, 0, bufferSizeInBytes);
                    }
    
                    // Check if the sound is still playing after the last read
                    if (!clip.isRunning()) {
                        // Stop the sound if it's not currently playing
                        clip.stop();
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void paint(Graphics g) {
            super.paint(g);
            g.drawString("Hello World!", 10, 10);
        }
    }
    

    这段代码首先加载了音频文件,并将其转换为 AudioInputStream。然后,它创建了一个 TargetDataLine,用于录制和播放音频。最后,它设置了循环以按正确的顺序播放音频。

    注意:这段代码只是一个基本示例,实际应用中可能需要根据具体需求进行调整。例如,你可能需要处理音频流的错误情况、控制音量等。

    评论

报告相同问题?