Android uses Mediarecorder recording to call the Stop () method when the error "turn"

Source: Internet
Author: User

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
    1. <strong><span style="FONT-SIZE:18PX;"  >mr=new Mediarecorder ();
    2. Mr.setaudiosource (MediaRecorder.AudioSource.MIC);
    3. Mr.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
    4. Mr.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
    5. Mr.setmaxduration (10000);
    6. Mr.setoutputfile (Environment.getexternalstoragedirectory (). GetPath () +file.separator+"fisii77s.3gp");
    7. Mr.prepare ();
    8. 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
  1. <strong><span style="FONT-SIZE:18PX;" > public void Start (context context, String name) {
  2. if (! Environment.getexternalstoragestate (). Equals (
  3. Android.os.Environment.MEDIA_MOUNTED)) {
  4. return;
  5. }
  6. if (Mrecorder = = null) {
  7. Mrecorder = new Mediarecorder ();
  8. Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
  9. Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
  10. Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
  11. Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
  12. try {
  13. Mrecorder.prepare ();
  14. Mrecorder.start ();
  15. MEMA = 0.0;
  16. } catch (IllegalStateException e) {
  17. System.out.print (E.getmessage ());
  18. } catch (IOException e) {
  19. System.out.print (E.getmessage ());
  20. }
  21. }
  22. }</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
  1. <strong><span style="FONT-SIZE:18PX;" > public void Start (context context, String name) {
  2. if (! Environment.getexternalstoragestate (). Equals (
  3. Android.os.Environment.MEDIA_MOUNTED)) {
  4. return;
  5. }
  6. if (Mrecorder = = null) {
  7. Mrecorder = new Mediarecorder ();
  8. Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
  9. Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
  10. Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
  11. Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
  12. try {
  13. Mrecorder.prepare ();
  14. Mrecorder.start ();
  15. MEMA = 0.0;
  16. } catch (IllegalStateException e) {
  17. System.out.print (E.getmessage ());
  18. } catch (IOException e) {
  19. System.out.print (E.getmessage ());
  20. }
  21. }else{
  22. Stop ();
  23. Mrecorder = new Mediarecorder ();
  24. Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
  25. Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
  26. Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
  27. Mrecorder.setoutputfile (Utilsforchat.getmusicfilepath (context, name));
  28. try {
  29. Mrecorder.prepare ();
  30. } catch (IllegalStateException | IOException e) {
  31. //TODO auto-generated catch block
  32. E.printstacktrace ();
  33. }
  34. Mrecorder.start ();
  35. }
  36. }</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
  1. <strong><span style="FONT-SIZE:18PX;" > public void Stop () {
  2. if (mrecorder! = null) {
  3. try {
  4. Mrecorder.stop ();
  5. } catch (IllegalStateException e) {
  6. //TODO If the current Java state and JNI state are inconsistent,
  7. //e.printstacktrace ();
  8. Mrecorder = null;
  9. Mrecorder = new Mediarecorder ();
  10. }
  11. Mrecorder.release ();
  12. Mrecorder = null;
  13. }
  14. }</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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.