在Android Studio中调用MediaRecorder会导致应用程序崩溃

4

我正在尝试创建一个类来设置和开始音频录制,但是一旦我点击按钮,应用程序就会崩溃。我已经确定问题出在设置MediRecorder参数的地方。

private void startRec() throws IOException {
    if (mrecorder!=null)
        mrecorder.release();

    mrecorder= new MediaRecorder();

->  mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    /*
    mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mrecorder.setOutputFile(MFILE);

    mrecorder.prepare();
    mrecorder.start();

    */
}

当箭头所指向注释开始的那一行被执行时,它会崩溃。我还在清单文件中添加了以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

任何帮助都不胜感激。
更新后的日志。

[ 05-12 00:39:13.299 30086:30158 D/ ] ro.exynos.dss isEnabled: 0 05-12 00:39:13.309 30086-30158/record66.record6 D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1 05-12 00:39:13.319 30086-30086/record66.record6 W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView) 05-12 00:39:13.319 30086-30158/record66.record6 D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : record66.record6 05-12 00:39:13.359 30086-30086/record66.record6 D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1 05-12 00:39:13.389 30086-30086/record66.record6 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@682466c time:234401322 05-12 00:39:15.749 30086-30086/record66.record6 D/ViewRootImpl: ViewPostImeInputStage processPointer 0 05-12 00:39:15.879 30086-30086/record66.record6 D/ViewRootImpl: ViewPostImeInputStage processPointer 1 05-12 00:39:15.929 30086-30086/record66.record6 D/AndroidRuntime: Shutting down VM 05-12 00:39:15.939 30086-30086/record66.record6 E/AndroidRuntime: FATAL EXCEPTION: main Process: record66.record6, PID: 30086 java.lang.RuntimeException: setAudioSource failed. at android.media.MediaRecorder._setAudioSource(Native Method) at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:488) at record66.record6.MainActivity.startRec(MainActivity.java:58) at record66.record6.MainActivity.onClick(MainActivity.java:94) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10815) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 05-12 00:39:17.909 30086-30086/record66.record6 I/Process: Sending signal. PID: 30086 SIG: 9


请参考以下链接:https://dev59.com/kGkv5IYBdhLWcg3wzj41 - Saurabh Vardani
崩溃日志是什么? - F43nd1r
请将logcat的内容贴出来,这样我们才能帮助您。 - Dhrupal
@Dhrupal 我是新手,不确定你指的是哪个日志记录器,但我正在直接在手机上运行程序,当它崩溃时会给我一个通用的“发生错误,该程序将关闭”的提示。 - G-J
1个回答

1
请查看 logcat 是什么。
并告诉我们你遇到了什么错误。这样可以帮助我们提供帮助。
package com.example.dhrupalpatel.test;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener{
MediaRecorder mrecorder;
boolean mStartRecording=false;

Button start, stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

}

private void startRec() throws IOException {
   boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states, but all we need
        //  to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    File sdCardDirectory= Environment
            .getExternalStorageDirectory();
    if(mExternalStorageAvailable && !sdCardDirectory.exists())
    {
        sdCardDirectory.mkdir();
    }
    File f= new File(sdCardDirectory.getPath()+"/"+System.currentTimeMillis()+".mp3");
    if( mrecorder == null ) {
        mrecorder = new MediaRecorder();
        mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

        mrecorder.setOutputFile(f.getPath());
        mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    }
    if(!mStartRecording) {

        try {
            mrecorder.prepare();
            mrecorder.start();
            mStartRecording = true;
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void stopRec() throws IOException {

    if(mStartRecording) {
        mStartRecording = false;
        mrecorder.stop();
        mrecorder.reset();
        mrecorder.release();
        mrecorder = null;
    }
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.start:
            try {
                startRec();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.stop:
            try {
                stopRec();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
    }

}
}

好的,我明白了。编辑了原始帖子,请告诉我是否正确。 - G-J
谢谢,我会尝试的。我的代码为什么不起作用呢?是因为我没有实现点击监听器吗? - G-J
我已经添加了您编写的代码,但仍然遇到相同的问题。我使用您的最新代码更新了Logcat。 - G-J
我收到一个警告,上面写着“File.mkdir()的结果被忽略了”。我该如何解决它? - G-J
如果我的回答对您有帮助,您能否接受并点赞呢? :) - Dhrupal
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接