简体   繁体   English

Windows 7上的Java Sound API

[英]Java Sound API on Windows 7

I found this TestCode at http://www.tutorials.de/threads/java-einen-sound-abspielen.17178/ It's working on OSX but not on my PC Win7. 我在http://www.tutorials.de/threads/java-einen-sound-abspielen.17178/上找到了此TestCode 它可以在OSX上运行,但不能在我的PC Win7上运行。 Are there any issues with the Sound API on Win7? Win7上的Sound API是否有任何问题? I have several Sound Devices installed (integerated and FireWire AudioInterface) and tried both and heared nothing. 我安装了几个声音设备(集成和FireWire AudioInterface),但都尝试了两次,却听不到任何声音。

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

/*
 * SoundTest.java
 *
 * Created on 1. August 2003, 21:06
 */

/**
 *
 * @author  Administrator
 */
public class SoundTest {

    /** Creates a new instance of SoundTest */
    public SoundTest() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sample.wav"));
            AudioFormat af     = audioInputStream.getFormat();
            int size      = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio       = new byte[size];
            DataLine.Info info      = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);

           // for(int i=0; i < 32; i++) {
                Clip clip = (Clip) AudioSystem.getLine(info);
                clip.open(af, audio, 0, size);
                clip.start();
           // }
        }catch(Exception e){ e.printStackTrace(); }

    }

}

Thanks for replies! 感谢您的答复!

J Ĵ

If you are using Java 7 I recommend trying the JavaFX APIs. 如果您使用的是Java 7,建议您尝试使用JavaFX API。 They are way more advanced and compatible across platforms. 它们更加先进,并且跨平台兼容。 You are not limited to the web when using JavaFX, before anyone asks. 在任何人要求之前,使用JavaFX时,您不仅限于网络。

You took a bad example, thats mixing up code from two different approaches (reading the audiostream yourself versus letting clip handle the stream). 您举了一个不好的例子,那就是混合了两种不同方法的代码(自己读取音频流与让剪辑处理流)。

The top answer here How can I play sound in Java? 这里的最佳答案如何用Java播放声音? shows how to play via Clip. 显示如何通过Clip播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM