【android录屏填坑】录屏报错start fail,stop fail

先写个标题,有时间了填充内容。

时隔N天,终于忙完了。内容填充中……

前言

为什么要写这么一篇文章。因为我最近做录屏实在是被坑哭了。不是遇到start fail报错,就是遇到stop fail报错,这种报错就只会告诉你在哪一行,根本没有具体的信息指引你是什么原因出错,只能百度,因此浪费了很多时间在处理报错上。录屏功能的需求还是挺大的,因此,为了让做录屏的攻城狮门减少处理这类问题的时间,也为了我自己以后不踩这个坑,总结记录一下这类错误的处理方式。

正文

1.start fail

这类报错要重点注意初始化mediaRecorder时,设置参数的顺序,顺序错了就会报这个错。

这里直接提供一个正确的初始化参数顺序,实测可用,请安心复制粘贴:

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        if (TextUtils.isEmpty(fileName)) {
            recordFilePath = getsaveDirectory() + System.currentTimeMillis() + ".mp4";
        } else {
            recordFilePath = getsaveDirectory() + fileName + ".mp4";
        }
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setOutputFile(recordFilePath);
        mediaRecorder.setVideoSize(width, height);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
        mediaRecorder.prepare();

还有很小一部分是因为设置的录制格式不支持,如果你用以上初始化代码初始化之后,调用mediaRecorder.start还是报错,就试试将setOutputFormat改成别的格式,比如MP4等。如果还不行,那么我这篇文章帮不到你了,请另行百度。

2.stop fail

这个stop fail是最恶心最难搞的,因为,我遇到这个问题的时候,是在一部分机型上才有,别的运行正常。之后我注意到有问题的都是刘海屏手机,于是,试着将setVideoSize的高度减去刘海屏的高度,发现可以正常录屏了。也就是说,录屏功能需要适配刘海屏。

以下是适配刘海屏的代码:

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        if (TextUtils.isEmpty(fileName)) {
            recordFilePath = getsaveDirectory() + System.currentTimeMillis() + ".mp4";
        } else {
            recordFilePath = getsaveDirectory() + fileName + ".mp4";
        }
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setOutputFile(recordFilePath);
        LogUtil.i(TAG+" oriHeight = "+height);
        if(NotchScreenUtil.hasNotchInScreenAtHuawei(this)){
            int notchHeight = NotchScreenUtil.getNotchSizeAtHuawei(this);
            height-=notchHeight;
            if(notchHeight == 81){//华为p20 pro
                width = 720;
                height = 1080;
            }
            LogUtil.i(TAG+" huawei : notchHeight = "+notchHeight);
        }else if(NotchScreenUtil.hasNotchInScreenAtOppo(this)){
            int notchHeight = NotchScreenUtil.getNotchSizeAtOppo();
            height-=notchHeight;
            LogUtil.i(TAG+" oppo : notchHeight = "+notchHeight);
        }else if(NotchScreenUtil.hasNotchInScreenAtVivo(this)){
            int notchHeight = NotchScreenUtil.getNotchSizeAtVivo(this);
            height-=notchHeight;
            LogUtil.i(TAG+" vivo : notchHeight = "+notchHeight);
        }
        LogUtil.i(TAG+" result height = "+height);
        mediaRecorder.setVideoSize(width, height);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

刘海屏适配工具类(NotchScreenUtil)代码太长了,请到这个链接下载:

https://download.csdn.net/download/yonghuming_jesse/11223089

PS:这里单独区分了一下华为p20 pro,因为华为p20 pro减去刘海屏高度依然会报错,于是只能设置成初始尺寸720*1080。oriheight是取的屏幕高度

还有一部分报错是因为调用mediaRecorder.start()到mediaRecorder.stop()之间的间隔太短造成的,简而言之,就是不能刚开始就结束录制。建议设置一个最小录制时间,如3秒。

如果帮到你了点个赞吧,如果遇到问题请留言。

如果觉得我写的博客还行,或者是刚入行没多久的新人,欢迎关注我,博主会隔三差五的更新一些填坑文章以及技术干货,应该会对你有用。(System.out.print(smile))。

 
发布了73 篇原创文章 · 获赞 30 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/yonghuming_jesse/article/details/90293352