国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java制作仿微信錄制小視頻控件

2019-11-26 15:13:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文為用 MediaRecorder 錄制小視頻的控件,可以設(shè)置視頻錄制的時(shí)間、空間大小、初始時(shí)是否打開(kāi)攝像頭等。 此控件為組合控件,繼承自 LinearLayout ,為防止出錯(cuò),需實(shí)現(xiàn) android.media.MediaRecorder.OnErrorListener 接口。

小視頻錄制界面

MovieRecorderView.java

import java.io.File;import java.io.IOException;import java.util.Timer;import java.util.TimerTask; import android.content.Context;import android.content.res.TypedArray;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.media.MediaRecorder;import android.media.MediaRecorder.AudioEncoder;import android.media.MediaRecorder.AudioSource;import android.media.MediaRecorder.OnErrorListener;import android.media.MediaRecorder.OutputFormat;import android.media.MediaRecorder.VideoEncoder;import android.media.MediaRecorder.VideoSource;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.widget.LinearLayout;import android.widget.ProgressBar; import com.contron.dgyj.R;import com.contron.dgyj.common.Globals;import com.contron.dgyj.im.ImGlobal;import com.lidroid.xutils.util.LogUtils; /** * 視頻播放控件 *  * @author liuyinjun *  * @date 2015-2-5 */public class MovieRecorderView extends LinearLayout implements OnErrorListener {   private SurfaceView mSurfaceView;  private SurfaceHolder mSurfaceHolder;  private ProgressBar mProgressBar;   private MediaRecorder mMediaRecorder;  private Camera mCamera;  private Timer mTimer;// 計(jì)時(shí)器  private OnRecordFinishListener mOnRecordFinishListener;// 錄制完成回調(diào)接口   private int mWidth;// 視頻分辨率寬度  private int mHeight;// 視頻分辨率高度  private boolean isOpenCamera;// 是否一開(kāi)始就打開(kāi)攝像頭  private int mRecordMaxTime;// 一次拍攝最長(zhǎng)時(shí)間  private int mTimeCount;// 時(shí)間計(jì)數(shù)  private File mVecordFile = null;// 文件   public MovieRecorderView(Context context) {    this(context, null);  }   public MovieRecorderView(Context context, AttributeSet attrs) {    this(context, attrs, 0);  }   public MovieRecorderView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MovieRecorderView, defStyle, 0);    mWidth = a.getInteger(R.styleable.MovieRecorderView_width, 320);// 默認(rèn)320    mHeight = a.getInteger(R.styleable.MovieRecorderView_height, 240);// 默認(rèn)240     isOpenCamera = a.getBoolean(R.styleable.MovieRecorderView_is_open_camera, true);// 默認(rèn)打開(kāi)    mRecordMaxTime = a.getInteger(R.styleable.MovieRecorderView_record_max_time, 10);// 默認(rèn)為10     LayoutInflater.from(context).inflate(R.layout.movie_recorder_view, this);    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);    mProgressBar.setMax(mRecordMaxTime);// 設(shè)置進(jìn)度條最大量     mSurfaceHolder = mSurfaceView.getHolder();    mSurfaceHolder.addCallback(new CustomCallBack());    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     a.recycle();  }   /**   *    * @author liuyinjun   *    * @date 2015-2-5   */  private class CustomCallBack implements Callback {     @Override    public void surfaceCreated(SurfaceHolder holder) {      if (!isOpenCamera)        return;      try {        initCamera();      } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();      }    }     @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {     }     @Override    public void surfaceDestroyed(SurfaceHolder holder) {      if (!isOpenCamera)        return;      freeCameraResource();    }   }   /**   * 初始化攝像頭   *    * @author liuyinjun   * @date 2015-2-5   * @throws IOException   */  private void initCamera() throws IOException {    if (mCamera != null) {      freeCameraResource();    }    try {      mCamera = Camera.open();    } catch (Exception e) {      e.printStackTrace();      freeCameraResource();    }    if (mCamera == null)      return;     setCameraParams();    mCamera.setDisplayOrientation(90);    mCamera.setPreviewDisplay(mSurfaceHolder);    mCamera.startPreview();    mCamera.unlock();  }   /**   * 設(shè)置攝像頭為豎屏   *    * @author liuyinjun   * @date 2015-2-5   */  private void setCameraParams() {    if (mCamera != null) {      Parameters params = mCamera.getParameters();      params.set("orientation", "portrait");      mCamera.setParameters(params);    }  }   /**   * 釋放攝像頭資源   *    * @author liuyinjun   * @date 2015-2-5   */  private void freeCameraResource() {    if (mCamera != null) {      mCamera.setPreviewCallback(null);      mCamera.stopPreview();      mCamera.lock();      mCamera.release();      mCamera = null;    }  }   private void createRecordDir() {    File sampleDir = new File(Environment.getExternalStorageDirectory() + File.separator + "im/video/");    if (!sampleDir.exists()) {      sampleDir.mkdirs();    }    File vecordDir = sampleDir;    // 創(chuàng)建文件    try {      mVecordFile = File.createTempFile("recording", ".mp4", vecordDir);//mp4格式      LogUtils.i(mVecordFile.getAbsolutePath());    } catch (IOException e) {    }  }   /**   * 初始化   *    * @author liuyinjun   * @date 2015-2-5   * @throws IOException   */  private void initRecord() throws IOException {    mMediaRecorder = new MediaRecorder();    mMediaRecorder.reset();    if (mCamera != null)      mMediaRecorder.setCamera(mCamera);    mMediaRecorder.setOnErrorListener(this);    mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());    mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 視頻源    mMediaRecorder.setAudioSource(AudioSource.MIC);// 音頻源    mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 視頻輸出格式    mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);// 音頻格式    mMediaRecorder.setVideoSize(mWidth, mHeight);// 設(shè)置分辨率:    // mMediaRecorder.setVideoFrameRate(16);// 這個(gè)我把它去掉了,感覺(jué)沒(méi)什么用    mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);// 設(shè)置幀頻率,然后就清晰了    mMediaRecorder.setOrientationHint(90);// 輸出旋轉(zhuǎn)90度,保持豎屏錄制    mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);// 視頻錄制格式    // mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000);    mMediaRecorder.setOutputFile(mVecordFile.getAbsolutePath());    mMediaRecorder.prepare();    try {      mMediaRecorder.start();    } catch (IllegalStateException e) {      e.printStackTrace();    } catch (RuntimeException e) {      e.printStackTrace();    } catch (Exception e) {      e.printStackTrace();    }  }   /**   * 開(kāi)始錄制視頻   *    * @author liuyinjun   * @date 2015-2-5   * @param fileName   *      視頻儲(chǔ)存位置   * @param onRecordFinishListener   *      達(dá)到指定時(shí)間之后回調(diào)接口   */  public void record(final OnRecordFinishListener onRecordFinishListener) {    this.mOnRecordFinishListener = onRecordFinishListener;    createRecordDir();    try {      if (!isOpenCamera)// 如果未打開(kāi)攝像頭,則打開(kāi)        initCamera();      initRecord();      mTimeCount = 0;// 時(shí)間計(jì)數(shù)器重新賦值      mTimer = new Timer();      mTimer.schedule(new TimerTask() {         @Override        public void run() {          // TODO Auto-generated method stub          mTimeCount++;          mProgressBar.setProgress(mTimeCount);// 設(shè)置進(jìn)度條          if (mTimeCount == mRecordMaxTime) {// 達(dá)到指定時(shí)間,停止拍攝            stop();            if (mOnRecordFinishListener != null)              mOnRecordFinishListener.onRecordFinish();          }        }      }, 0, 1000);    } catch (IOException e) {      e.printStackTrace();    }  }   /**   * 停止拍攝   *    * @author liuyinjun   * @date 2015-2-5   */  public void stop() {    stopRecord();    releaseRecord();    freeCameraResource();  }   /**   * 停止錄制   *    * @author liuyinjun   * @date 2015-2-5   */  public void stopRecord() {    mProgressBar.setProgress(0);    if (mTimer != null)      mTimer.cancel();    if (mMediaRecorder != null) {      // 設(shè)置后不會(huì)崩      mMediaRecorder.setOnErrorListener(null);      mMediaRecorder.setPreviewDisplay(null);      try {        mMediaRecorder.stop();      } catch (IllegalStateException e) {        e.printStackTrace();      } catch (RuntimeException e) {        e.printStackTrace();      } catch (Exception e) {        e.printStackTrace();      }    }  }   /**   * 釋放資源   *    * @author liuyinjun   * @date 2015-2-5   */  private void releaseRecord() {    if (mMediaRecorder != null) {      mMediaRecorder.setOnErrorListener(null);      try {        mMediaRecorder.release();      } catch (IllegalStateException e) {        e.printStackTrace();      } catch (Exception e) {        e.printStackTrace();      }    }    mMediaRecorder = null;  }   public int getTimeCount() {    return mTimeCount;  }   /**   * @return the mVecordFile   */  public File getmVecordFile() {    return mVecordFile;  }   /**   * 錄制完成回調(diào)接口   *   * @author liuyinjun   *    * @date 2015-2-5   */  public interface OnRecordFinishListener {    public void onRecordFinish();  }   @Override  public void onError(MediaRecorder mr, int what, int extra) {    try {      if (mr != null)        mr.reset();    } catch (IllegalStateException e) {      e.printStackTrace();    } catch (Exception e) {      e.printStackTrace();    }  }}

movie_recorder_view.xml

<?xml version="1.0" encoding="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" encoding="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>

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 冕宁县| 临海市| 铜陵市| 普兰店市| 聂荣县| 绥中县| 葵青区| 万载县| 鹿泉市| 桂平市| 张家界市| 泰州市| 西平县| 乳源| 长葛市| 无极县| 眉山市| 牡丹江市| 山丹县| 阳江市| 盖州市| 灵寿县| 武隆县| 乌拉特中旗| 文水县| 青阳县| 伊川县| 威信县| 厦门市| 汉源县| 论坛| 赤峰市| 五寨县| 松原市| 巴林左旗| 台湾省| 苏州市| 家居| 玉溪市| 淮滨县| 延长县|