zoukankan      html  css  js  c++  java
  • java播放wav文件

    import java.io.File;
    import java.io.IOException;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine.Info;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    public class PlayWav {
        
        public PlayWav(String file) {
            try {
                // read wav file to audio stream
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(file));
                // read audio format from stream
                AudioFormat audioFormat = audioInputStream.getFormat();
                System.out.println("采样率:" + audioFormat.getSampleRate());
                System.out.println("总帧数:" + audioInputStream.getFrameLength());
                System.out.println("时长(秒):" + audioInputStream.getFrameLength() / audioFormat.getSampleRate());
                // SourceDataLine info
                Info dataLineInfo = new Info(SourceDataLine.class, audioFormat);
    
                SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
                byte[] b = new byte[1024];
                int len = 0;    
                sourceDataLine.open(audioFormat, 1024);
                sourceDataLine.start();
                while ((len = audioInputStream.read(b)) > 0) {
                    sourceDataLine.write(b, 0, len);
                }
                
                audioInputStream.close();
                sourceDataLine.drain();
                sourceDataLine.close();
                
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            }
    
        }
    
        
        public static void main(String[] args) {
            new PlayWav("D:\123.wav");
    
        }
    }

    import java.io.File;import java.io.IOException;
    import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine.Info;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.UnsupportedAudioFileException;
    public class PlayWav {public PlayWav(String file) {try {// read wav file to audio streamAudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(file));// read audio format from streamAudioFormat audioFormat = audioInputStream.getFormat();System.out.println("采样率:" + audioFormat.getSampleRate());System.out.println("总帧数:" + audioInputStream.getFrameLength());System.out.println("时长(秒):" + audioInputStream.getFrameLength() / audioFormat.getSampleRate());// SourceDataLine infoInfo dataLineInfo = new Info(SourceDataLine.class, audioFormat);
    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);byte[] b = new byte[1024];int len = 0;sourceDataLine.open(audioFormat, 1024);sourceDataLine.start();while ((len = audioInputStream.read(b)) > 0) {sourceDataLine.write(b, 0, len);}audioInputStream.close();sourceDataLine.drain();sourceDataLine.close();} catch (UnsupportedAudioFileException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (LineUnavailableException e) {e.printStackTrace();}
    }
    public static void main(String[] args) {new PlayWav("D:\123.wav");
    }}

  • 相关阅读:
    jQuery 重新温习 遗忘知识点
    正则表达式获取博客园随笔1
    用django创建一个简单的sns
    WCF小实例以及三种宿主
    iOS: imageIO完成渐进加载图片
    Excel 菜单系统
    分布式EventBus的Socket实现
    Jenkins安装plugin
    邮件系统存储设计问答
    在Windows上使用CodeLite+MinGW+Clang进行开发
  • 原文地址:https://www.cnblogs.com/yoyotl/p/8330399.html
Copyright © 2011-2022 走看看