Android自定义录象机,录制一段视频

来源:互联网 发布:阿里云域名备案幕布 编辑:程序博客网 时间:2024/05/18 02:25

XML: main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:id="@+id/activity_main"    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"    tools:context="com.liang.test1.MainActivity"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="start"            android:onClick="start"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="stop"            android:onClick="stop"/>    </LinearLayout>    <SurfaceView        android:id="@+id/sf"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
xml:mainifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest package="com.liang.test1"          xmlns:android="http://schemas.android.com/apk/res/android">    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.CAMERA"/>    <uses-permission android:name="android.permission.RECORD_AUDIO"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application></manifest>
MainActivity.java:

package com.liang.test1;import android.media.CamcorderProfile;import android.media.CameraProfile;import android.media.MediaRecorder;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {    private SurfaceView sf;    private SurfaceHolder sh;    private MediaRecorder mr=new MediaRecorder();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sf=(SurfaceView)findViewById(R.id.sf);        sh=sf.getHolder();        sh.addCallback(this);    }    @Override    public void surfaceCreated(SurfaceHolder surfaceHolder) {        mr.setAudioSource(MediaRecorder.AudioSource.MIC);        mr.setVideoSource(MediaRecorder.VideoSource.CAMERA);        mr.setProfile(CamcorderProfile.get(CameraProfile.QUALITY_HIGH));        mr.setPreviewDisplay(sh.getSurface());        mr.setOutputFile("mnt/sdcard/视频文件名.mp4");        try {            mr.prepare();        } catch (Exception e) {            Log.e("tag","tag");            e.printStackTrace();            finish();        }    }    @Override    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {    }    @Override    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {        mr.release();    }    public void start(View v){        mr.start();    }    public  void stop(View v){        mr.stop();    }}

1 0
原创粉丝点击