Setting up a MediaRecorder as a simple Audio recorder, my "recorderName.prepare()" is being showing by Eclipse as an "Unhandled IOException".
If I put in place some error exception the program runs but obviously the .prepare statement doesn't work.
As far as I can make out I have declared everything for the MediaRecorder:
AudioSource OutPutFormat Encoder OutputFile
I have also declared RECORD_AUDIO and WRITE_EXTERNAL_STORAGE permissions in the manifest.
import android.media.MediaRecorder;
private MediaRecorder mRec; //Gobal
.
.
.
public void StartRec()
{
mRec = new MediaRecorder();
mRec.setAudioSource(MediaRecorder.AudioSource.MIC);
mRec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRec.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.3gp");
mRec.prepare(); //Underlined as an Unhandled Exception
mRec.start();
}
Eclipse recommends surrounding it a try and catch or adding a declaration to the method.
As for a stack trace, which is produced from the emulator?, I cannot compile the application.
Also please bear in mind I have been at this for less than two weeks.
This question may be acheingly easy, but considering I have been only just started out on Android I feel justified in opting out and asking for help :(
Thank You
that prepare()
function can throw an IOException
, and therefore Java dictates that you should always make sure that exception is 'caught', or thrown.
Eclipse is warning you that you are not catching this, and the quickest way is to do as Eclipse is suggesting
try{
mRec.prepare(); //Underlined as an Unhandled Exception
}catch (IOException e){
Log.e("yourTag","There was a problem: ".e->toString());
}
This only means that if there is an error, you catch it and do error reporting. Now you say this:
*If I put in place some error exception the program runs but obviously the .prepare statement doesn't work. *
But is that really that obvious? If you are generating an error, you should try and fix that, but if you are not (like you seem to imply?) then the code will just work. Try-catch doesn't prevent the prepare
from running, it only gives you an option for the case (this should be an exceptional case, like for instance the file could not be used, found or opened) that there is a problem.
(also, check out this link: oracle java manual for some more info on exceptions )