This article was reproduced from: http://blog.csdn.net/u014737138/article/details/49738827
This question on the Internet to see too many answers, has been suggested that according to the official website of the API order, in fact, the method of solving the problem is not so, so can not solve the problem, according to that order to also rice useful
We need to know why it stopped, why it stopped,
There is a conclusion in this: The flashback must be a control value error, in Java is java.lang.NullXXException error
There seems to be no other reason to cause the flashback,
Stop is deleted because the Stop object does not exist, this is understood,
The Stop object does not exist, indicating that the object new failed, this is understood,
The Stop object new fails, so you need to see what new is doing:
[Java]View PlainCopy
- <strong><span style="FONT-SIZE:18PX;" >mr=new Mediarecorder ();
- Mr.setaudiosource (MediaRecorder.AudioSource.MIC);
- Mr.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
- Mr.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
- Mr.setmaxduration (10000);
- Mr.setoutputfile (Environment.getexternalstoragedirectory (). GetPath () +file.separator+"fisii77s.3gp");
- Mr.prepare ();
- Mr.start ();</span></strong>
I believe everyone is doing this, the key is that we have a layer of if (mr==null) judgment in front of the
I wrote this before the error in the code:
[Java]View PlainCopy
- <strong><span style="FONT-SIZE:18PX;" > public void Start (context context, String name) {
- if (! Environment.getexternalstoragestate (). Equals (
- Android.os.Environment.MEDIA_MOUNTED)) {
- return;
- }
- if (Mrecorder = = null) {
- Mrecorder = new Mediarecorder ();
- Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
- Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
- Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
- Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
- try {
- Mrecorder.prepare ();
- Mrecorder.start ();
- MEMA = 0.0;
- } catch (IllegalStateException e) {
- System.out.print (E.getmessage ());
- } catch (IOException e) {
- System.out.print (E.getmessage ());
- }
- }
- }</span></strong>
That is, if the object exists, not destroyed, I do not have to create, according to common sense, it should be judged right, but commonsense is generally wrong,
In Java to determine whether an object is empty indeed = = NULL The key is that ==null is who to judge this seems to be the interview often like this test me here or talk about I have been so understanding of things ah, I still reading, I hope to find a job interview when useful haha
= = This judgment is a comparison of the address values, equals is the content value comparison
= = This address value refers to the heap memory, the value of equals is in the stack memory
After we know the mechanism of NULL judgment, we have to think about whether we can use this object directly if the object still exists, and the test answer is no,
Next, if we were to create a Mediarecorder object for the first time, it would have stopped and released after we had finished recording, otherwise the recording file would have been unsuccessful.
Stop, stop is the Jni object, release, release the JNI inside the object, also released the Java object inside the stack memory value, heap memory is still there, the reference is empty here to understand, the reason is out,
In this case the JNI mechanism if not understand, you can read my last article, the explanation is very detailed
After release, if the second recording is executed immediately, the if code above will need to be judged, it is obvious that if the statement is not executed, because the heap memory is not released, but the objects in the JNI are all released.
This time if you still use this object to record, then the result is you speak to the air, recorded sound also become air, and then you release the finger is the recording object stopped, can not stop, because the Jni object is empty, does not exist, how to stop it, the program directly flash back, the reason is this
The solution is this time. If your object is not empty, you need to recreate it again, mainly to ensure that your recording will not become air,
[Java]View PlainCopy
- <strong><span style="FONT-SIZE:18PX;" > public void Start (context context, String name) {
- if (! Environment.getexternalstoragestate (). Equals (
- Android.os.Environment.MEDIA_MOUNTED)) {
- return;
- }
- if (Mrecorder = = null) {
- Mrecorder = new Mediarecorder ();
- Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
- Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
- Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
- Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
- try {
- Mrecorder.prepare ();
- Mrecorder.start ();
- MEMA = 0.0;
- } catch (IllegalStateException e) {
- System.out.print (E.getmessage ());
- } catch (IOException e) {
- System.out.print (E.getmessage ());
- }
- }else{
- Stop ();
- Mrecorder = new Mediarecorder ();
- Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
- Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
- Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
- Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
- try {
- Mrecorder.prepare ();
- } catch (IllegalStateException | IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- Mrecorder.start ();
- }
- }</span></strong>
The code becomes this, in fact, here is more simple point is every time not to judge, come in directly to the original stop, and then create can save a lot of code
Then is the Stop function, be sure to write the right, or the error of the OH:
[Java]View PlainCopy
- <strong><span style="FONT-SIZE:18PX;" > public void Stop () {
- if (mrecorder! = null) {
- try {
- Mrecorder.stop ();
- } catch (IllegalStateException e) {
- //TODO If the current Java state and JNI state are inconsistent,
- //e.printstacktrace ();
- Mrecorder = null;
- Mrecorder = new Mediarecorder ();
- }
- Mrecorder.release ();
- Mrecorder = null;
- }
- }</span></strong>
Someone in this is sure to ask, you this inside stop has made an exception throws, why the above creation also add else that code,
This is also the cause of the model and then I finally retained the code because to ensure that you say, the sound of the recording will not become the air ah This is also a very important bug ah
Android uses Mediarecorder recording to call the Stop () method when the error "turn"