android学习之实现简单的录像功能
录像功能的实现:1.所需要的权限:
<uses-permission android:name="android.permission.CAMERA"/> <!--摄像头的权限-->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2.xml布局的配置:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView"
/>
<RelativeLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="17dp"
android:text="录像"
android:onClick="startVoide"
android:enabled="true"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="@+id/start"
android:text="停止"
android:onClick="startVoide"
android:enabled="false"
/>
</RelativeLayout>
</FrameLayout>
3.acativity里面的代码:
package com.example.view_player;
import java.io.File;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private SurfaceView surfaceView; //视频显示区域的声明
private RelativeLayout layout; //相对布局的声明
private Callback callback; //
private Camera camera;
private MediaRecorder mediaRecorder; //媒体录制
private Button start; //按钮
private Button stop;
@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);
start = (Button) this.findViewById(R.id.start);
this.stop = (Button) this.findViewById(R.id.stop);
layout = (RelativeLayout) this.findViewById(R.id.layout);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
callback = new MyCallback();
surfaceView.getHolder().addCallback(callback);
}
private class MyCallback implements Callback //回调类
{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
try
{
camera = Camera.open(); //打开摄像
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();//开始预览
}catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if(camera != null)
{
camera.stopPreview(); //停止预览
camera.release(); //释放资源
camera = null;
}
}
}
public void startVoide(View v)
{
switch(v.getId())
{
case R.id.start:
start.setEnabled(false);
stop.setEnabled(true);
try
{
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + System.currentTimeMillis() + ".3gp");
camera.unlock();
mediaRecorder = new MediaRecorder(); //媒体录制对象
mediaRecorder.setCamera(camera); //设置摄像
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置输出的文件的格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置编码
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setOutputFile(file.getAbsolutePath()); //设置输出文件的路径
mediaRecorder.setVideoSize(320, 240);//设置video的大小
mediaRecorder.setVideoFrameRate(5);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare(); //缓冲
mediaRecorder.start(); //开始录制
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
break;
case R.id.stop:
start.setEnabled(true);
stop.setEnabled(false);
if(mediaRecorder != null)
{
mediaRecorder.stop(); //停止
mediaRecorder.reset(); //重置
mediaRecorder.release(); //释放资源
camera.lock();
mediaRecorder = null;
}
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
layout.setVisibility(ViewGroup.VISIBLE); //当触屏的时候按钮可见
return super.onTouchEvent(event);
}
}
@大黑鱼 支持玄玄~ 支持不犹豫! 支持楼楼{:5_92:} {:1_1:} {:1_1:}{:1_1:}{:1_1:}
页:
[1]