My thread's run() method has while loop inside a try-catch block as follows:
try{
while(true){
// some code here
if(condition)
break;
else
//more code here
}
}catch(Exception e){..}
I guess the code is not getting into infinite loop since :
My if condition is guaranteed to cause the loop to break after some iterations.
Since the catch block is outside while loop, any exception inside while loop will cause the loop to break.
As soon as I start the thread,the app crashes.
I have gone through this post but still its unclear whats wrong with above code.
Here is my complete run method :
private static final int AUDIO_SOURCE=MediaRecorder.AudioSource.MIC;
private static final int SAMPLE_RATE_IN_HZ=44100;
private static final int CHANNEL_CONFIG=AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_FORMAT=AudioFormat.ENCODING_PCM_16BIT;
public void run() {
//writing to AudioTrack object using 512kB buffer
int buffSize=512*1024; //512kb = 512*1024 B
byte[] buff=new byte[buffSize];
int fileSize=(int)outputFile.length(); //outputFile=.PCM file
int bytesRead=0,readCount=0;
FileInputStream fin=null;
try {
fin = new FileInputStream(outputFile);
}catch(Exception e){}
int TrackBufferSizeInBytes=android.media.AudioTrack.getMinBufferSize(SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG,AUDIO_FORMAT);
//create AudioTrack object
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,SAMPLE_RATE_IN_HZ,CHANNEL_CONFIG,
AUDIO_FORMAT, TrackBufferSizeInBytes, AudioTrack.MODE_STREAM);
at.play();
try{
while(bytesRead<fileSize){
readCount=fin.read(buff,0,buffSize);
if(readCount==(-1)) // if EOF is reached
break;
else{
at.write(buff, 0, readCount); //write read bytes to Track
bytesRead+=readCount;
}
}
at.stop();
at.release();
at=null;
fin.close();
}catch(Exception e){}
}
Please help me.Thanks in advance!