Android 錄制視頻:

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了絳縣免費(fèi)建站歡迎大家使用!
public static voidrecordVideo(Context context, intlimit_time, intsize) {? ??
Intent intent =newIntent();
intent.setAction(MediaStore. ACTION_VIDEO_CAPTURE );
intent.putExtra(MediaStore. EXTRA_VIDEO_QUALITY ,1);
intent.addCategory(Intent. CATEGORY_DEFAULT );
if(size !=0) {? ? ? ??
? //大小限制是long型,int 型無效,所以后邊要寫一個(gè)L
? ? intent.putExtra(MediaStore. EXTRA_SIZE_LIMIT ,size *1024*1024L);//限制錄制大小(10M=10 * 1024 * 1024L)
}
if(limit_time !=0) {? ? ??
? ? ? intent.putExtra(MediaStore. EXTRA_DURATION_LIMIT ,limit_time);//限制錄制時(shí)間(10秒=10)
}? ??
File videoFile =createVideoFile(context);
if(videoFile !=null) {? ? ? ??
? ? intent.putExtra(MediaStore. EXTRA_OUTPUT ,Uri.fromFile(videoFile));
? ? context.startActivityForResult(intent, CAPTURE_VIDEO_CODE );
}
}
在Android 4.4(Kitkat - API level 19)上集成了一個(gè)比較好用的視頻(.mp4格式)錄制功能 – screenrecord 。
使用方法
1.? 基本用法
$ adb shell screenrecord /sdcard/myscreenrecord.mp4
/sdcard/myscreenrecord.mp4 為視頻錄制文件路徑
錄制默認(rèn)分辨率,默認(rèn)4Mbps,默認(rèn)180s的視頻,保存到sdcard上名為myscreenrecord.mp4
2.? 旋轉(zhuǎn)(參數(shù):–rotate)
$ adb shell screenrecord --rotate /sdcard/myscreenrecord.mp4
旋轉(zhuǎn)90度
3.? 指定分辨率(參數(shù):–size)
$adb shell screenrecord --size 112x112 /sdcard/myscreenrecord.mp4
分辨率為112x112,建議不要指定分辨率,使用默認(rèn)分辨率效果最佳;
注意,分辨率不是完全可以隨意定制的,比如在我手機(jī)上錄制100x100的會(huì)提示錯(cuò)誤:
The max width/height supported by codec is1920x1088
100x100is not supported by codec, suggest to set it as112x112
4.? 指定比特率(參數(shù):–bit-rate)
$adb shell screenrecord --bit -rate 8000000 /sdcard/myscreenrecord.mp4
設(shè)置比特率為8Mbps,比特率越大,文件越大,畫面越清晰;
5.? 限制錄制時(shí)間 (參數(shù): –time-limit)
$adb shell screenrecord --time -limit 10 /sdcard/myscreenrecord.mp4
限制視頻錄制時(shí)間為10s,如果不限制,默認(rèn)180s
6.? 導(dǎo)出視頻
$adb pull /sdcard/myscreenrecord.mp4
7.? 注意
請(qǐng)關(guān)注視頻文件生成大小,根據(jù)自身情況而定;
暫不支持聲音;
咱時(shí)不支持模擬器錄制( 模擬器錄制看這里 ),出現(xiàn)如下提示:Unable to get output buffers (err=-38)
Encoder failed (err=-38),可認(rèn)為是此原因;
8.? 命令查看
adb shell screenrecord --help
Usage: screenrecord [options]
Records the device's display to a .mp4 file.
Options:
--size WIDTHxHEIGHT
Set the video size, e.g."1280x720".? Default is the device's main
display resolution (if supported),??????? 1280x720if not.? For best
results,use a size supported by the AVC encoder.
--bit -rate RATE
Set the video bit rate, in megabits per second.? Default4Mbps.
--time -limit TIME
Set the maximum recording time, in seconds.? Default / maximum is180.
--rotate
Rotate the output90 degrees.
--verbose
Display interesting information on stdout.
--help
Showthis message.
Recording continues until Ctrl-C is hit or the time limit is reached.
(1)申請(qǐng)權(quán)限
(2)設(shè)置布局
這里做了一個(gè)簡(jiǎn)單的布局:添加了一個(gè)按鈕和一個(gè)ImageView控件用于顯示拍攝的圖像。
(3)為按鈕添加點(diǎn)擊事件監(jiān)聽
點(diǎn)擊按鈕時(shí),調(diào)用系統(tǒng)相機(jī)進(jìn)行拍照,并在確定后將圖像顯示在ImageView控件中。
(1)申請(qǐng)權(quán)限
(2)設(shè)置布局
添加了一個(gè)按鈕和一個(gè)VideoView控件用于顯示錄制的視頻。
(3)為按鈕添加點(diǎn)擊事件監(jiān)聽
同前面一樣,點(diǎn)擊按鈕后調(diào)用系統(tǒng)相機(jī)進(jìn)行錄制視頻,錄制完成后點(diǎn)擊確定即可將錄制的視頻顯示在VideoView控件中。
對(duì)于Android11.0的版本,在調(diào)用系統(tǒng)相近進(jìn)行視頻錄制的時(shí)候,即使在AndroidMenifest.xml中申請(qǐng)了CAMERA權(quán)限,還是會(huì)在程序運(yùn)行時(shí)報(bào)錯(cuò): Permission? Denial , ? . .... ....? with revoked permission android.permission.CAMERA
解決方法是在程序中動(dòng)態(tài)申請(qǐng)權(quán)限:
寫在最后:文章是在學(xué)習(xí)過程中做的學(xué)習(xí)筆記,同時(shí)與志同道合者分享,文章內(nèi)容均經(jīng)過我自己實(shí)驗(yàn)證實(shí)可行,如有問題歡迎留言,很高興一起交流討論,共同進(jìn)步!
最近有需求,錄制視頻的時(shí)候要添加水印,怎么搞?最后決定,opengles作為相機(jī)預(yù)覽并在上面繪制水印,使用mediacodec進(jìn)行編碼,使用mediamuxer輸出mp4文件,有需要的拿走。
本想寫篇博客,發(fā)現(xiàn)這些無論拿出哪個(gè)點(diǎn)都得寫半天,我最近也對(duì)opengles十分感興趣,以后有時(shí)間詳細(xì)分析一下.
用法比較簡(jiǎn)單
waterX和waterY是水印左下角坐標(biāo)的位置,范圍為[-1,1]
效果如下:
最后附上github地址
github
新聞標(biāo)題:android錄制視頻,android錄制視頻VBR
鏈接分享:http://chinadenli.net/article26/dsipjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、微信小程序、虛擬主機(jī)、云服務(wù)器、移動(dòng)網(wǎng)站建設(shè)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)