Java Sound Program(Capture And Playback) 3 from developer.com
来源:互联网 发布:网页三剑客软件 编辑:程序博客网 时间:2024/05/17 03:38
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
public class AudioCapture01 extends JFrame{
boolean stopCapture=false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
public static void main(String[] args){
new AudioCapture01();
}
public AudioCapture01(){
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{
audioFormat=getAudioFormat();
DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread=new Thread(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 Thread(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);
}
}
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);
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
public class AudioCapture01 extends JFrame{
boolean stopCapture=false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
public static void main(String[] args){
new AudioCapture01();
}
public AudioCapture01(){
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{
audioFormat=getAudioFormat();
DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread=new Thread(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 Thread(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);
}
}
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);
}
}
}
}
- Java Sound Program(Capture And Playback) 3 from developer.com
- Java Sound Program( Capture And Playback) from developer.com
- Java Sound Program(Capture And Playback) 2 from developer.com
- Java Sound Program( Capture And Stop) from developer.com
- Java event capture and playback
- Skype audio playback and capture problem on ubuntu 8.10
- Calling Java program from C#
- java console program demo quote from Stanford cs106a, "the art and science of java"
- Capture Image from Camera and Display in Activity
- java sound
- 25 Core Java Questions and Answers from Job Interviews Software developer
- Debugging PL-SQL calls from Java Session Using Eclipse and SQL Developer
- Linux sound and SCSI
- safe and sound
- capture from the camera
- iPhone Developer Program
- iOS Developer Program
- Multimedia and Camera - Media Playback
- drupaluser
- mem
- MFC文档/视图架构之相互关系(一)
- DWR如何配置到web应用中三步骤
- LCS 两个字符串的最大公共子字符串
- Java Sound Program(Capture And Playback) 3 from developer.com
- 弹出式菜单
- 日语口语常用语
- ORA-00600: internal error code, arguments: [keltnfy-ldmInit], [46], [1], [], [], [], [], []
- JSP中include指令和include行为的区别
- Oracle中ORA-06508错误解决,触发器调用存储过程
- 可伸缩性的最差实践
- mysql数据库
- FOREIGN KEY 约束