Android学习笔记_27_多媒体之视频刻录

一、配置文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.videorecord"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />



    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.videorecord.MainActivity"

            android:label="@string/app_name" android:screenOrientation="landscape" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

 <uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>
View Code

  布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >



    <SurfaceView

        android:id="@+id/surfaceView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />



    <RelativeLayout

        android:id="@+id/buttonlayout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

         >



        <Button

            android:id="@+id/videostop"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"

            android:layout_alignParentRight="true"

            android:layout_marginRight="5dp"

            android:layout_toLeftOf="@+id/videorecord"

            android:onClick="record"

            android:enabled="false"

            android:text="@string/stop" />



        <Button

            android:id="@+id/videorecord"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"

            android:layout_alignParentRight="true"

            android:layout_marginRight="59dp"

            android:onClick="record"

            android:text="@string/record" />

    </RelativeLayout>



</RelativeLayout>
View Code

二、代码实现:

package com.example.videorecord;



import android.app.Activity;

import android.media.MediaRecorder;

import android.os.Bundle;

import android.view.Menu;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.ViewGroup;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

/**

 * 视频采集刻录

 * @author Administrator

 *

 */

public class MainActivity extends Activity {

    private View layout;

    private SurfaceView surfaceView;

    private Button recordButton;

    private Button stopButton;

    private MediaRecorder recorder;

    private boolean record=false;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //设置窗口没有标题

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //全屏显示

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 

                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        

        setContentView(R.layout.activity_main);

        layout= this.findViewById(R.id.buttonlayout);

        recordButton= (Button) this.findViewById(R.id.videorecord);

        stopButton= (Button) this.findViewById(R.id.videostop);

        //确定surfaceView什么时候被时间

        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);

        /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/

        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        surfaceView.getHolder().setFixedSize(176, 144);    //设置分辨率

        surfaceView.getHolder().setKeepScreenOn(true);//设置高亮

        

    }

    public void record(View v){

        try {

            switch (v.getId()) {

            case R.id.videorecord:

                recorder = new MediaRecorder();

                recorder.reset();                    

                recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频    

                recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //麦克风            

                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//输出格式3gp

                recorder.setVideoSize(320, 240);//视频大小

                recorder.setVideoFrameRate(3); //每秒3帧,每秒从摄像头捕获多少画面        

                recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //设置视频编码方式    

                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置声音编码        

                recorder.setOutputFile("/storage/sdcard0/360/"+System.currentTimeMillis()+".3gp");//文件输出位置    

                //设置预览显示

                recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

                recorder.prepare();//预期准备

                recorder.start();//开始刻录

                record = true;

                recordButton.setEnabled(false);

                stopButton.setEnabled(true);

                break;

            case R.id.videostop:

                if(record){

                    recorder.stop();//停止刻录

                    record = false;

                    recorder.release();

                    recorder=null;

                }            

                recordButton.setEnabled(true);

                stopButton.setEnabled(false);

                break;

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    

    //触摸事件

    public boolean onTouchEvent(MotionEvent event) {

        //手指按下屏幕

        if(event.getAction()==MotionEvent.ACTION_DOWN){

            layout.setVisibility(ViewGroup.VISIBLE);//显示布局

            return true;

        }

        return super.onTouchEvent(event);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



}

 

你可能感兴趣的:(Android学习)