Java Sound Program(Capture And Playback) 2 from developer.com

import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;
import java.awt.*;
import java.awt.event.*;

public class AudioCapture02 extends JFrame{
    boolean stopCapture=false;
    ByteArrayOutputStream byteArrayOutputStream;
    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    AudioInputStream audioInputStream;
    SourceDataLine sourceDataLine;
    
    public static void main(String[] args){
        new AudioCapture02();
    }
    
    public AudioCapture02(){
        final JButton captureBtn=new JButton("Capture");
        final JButton stopBtn=new JButton("Stop");
        final JButton playBtn=new JButton("Playback");
        
        captureBtn.setEnabled(true);
        stopBtn.setEnabled(false);
        playBtn.setEnabled(false);
        
        captureBtn.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        captureBtn.setEnabled(false);
                        stopBtn.setEnabled(true);
                        playBtn.setEnabled(false);
                        captureAudio();
                    }
                }
            );
        getContentPane().add(captureBtn);
        
        stopBtn.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        captureBtn.setEnabled(true);
                        stopBtn.setEnabled(false);
                        playBtn.setEnabled(true);
                        
                        stopCapture=true;
                    }
                }
            );
        getContentPane().add(stopBtn);
        
        playBtn.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        playAudio();
                    }
                }
            );
        getContentPane().add(playBtn);
        
        getContentPane().setLayout(new FlowLayout());
        setTitle("Capture/Playback Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(250,70);
        setVisible(true);            
    }
    
    private void captureAudio(){
        try{
            Mixer.Info[] mixerInfo=AudioSystem.getMixerInfo();
            System.out.println("Available mixers:");
            for(int cnt=0;cnt<mixerInfo.length;cnt++){
                System.out.println(mixerInfo[cnt].getName());
            }
            audioFormat=getAudioFormat();
            
            DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
            
            Mixer mixer=AudioSystem.getMixer(mixerInfo[3]);
            targetDataLine=(TargetDataLine)mixer.getLine(dataLineInfo);
            targetDataLine.open(audioFormat);
            targetDataLine.start();
            
            Thread captureThread=new CaptureThread();
            captureThread.start();
        }catch(Exception e){
            System.out.println(e);
            System.exit(0);
        }
    }
    
    private void playAudio(){
        try{
            byte audioData[]=byteArrayOutputStream.toByteArray();
            InputStream byteArrayInputStream=new ByteArrayInputStream(audioData);
            AudioFormat audioFormat=getAudioFormat();
            audioInputStream=new AudioInputStream(byteArrayInputStream,audioFormat,audioData.length/audioFormat.getFrameSize());
            DataLine.Info dataLineInfo=new DataLine.Info(SourceDataLine.class,audioFormat);
            
            sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();
            
            Thread playThread=new PlayThread();
            playThread.start();
        }catch(Exception e){
            System.out.println(e);
            System.exit(0);
        }
    }
    
    private AudioFormat getAudioFormat(){
        float sampleRate=8000.0F;
        int sampleSizeInBits=16;
        int channels=1;
        boolean signed=true;
        boolean bigEndian=false;
        
        return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);    
    }
    
    class CaptureThread extends Thread{
        byte tempBuffer[]=new byte[10000];
        public void run(){
            byteArrayOutputStream=new ByteArrayOutputStream();
            stopCapture=false;
            try{
                while(!stopCapture){
                    int cnt=targetDataLine.read(tempBuffer,0,tempBuffer.length);
                    if(cnt>0){
                        byteArrayOutputStream.write(tempBuffer,0,cnt);
                    }
                }
                byteArrayOutputStream.close();
            }catch(Exception e){
                System.out.println(e);
                System.exit(0);
            }
        }
    }
    
    class PlayThread extends Thread{
        byte tempBuffer[]=new byte[10000];
        
        public void run(){
            try{
                int cnt;
                while((cnt=audioInputStream.read(tempBuffer,0,tempBuffer.length))!=-1){
                    if(cnt>0){
                        sourceDataLine.write(tempBuffer, 0, cnt);
                    }
                }
                sourceDataLine.drain();
                sourceDataLine.close();
            }catch(Exception e){
                System.out.println(e);
                System.exit(0);
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值