0

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 :

  1. My if condition is guaranteed to cause the loop to break after some iterations.

  2. 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!

Community
  • 1
  • 1
akshay7692
  • 601
  • 1
  • 8
  • 19
  • If an exception is caught, you go straight to the catch block and you can forget your loop. What exception are you catching? Can you Log.e("TAG", "err", e); in catch? – VM4 Jan 04 '14 at 13:16
  • are you using a lot of memory, or recursion? – thumbmunkeys Jan 04 '14 at 13:16
  • 1
    "the app crashes" - that suggests there will be an exception in the log. What is it? – Jon Skeet Jan 04 '14 at 13:18
  • @VM : The loop body has read() and write() operations. – akshay7692 Jan 04 '14 at 13:20
  • @thumbmunkeys: no just a read and write statement. – akshay7692 Jan 04 '14 at 13:21
  • The posted code is probably OK, the crash must be caused by something else. Try to collect information (crash report, unhandled exception, ...) and/or post more code. – H H Jan 04 '14 at 13:22
  • @jonSkeet: will report exception soon but is it related to infinite loop?? because sometimes app closes without showing any error. – akshay7692 Jan 04 '14 at 13:24
  • @akshay7692: Without the exception, it's impossible to tell. I would expect there to *always* be a log entry if the app is actually crashing. – Jon Skeet Jan 04 '14 at 13:26
  • Looks OK to me too. All my threads, in all languages, look like that at the outermost level, (with a logger call in the exception block). TBH, I often have another while loop too, outside the exception, that just 'restarts'' the thread code. This can lead to logs full of 'Access Violation' messages, but a 'working' app:) – Martin James Jan 04 '14 at 14:49
  • Actually I learnt that since my app records audio from MIC ,I cant use emulator to test the app. So no way to have log entry for exceptions! @JonSkeet – akshay7692 Jan 05 '14 at 11:54
  • @HenkHolterman : okeys....code added. – akshay7692 Jan 05 '14 at 14:21

1 Answers1

0

You should use printStackTrace() method in your catch block, it will display the Exception generated on which line of code !

Try using this in your catch block :

e.printStackTrace();

Or you can insert breakpoint & debug the code to see its behaviour

Or you can also use Log & see the values of variables in Logcat

Community
  • 1
  • 1
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
  • 4
    As a hint, answers using full words are much more pleasant to read than txtspeak... So "you" instead of "u" etc. – Jon Skeet Jan 04 '14 at 13:51
  • @VivekWarde : Yes, Thats fine but I cant use emulator since my app requires hardware resources to record and play sound. – akshay7692 Jan 05 '14 at 12:01
  • @akshay7692 Ok you maybe using a device but in your code you havent handle the exception & i think you havent debug ur code , atleast determine that at which line of code the app is crashing ! – Vivek Warde Jan 05 '14 at 16:52
  • @VivekWarde : Well, every time I debug my code in Eclipse, emulator is launched. Is there any way to debug it without starting emulator?? – akshay7692 Jan 05 '14 at 17:49
  • A Device can be used & for more info read this http://developer.android.com/tools/debugging/index.html – Vivek Warde Jan 06 '14 at 12:56
  • I don't think this answers the problem in the question. It only tells how the OP can give more details in order for others to actually answer. – davidcesarino Jan 09 '14 at 03:31
  • This answer was posted before the Question was edited ! At that time the code was not given by the Asker... – Vivek Warde Jan 09 '14 at 12:14
  • @VivekWarde : Thanks for the link! I connected my device to PC, debugged the code and now the code is running fine. Thanks all the guys for replying! – akshay7692 Jan 11 '14 at 17:18