【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 prowidth = 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))。
總結
以上是生活随笔為你收集整理的【android录屏填坑】录屏报错start fail,stop fail的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu(Linux)使用Simpl
- 下一篇: 无人机航空摄影测量技术的优势