Trying to record audio but getting message “mediar

2019-01-23 12:40发布

问题:

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();
        }

}

}

回答1:

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;


回答2:

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



回答3:

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.



回答4:

I have Android 4.0.4 (an by my unmodified version of, Samsung have made changes to it) running on my Samsung Galaxy S3 and I can sometimes get a SIGSEGV ("A/libc(20448): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)") when I use the mediarecorder.

I also get a SIGSEGV in my AVD for Android 4.0, so it's possible to get a SIGSEGV even there.

Now I just have to find what I do wrong with the mediarecorder. =)