Audio recorder Freezes on Win2000/WinXP in while loop
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amlan_Roy
Posted On:   Friday, February 20, 2004 03:26 AM

Hi, I am trying to create Audio file using following code but its freezing after some time means after creating some files. BRIEF : I am running this code in while loop where invocing constructor and writing file some time its running successfully and creating all files but when running same code twice or thrice then it is freezing WITHOUT ERROR. while opeing target data line. Can anybody provide some solution Thanks ================================================================================= import java.io.IOException; import java.io.File; import javax.sound.sampled.DataLine; import javax.sound.sampled.TargetDataLine; import javax.sound.sampled.AudioFormat;    More>>

Hi,

I am trying to create Audio file using following code but its freezing after some time means after creating some files.

BRIEF : I am running this code in while loop where invocing constructor and writing file some time its running successfully and creating all files but when running same code twice or thrice then it is freezing WITHOUT ERROR. while opeing target data line.
Can anybody provide some solution

Thanks
=================================================================================
import java.io.IOException;
import java.io.File;

import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFileFormat;


public class AudioRecorder extends Thread {

private TargetDataLine m_line;
private AudioFileFormat.Type m_targetType;
private AudioInputStream m_audioInputStream;
private File m_outputFile;
private boolean m_bRecording;
private String strFilename;
private boolean status;

public AudioRecorder(String fileName) {

status = false;
strFilename = fileName;
File outputFile = new File(strFilename);

boolean isFileExist = outputFile.exists();

if(isFileExist) {

boolean isFileDelete = false;

while( ! isFileDelete ){
isFileDelete = outputFile.delete();
}

outputFile = new File(strFilename);
}

AudioFormat audioFormat = null;

// 8 kHz, 8 bit, mono
//audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, 8, 1, 1, 8000.0F, true);

// 44.1 kHz, 16 bit, stereo
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);


DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try{
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}catch (LineUnavailableException e){
System.out.println("unable to get a recording line");
e.printStackTrace();
System.exit(1);
}

AudioFileFormat.Type targetType = AudioFileFormat.Type.AU;

m_line = targetDataLine;
m_audioInputStream = new AudioInputStream(targetDataLine);
m_targetType = targetType;
m_outputFile = outputFile;
}

public void start(){
//setPriority(Thread.MAX_PRIORITY);
status = false;
m_line.start();
super.start();
//System.out.println("Finished start");
}

public void stopRecording(){
m_line.stop();
m_line.close();
m_bRecording = false;
//System.out.println("Finished Recording");
}

public synchronized void setStatus(){
status = true;
notify();
}

public void run(){
//System.out.println("Start Run");
try{
AudioSystem.write(
m_audioInputStream,
m_targetType,
m_outputFile);
System.out.println("after write()");
}catch (IOException e){
e.printStackTrace();
}
//System.out.println("Finished Run");
}

private static void printUsageAndExit(){
System.out.println("SimpleAudioRecorder: usage:");
System.out.println(" java SimpleAudioRecorder ");
System.exit(0);
}
}


==================================================

   <<Less
About | Sitemap | Contact