java 声音

1.从MIC读入声音
//8000,11025,16000,22050,44100 具有此格式的声音每秒播放和录制的帧数
        float sampleRate = 16000.0F;
        //8,16 存储每个快照的位数
        int sampleSizeInBits = 16;
        //1,2 单声道 立体声
        int channels =1;
        //true,false
        boolean signed = true;
        //true,false 字节序
        boolean bigEndian = false;
        audioFormat = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
        //STEP2:受数据行支持的音频格式   其内部缓冲区的最小和最大大小
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
       
        //STEP3:从系统取样音频系统资源的入口点 音频捕获设备获取其数据的数据行
        TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
        //STEP4:打开
        targetDataLine.open(audioFormat);
        //STEP5:开始
        targetDataLine.start();
    
        bos = new ByteArrayOutputStream();
        int size = 0;
        boolean isContinue = true;
        int count = 0;
        long startTime = System.currentTimeMillis();
        while (isContinue)
        {
            int readed = targetDataLine.read(temps, 0, temps.length);
            if (readed > 0)
            {
                bos.write(temps, 0, readed);
                size += readed;
            }
            System.out.println((System.currentTimeMillis() - startTime) + ":" + size + "  count: " + count + " "
                + readed);
            if (count++ > 10)
            {
                isContinue = false;
            }
        }
        bos.close();
        System.out.println("录音结束");


2.播放声音 (中间含保存)
byte[] audioData = bos.toByteArray();
        System.out.println(audioData.length);
        InputStream bis = new ByteArrayInputStream(audioData);
        audioInputStream = new AudioInputStream(bis, audioFormat, audioData.length / audioFormat.getFrameSize());
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        int readed = 0;
        long startTime = System.currentTimeMillis();
        int count = 0;
       
        //保存
        File file = new File("D:/test1.wav");
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, file);
          
        audioInputStream.reset();
        while ((readed = audioInputStream.read(temps, 0, temps.length)) != -1)
        {
            System.out.println((System.currentTimeMillis() - startTime) + "  count: " + count + " " + readed);
            sourceDataLine.write(temps, 0, readed);
            count++;
        }
        sourceDataLine.drain();
        sourceDataLine.close();

3.从本地读取并播放
File f = new File("F:/workspace/test/sound/hero-attack1.wav");
        AudioInputStream audioStream = AudioSystem.getAudioInputStream(f);
        AudioFileFormat format = AudioSystem.getAudioFileFormat(f);
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format.getFormat());
        sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(format.getFormat());
        sourceDataLine.start();
        int readed = 0;
        while ((readed = audioStream.read(temps, 0, temps.length)) != -1)
        {
            sourceDataLine.write(temps, 0, readed);
        }
        sourceDataLine.drain();
        sourceDataLine.close();

你可能感兴趣的:(java,F#)