Artikel ini adalah kontrol untuk merekam video pendek dengan MediaRecorder. Anda dapat mengatur waktu, ukuran ruang, dan apakah akan menyalakan kamera terlebih dahulu, dll. untuk perekaman video. Kontrol ini merupakan kontrol kombinasi dan merupakan warisan dari LinearLayout. Untuk mencegah error, antarmuka android.media.MediaRecorder.OnErrorListener perlu diimplementasikan.
Antarmuka perekaman video kecil
MovieRecorderView.java
impor java.io.File;impor java.io.IOException;impor java.util.Timer;impor java.util.TimerTask; impor android.content.Context;impor android.content.res.TypedArray;impor android.hardware.Camera ;impor android.hardware.Camera.Parameters;impor android.media.MediaRecorder;impor android.media.MediaRecorder.AudioEncoder;impor android.media.MediaRecorder.AudioSource;impor android.media.MediaRecorder.OnErrorListener;impor android.media.MediaRecorder.OutputFormat;impor android.media.MediaRecorder.VideoEncoder;impor android.media.MediaRecorder .VideoSource;impor android.util.AttributeSet;impor android.view.LayoutInflater;impor android.view.SurfaceHolder;impor android.view.SurfaceHolder.Callback;impor android.view.SurfaceView;impor android.widget.LinearLayout;impor android.widget.ProgressBar; impor com.contron.dgyj.R;import com.contron.dgyj.common.Globals;import com.contron.dgyj.im.ImGlobal;import com.lidroid.xutils.util.LogUtils; /** * Kontrol pemutaran video* * @author liuyinjun * * @tanggal 2015- 2-5 */kelas publik MovieRecorderView extends LinearLayout mengimplementasikan OnErrorListener { private SurfaceView mSurfaceView; SurfaceHolder mSurfaceHolder; private ProgressBar mProgressBar; private MediaRecorder mMediaRecorder; private Camera mCamera; private Timer mTimer; // Timer private OnRecordFinishListener mOnRecordFinishListener; boolean pribadi isOpenCamera;// Apakah akan menyalakan kamera dari awal private int mRecordMaxTime; // Waktu maksimum untuk satu pengambilan gambar private int mTimeCount; // Penghitungan waktu private File mVecordFile = null; ); } public MovieRecorderView(Konteks konteks, AttributeSet attrs) { ini(konteks, attrs, 0); MovieRecorderView(Konteks konteks, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = konteks.obtainStyledAttributes(attrs, R.styleable.MovieRecorderView, defStyle, 0); .MovieRecorderView_width, 320);// Default 320 mHeight = a.getInteger(R.styleable.MovieRecorderView_height, 240); // Default 240 isOpenCamera = a.getBoolean(R.styleable.MovieRecorderView_is_open_camera, true); // Default terbuka mRecordMaxTime = a.getInteger(R.styleable. MovieRecorderView_record_max_time, 10);//Defaultnya adalah 10 LayoutInflater.from(context).inflate(R.layout.movie_recorder_view, this); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); id .progressBar); mProgressBar.setMax(mRecordMaxTime);// Tetapkan jumlah maksimum bilah kemajuan mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(new CustomCallBack()); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); *@tanggal 5-2-2015*/ kelas privat CustomCallBack mengimplementasikan Callback { @Override public void SurfaceCreated(SurfaceHolder Holder) { if (!isOpenCamera) return; coba { initCamera(); catch (IOException e) { // TODO Blok tangkapan yang dihasilkan secara otomatis e.printStackTrace(); @Override public void surfaceChanged(Pemegang SurfaceHolder, format int, lebar int, tinggi int) { } @Override public void SurfaceDestroyed(SurfaceHolder Holder) { if (!isOpenCamera) return; freeCameraResource(); } } /** * Inisialisasi kamera* * @author liuyinjun * @tanggal 5-2-2015 * @throws IOException */ private void initCamera() melempar IOException { jika (mCamera != null) { freeCameraResource(); mCamera = Kamera.open(); } tangkapan (Pengecualian e) { e.printStackTrace(); freeCameraResource(); jika (mCamera == null) kembali; mSurfaceHolder); mCamera.startPreview(); mCamera.unlock(); Atur kamera ke layar vertikal * * @author liuyinjun * @tanggal 5-2-2015 */ private void setCameraParams() { if (mCamera != null) { Parameter params = mCamera.getParameters(); " , "potret"); mCamera.setParameters(params); } } /** * Rilis sumber daya kamera* * @author liuyinjun * @tanggal 5-2-2015 */ private void freeCameraResource() { if (mCamera != null) { mCamera.setPreviewCallback(null); mCamera.lock(); mCamera = null; } } private void createRecordDir() { File sampleDir = baru File(Environment.getExternalStorageDirectory() + File.separator + "im/video/"); if (!sampleDir.exists()) { sampleDir.mkdirs() } File vecordDir = sampleDir; File.createTempFile("rekaman", ".mp4", vecordDir);//mp4 format LogUtils.i(mVecordFile.getAbsolutePath()); } catch (IOException e) { } } /** * Inisialisasi* * @author liuyinjun * @tanggal 5-2-2015 * @throws IOException * / private void initRecord() melempar IOException { mMediaRecorder = baru MediaRecorder(); mMediaRecorder.reset(); jika (mCamera != null) mMediaRecorder.setCamera(mCamera); mMediaRecorder.setOnErrorListener(ini); Sumber video mMediaRecorder.setAudioSource(AudioSource.MIC);//Sumber audio mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);//Format keluaran video mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);//Format audio mMediaRecorder.setVideoSize(mWidth, mHeight) ;//Tetapkan resolusi: // mMediaRecorder.setVideoFrameRate(16);// Saya menghapus ini, sepertinya tidak ada gunanya. mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);// Atur frekuensi frame, dan kemudian akan menjadi jelas. ;/ / Outputnya diputar 90 derajat, menjaga perekaman layar vertikal. mMediaRecorder.prepare(); coba { mMediaRecorder.start(); } tangkapan (IllegalStateException e) { e.printStackTrace(); } tangkapan (RuntimeException e) { e.printStackTrace(); (); } } /** * Mulai merekam video* * @penulis liuyinjun * @tanggal 5-2-2015 * @param fileName * Lokasi penyimpanan video * @param onRecordFinishListener * Antarmuka panggilan balik setelah waktu yang ditentukan tercapai */ public void record(final OnRecordFinishListener onRecordFinishListener) { this.mOnRecordFinishListener = onRecordFinishListener; (!isOpenCamera)//Jika kamera tidak dihidupkan, buka initCamera(); initRecord(); mTimeCount = 0; // Tetapkan kembali penghitung waktu mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { // TODO Metode yang dibuat secara otomatis stub mTimeCount++; mProgressBar. setProgress(mTimeCount);//Menetapkan bilah kemajuan jika (mTimeCount == mRecordMaxTime) {// Ketika waktu yang ditentukan tercapai, hentikan pengambilan gambar stop(); if (mOnRecordFinishListener != null) mOnRecordFinishListener.onRecordFinish(); /** * Berhenti memotret * * @author liuyinjun * @tanggal 5-2-2015 */ public void stop() { stopRecord(); rilisRecord(); freeCameraResource(); } /** * Berhenti merekam* * @author liuyinjun * @tanggal 5-2-2015 */ public void stopRecord() { mProgressBar.setProgress(0); mTimer != null) mTimer.cancel(); jika (mMediaRecorder != null) {// Ini tidak akan macet setelah pengaturan mMediaRecorder.setOnErrorListener(null); mMediaRecorder.setPreviewDisplay(null); coba { mMediaRecorder.stop(); .printStackTrace(); } tangkapan (Pengecualian e) { e.printStackTrace(); } /** * Sumber daya rilis * * @author liuyinjun * @tanggal 5-2-2015 */ private void releaseRecord() { if (mMediaRecorder != null) { mMediaRecorder.setOnErrorListener(null); ; } tangkapan (IllegalStateException e) { e.printStackTrace(); e.printStackTrace(); } } mMediaRecorder = null; } public int getTimeCount() { return mTimeCount; } /** * @return mVecordFile */ public File getmVecordFile() { return mVecordFile; Antarmuka * * @author liuyinjun * * @tanggal 5-2-2015 */ antarmuka publik OnRecordFinishListener { public void onRecordFinish(); } @Override public void onError(MediaRecorder mr, int apa, int ekstra) { coba { if (mr != null) mr.reset(); (); } catch (Pengecualian e) { e.printStackTrace();
film_recorder_view.xml
<?xml version="1.0" coding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas .android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
attrs.xml
<?xml version="1.0" coding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas .android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
Di atas adalah keseluruhan isi artikel ini, saya harap Anda semua menyukainya.