Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Sound Player which save Mic sound

Sat Jun 09, 2007 1:46 pm

This program save the sound as an input from your MIC and then you can play the sound again .
It is so useful specially and i is implemented in Java.So any one who is interested in recording speech and working on it will find it so useful or even those what to knew how to run sound in Java.This Class Contain the most classes in the Javax.sound.*; library .
java code
/*
* SoundPlayer.java
*
* Created on October 5, 2006, 3:54 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

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

public class SoundPlayer extends JFrame {

protected boolean running;
ByteArrayOutputStream out;

public SoundPlayer() {
super("Capture Sound Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container content = getContentPane();

final JButton capture = new JButton("Capture");
final JButton stop = new JButton("Stop");
final JButton play = new JButton("Play");

capture.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(false);

ActionListener captureListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
capture.setEnabled(false);
stop.setEnabled(true);
play.setEnabled(false);
captureAudio();
}
};
capture.addActionListener(captureListener);
content.add(capture, BorderLayout.NORTH);

ActionListener stopListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
capture.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
running = false;
}
};
stop.addActionListener(stopListener);
content.add(stop, BorderLayout.CENTER);

ActionListener playListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
playAudio();
}
};
play.addActionListener(playListener);
content.add(play, BorderLayout.SOUTH);
}

private void captureAudio() {
try {
final AudioFormat format = getFormat();
DataLine.Info info = new DataLine.Info(
TargetDataLine.class, format);
final TargetDataLine line = (TargetDataLine)
AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int)format.getSampleRate()
* format.getFrameSize();
byte buffer[] = new byte[bufferSize];

public void run() {
out = new ByteArrayOutputStream();
running = true;
try {
while (running) {
int count =
line.read(buffer, 0, buffer.length);
if (count > 0) {
out.write(buffer, 0, count);
}
}
out.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-1);
}
}
};
Thread captureThread = new Thread(runner);
captureThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
}
}

private void playAudio() {
try {
byte audio[] = out.toByteArray();
InputStream input =
new ByteArrayInputStream(audio);
final AudioFormat format = getFormat();
final AudioInputStream ais =
new AudioInputStream(input, format,
audio.length / format.getFrameSize());
DataLine.Info info = new DataLine.Info(
SourceDataLine.class, format);
final SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(format);
line.start();

Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate()
* format.getFrameSize();
byte buffer[] = new byte[bufferSize];

public void run() {
try {
int count;
while ((count = ais.read(
buffer, 0, buffer.length)) != -1) {
if (count > 0) {
line.write(buffer, 0, count);
}
}
line.drain();
line.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-3);
}
}
};
Thread playThread = new Thread(runner);
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
}
}

private AudioFormat getFormat() {
float sampleRate = 8000;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = true;
return new AudioFormat(sampleRate,
sampleSizeInBits, channels, signed, bigEndian);
}

public static void main(String args[]) {
JFrame frame = new SoundPlayer();
frame.pack();
frame.show();
}
}




Re: Sound Player which save Mic sound

Mon Jan 21, 2013 2:02 am

updated.

Post a reply
  Related Posts  to : Sound Player which save Mic sound
 Sound Level Meter     -  
 add sound to your animations in flash     -  
 play a sound when the user enters the area- audio feedback     -  
 [Java][C#][Skype][WinAPI] simple plugin for Skype, sound red     -  
 How to burn MP4/MPEG4 to DVD playable on a DVD player?     -  
 Save Image to a file in ITK     -  
 How to save a search fields...in Struts2.0     -  
 How to download and save .mov files from this website ?     -  
 error closing file matlab save     -  
 Chat client and server, save messages and delete     -  

Topic Tags

Java Sound