24

我正在尝试在 android 中录制音频,但我遇到了问题。

我有开始和停止按钮,“开始”用于开始录制,“停止”用于停止录制。

问题是,当我按下停止按钮时,我的应用程序会记录一条消息“W/MediaRecorder(635): mediarecorder away with unhandled events”。(启动功能正在正确保存音频文件。)

然后,如果我再次按下开始或停止按钮,则会收到错误消息“A/libc(743): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 743 (xample.recorder)”

录音类代码如下:

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

主要活动代码如下:

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

}

}

4

3 回答 3

66

我通过在释放记录器之前重置记录器解决了这个问题。

recorder.stop();     // stop recording
recorder.reset();    // set state to idle
recorder.release();  // release resources back to the system
recorder = null;
于 2012-08-16T09:34:08.493 回答
1

这可能是由于运行修改后的固件而引起的。SIGSEGV 不应该来自 Java。阅读这篇文章。最后有错误的解释。祝你好运。

录制音频时出现Android SIGSEGV错误

于 2012-08-09T06:57:50.530 回答
1

该文档指出:

为了接收与这些侦听器相关的相应回调,应用程序需要在运行 Looper 的线程上创建 MediaRecorder 对象(默认情况下,主 UI 线程已经运行了 Looper)。

确保在 UI 线程上创建记录器。也许也可以在 UI 线程上调用它的方法。

于 2014-07-22T07:07:05.100 回答