【问题标题】:how to play wav file in java 1.4如何在 java 1.4 中播放 wav 文件
【发布时间】:2011-04-18 18:10:07
【问题描述】:

作为标题 如何在 java v1.4 中重复播放声音文件?

【问题讨论】:

  • 人们还在使用 Java 1.4?并为它写new代码?它已经停产多年了!
  • @Joachim:确实如此。很多客户端都安装在“如果可以,不要更改,只需添加新模块”的位置。我肯定知道。
  • @Tomás:这是一个非常不负责任的立场。仅此类设置带来的安全问题就可能比任何升级都花费更多。
  • @Joachim:我同意。但客户付费和指挥。
  • 这已经很老了,但无论如何。有时你必须处理过时的系统,它只是发生了。就我而言,是供我个人使用的(没有客户支付或强制要求任何东西)。

标签: java audio java1.4


【解决方案1】:

如果您只想播放 wav 文件,那么“org.life.java”的答案是正确的。对于其他格式类型,您可以使用 JMF(http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html)。

注意: JMF 现在已经过时了...但它可以与 jdk 1.4 一起使用

【讨论】:

    【解决方案2】:
    import java.net.URL;
    import javax.sound.sampled.*;
    
    public class LoopSound {
    
      public static void main(String[] args) throws Exception {
        URL url = new URL(
          "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
          getAudioInputStream( url );
        clip.open(ais);
        clip.loop(0);
        javax.swing.JOptionPane.
          showMessageDialog(null, "Close to exit!");
      }
    } 
    

    【讨论】:

    • AudioSystem.getClip() 在 JDK 1.4 中不存在
    【解决方案3】:

    这适用于 JDK 1.4(在 Windows XP 和 JDK 1.4.2_06 中测试)。 另一个答案失败了,因为正如 cmets 中正确说明的那样,AudioSystem.getClip() 在 JDK 1.4 上不存在。下面是一个完整的源代码(以 main 函数的形式,但它适用于其他任何东西),它使用 DataLine 并在单独的线程中播放以获得更好的整体性能:

    import java.io.File;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.SourceDataLine;
    
    public class AudioTest {
    
      public static void main(String[] args) throws Exception {
        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("C:/sound1.wav"));
    
        AudioFormat format = ais.getFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    
        class PlayThread extends Thread {
          private AudioInputStream ais;
          private AudioFormat format;
          private SourceDataLine sourceDataLine;
    
          byte tempBuffer[] = new byte[10000];
    
          public PlayThread(AudioInputStream ais, SourceDataLine sourceDataLine, AudioFormat format) {
            this.ais = ais;
            this.sourceDataLine = sourceDataLine;
            this.format = format;
          }
    
          public void run() {
            try {
              sourceDataLine.open(this.format);
              sourceDataLine.start();
    
              int cnt;
              while ((cnt = this.ais.read(tempBuffer, 0, tempBuffer.length)) != -1) {
                if (cnt > 0) {
                  sourceDataLine.write(tempBuffer, 0, cnt);
                }
              }
    
              sourceDataLine.drain();
              sourceDataLine.close();
    
            } catch (Exception e) {
              throw new RuntimeException(e);
            }
          }
        }
    
        new PlayThread(ais, sourceDataLine, format).start();
    
      }
    }
    

    问题和答案都很老了,但我只需要在只运行 Windows XP 的无风扇迷你 PC 上完成这项工作...¯\_(ツ)_/¯

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 2011-01-25
      • 2018-08-23
      • 1970-01-01
      • 2010-09-19
      • 2013-07-13
      • 1970-01-01
      相关资源
      最近更新 更多