Android自定義錄制視頻功能
Android錄制視頻MediaRecorder+SurfaceView的使用方法,供大家參考,具體內(nèi)容如下
先看效果圖:

<1>將視頻動畫顯示到SurfaceView控件上
<2>使用MediaRecorder類進(jìn)行視頻的錄制
常用的方法:
mediaRecorder.reset(); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //從照相機(jī)采集視頻 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setVideoFrameRate(3); //每秒3幀 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //設(shè)置視頻編碼方式 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp"); mediaRecorder.setOutputFile(videoFile.getAbsolutePath()); mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); mediaRecorder.prepare();//預(yù)期準(zhǔn)備 mediaRecorder.start();//開始刻錄 mediaRecorder.stop();//停止刻錄
下面看代碼:
public class MediaRecorderActivity extends AppCompatActivity {
private SurfaceView sv_mediarecorder_surface;
private MediaRecorder mediaRecorder;
private boolean isStartAndStop=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
setContentView(R.layout.activity_mediarecorder);
//獲取SurfaceView
sv_mediarecorder_surface = (SurfaceView) findViewById(R.id.sv_mediarecorder_surface);
//實(shí)例化媒體錄制器
mediaRecorder = new MediaRecorder();
}
//視頻錄制與暫停的方法
public void startMediaCorder(View view){
Button button= (Button) view;
if (!isStartAndStop) {
if (mediaRecorder==null){
//實(shí)例化媒體錄制器
mediaRecorder = new MediaRecorder();
}
mediaRecorder.reset();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //從照相機(jī)采集視頻
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設(shè)置麥克風(fēng)
//設(shè)置保存的格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//設(shè)置編碼格式
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoFrameRate(3);
//獲取根路徑
String sdPath= Environment.getExternalStorageDirectory().getAbsolutePath();
//設(shè)置保存的路徑
mediaRecorder.setOutputFile(sdPath+"/taoge"+System.currentTimeMillis()+".mp4");
//將畫面展示到SurfaceView
mediaRecorder.setPreviewDisplay(sv_mediarecorder_surface.getHolder().getSurface());
//準(zhǔn)備
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
button.setText("結(jié)束");
}else{
// 為其它應(yīng)用釋放攝像頭
mediaRecorder.release();
mediaRecorder = null;
//關(guān)閉
button.setText("開始");
}
isStartAndStop=!isStartAndStop;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配技巧
今天小編就為大家分享一篇關(guān)于Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配技巧,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android使用Notification在狀態(tài)欄上顯示通知
這篇文章主要為大家詳細(xì)介紹了Android使用Notification在狀態(tài)欄上顯示通知,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
基于Android studio3.6的JNI教程之helloworld思路詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之helloworld,本文通過圖文實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android中在GridView網(wǎng)格視圖上實(shí)現(xiàn)item拖拽交換的方法
這篇文章主要介紹了Android中在GridView上實(shí)現(xiàn)item拖拽交換效果的方法,比起ListView的列表?xiàng)l目交換稍顯復(fù)雜,文中先介紹了關(guān)于創(chuàng)建GridView的一些基礎(chǔ)知識,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)在屏幕上移動圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例的相關(guān)資料,這里介紹了Webview加載網(wǎng)頁的方法及帶進(jìn)度的Drawable文件view_progress_webview的實(shí)現(xiàn),需要的朋友可以參考下2017-07-07

