26

I am trying to record audio in android but I am facing a problem.

I have start and stop buttons, "start" for starting recording and "stop" for stopping recording.

The problem is, when I press the stop button then my application logs a message "W/MediaRecorder(635): mediarecorder went away with unhandled events". (Start function is saving the audio file properly.)

Then, if I again press start or stop button then I get error message " A/libc(743): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 743 (xample.recorder)"

Code of recording class is below:

  /**
   * Creates a new audio recording at the given path (relative to root of SD card).
   */
  public AudioRecorder(String path) {
    this.path = sanitizePath(path);
  }
  private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
          path = "/" + path;
        }
        if (!path.contains(".")) {
          path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
      }

  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    try{
    recorder.prepare();
    }
    catch(IOException e){
        Log.e("Recorder","Recording failed");
    }
    recorder.start();
  }
  /**
   * Stops a recording that has been previously started.
   */
  public void stop() throws IOException {
    recorder.stop();
    recorder.release();
  }

Code of main activity is below:

  /*
 * */
  public class Recorder extends Activity implements OnClickListener

   {
private static final String TAG="Recorder";
AudioRecorder ar=new AudioRecorder("/TestAudio.3gp");
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recorder);

    final Button start = (Button) this.findViewById(R.id.btn_start);
    start.setOnClickListener(this);


    final Button stop = (Button) this.findViewById(R.id.btn_stop);
    stop.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_recorder, menu);
    return true;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    try{
         switch (v.getId()) {
            case R.id.btn_start:
                ar.start();
                Log.d("Recorder","Recorded");
                Toast.makeText(this, "Controll returned from start function", Toast.LENGTH_LONG).show();              
                break;
            case R.id.btn_stop:
                ar.stop();
                Toast.makeText(this, "Recording stopped; Starting MediaPlayer", Toast.LENGTH_SHORT).show();
                //Toast.makeText(this, "Starting media player", Toast.LENGTH_LONG).show();
                ar.startPlaying();
                //Toast.makeText(this, "Recording stopped", Toast.LENGTH_LONG).show();

              break;
            }
        }
        catch(Exception e){
            Log.e("Recorder", e.getMessage(), e);   
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }

}

}

blackfyre
  • 2,549
  • 5
  • 39
  • 58
  • this is not the complete code? I do not see the constructor here recorder = new MediaRecorder(); show all stacktrace, did you receive another exceptions? – dimetil Aug 08 '12 at 20:50
  • I used this tutorial http://www.benmccann.com/dev-blog/android-audio-recording-tutorial/ In start button I call the function start and in stop button I call the function stop... After pressing the start button , I press stop and then if I again press the start button then I get error message " A/libc(743): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 743 (xample.recorder)" – blackfyre Aug 09 '12 at 03:58
  • My fix https://stackoverflow.com/a/47992111/4592448 – Fortran Dec 27 '17 at 12:25

3 Answers3

67

I solved this problem by reseting recorder before releasing it.

recorder.stop();     // stop recording
recorder.reset();    // set state to idle
recorder.release();  // release resources back to the system
recorder = null;
KitKat
  • 1,495
  • 14
  • 15
blackfyre
  • 2,549
  • 5
  • 39
  • 58
  • 2
    Well that might fix the follow up error but how to resolve the warning. Or really what does the warning mean and how to resolve it? I'm currently having that problem. – Geeks On Hugs Oct 08 '13 at 19:15
  • Now i am getting MediaRecorder stop failed: -1007 – Shridutt Kothari Jan 22 '15 at 07:36
  • 3
    Why the warning exist if I don't add the line `recorder.reset();` ? – Peter Zhu Mar 21 '15 at 05:03
  • 1
    I'm sceptical about this solution for those only trying to get rid of the warning. I assume the .reset() will just as much not handle any events. – Matthias Feb 18 '16 at 13:24
  • I had this problem, and thanks to your comment, I saw that I never called stop(). Just reset and release. This solved my problem, too. Thx! – MarkJoel60 Sep 07 '16 at 00:34
1

This could arise due to running modded firmware. A SIGSEGV should not be possible from Java. Read this post. There is an explanation of the error in the end. Good luck.

Android SIGSEGV error when recording audio

Community
  • 1
  • 1
dimetil
  • 3,851
  • 2
  • 29
  • 47
  • Thanks! Can you also tell me why is recording stopping before calling the stop function...Recording stops after few seconds. :/ – blackfyre Aug 09 '12 at 07:02
  • I don't understand sequence of events. Which event follows after which? Start button -> recording is started -> stop recording automatically ("few seconds") -> Stop button ? Show full stack trace, it can help – dimetil Aug 09 '12 at 07:29
  • I have edited my post and sequence is I press the record button-> recording started->after few seconds it is stopped although I have not pressed the stop button. – blackfyre Aug 09 '12 at 07:37
  • log will help you understand what caused the stop recording – dimetil Aug 09 '12 at 08:36
  • I am getting this error on stock Android 4.4.3 on a Nexus 4, so this is not limited to modded firmware. – personne3000 Jul 12 '14 at 13:28
  • 1
    Managed to fix this issue in my code: I was calling getMaxAmplitude on a different thread after releasing the recorder, which probably accessed released memory spaces in native code. Things like SIGSEGV can happen in Java when using native libraries (which the OS unsurprisingly does for media capabilities). – personne3000 Jul 12 '14 at 13:35
  • @personne3000 thanks for your comment please post as answer I want to up-vote it, my problem was exactly this I had ref of media recorder in other thread which after release of recorder tried to call some methods of MediaRecorder which was causing the "Fatal signal 11 (SIGSEGV)” no visible java stack-trace as happens with native code. Please add your comment as answer. – Prashant Oct 16 '17 at 09:05
1

The documentation states:

In order to receive the respective callback associated with these listeners, applications are required to create MediaRecorder objects on threads with a Looper running (the main UI thread by default already has a Looper running).

Make sure you create the recorder on the UI thread. Perhaps also call its methods on the UI thread.

grebulon
  • 7,697
  • 5
  • 42
  • 66