停止录制屏幕时应用程序崩溃

axzmvihb  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(349)

我正在尝试开发一个应用程序来记录mediarecorder在屏幕上显示的所有内容。但当mediarecorder停止时,应用程序崩溃。
当我开始录制时,一切正常,但当录制停止时,问题就出现了。
编辑我使用的是android 9.0 api 28
下面是我录制屏幕的代码,然后保存视频以便以后播放:

public class MainActivity extends AppCompatActivity {

Button Start , Stop;
MediaRecorder mediaRecorder;
String Save = null;
int Width , Height;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    Height = displayMetrics.heightPixels;
    Width = displayMetrics.widthPixels;

    Start = (Button)findViewById(R.id.Start);
    Stop = (Button)findViewById(R.id.Stop);

    Save = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            +new StringBuilder("/").append(new SimpleDateFormat("MM-yyyy-hh_mm_ss")
            .format(new Date())).append(".mp4").toString();

    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECORD_AUDIO)
            +ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{
                        Manifest.permission.RECORD_AUDIO
                        ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                },100);
    }
    Start.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {

            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setVideoEncodingBitRate(512 * 1000);
            mediaRecorder.setVideoSize(Width,Height);
            mediaRecorder.setAudioEncodingBitRate(16);
            mediaRecorder.setAudioSamplingRate(44100);
            mediaRecorder.setOutputFile(Save);
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaRecorder.start();
            Start.setEnabled(false);
            Stop.setEnabled(true);
        }
    });
    Stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mediaRecorder != null) {
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder = null;
            }
            Start.setEnabled(true);
            Stop.setEnabled(false);
        }
    });
}
}

我的日志:

Process: com.example.recordvoice, PID: 5633
java.lang.RuntimeException: stop failed.
    at android.media.MediaRecorder.stop(Native Method)
    at com.example.recordvoice.MainActivity$2.onClick(MainActivity.java:94)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

有人知道怎么解决这个问题吗?

ejk8hzay

ejk8hzay1#

尝试更改:

Stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mediaRecorder != null) {
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder = null;
            }
            Start.setEnabled(true);
            Stop.setEnabled(false);
        }
    });

收件人:

Stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }
        Start.setEnabled(true);
        Stop.setEnabled(false);
    }
});

我所改变的只是 mediaRecorder.reset();mediaRecorder.release(); 让我知道它是否有效

相关问题