46_视频刻录
--------------------
1.在main.xml布局文件添加用于视频画面绘制的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />
SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.reset();//重置为初始状态
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
/* 设置Video影片以SurfaceHolder播放 */
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setDataSource("/mnt/sdcard/oppo.mp4");
mediaPlayer.prepare();//缓冲
mediaPlayer.start();//播放
mediaPlayer.pause();//暂停播放
mediaPlayer.start();//恢复播放
mediaPlayer.stop();//停止播放
mediaPlayer.release();//释放资源
-------------------------------------------------------
2.package cn.itcast.video;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class VideoActivity extends Activity {
private static final String TAG = "VideoActivity";
private EditText filenameText;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
private String filename;//当前播放文件的名称
private int position;//记录播放位置
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mediaPlayer = new MediaPlayer();
this.filenameText = (EditText) this.findViewById(R.id.filename);
this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
ImageButton playButton = (ImageButton) this.findViewById(R.id.play);
ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause);
ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset);
ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);
ButtonClickListener listener = new ButtonClickListener();
playButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
resetButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
this.surfaceView.getHolder().setKeepScreenOn(true);
this.surfaceView.getHolder().addCallback(new SurfaceListener());
}
private class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.play://来自播放按钮
filename = filenameText.getText().toString();
play();
break;
case R.id.pause://来自暂停按钮
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
break;
case R.id.reset://来自重新播放按钮
if(!mediaPlayer.isPlaying()) play();
mediaPlayer.seekTo(0);
break;
case R.id.stop://来自停止按钮
if(mediaPlayer.isPlaying()) mediaPlayer.stop();
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
/**
* 播放视频
*/
private void play() throws IOException {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/mnt/sdcard/"+ filename);//设置需要播放的视频
mediaPlayer.setDisplay(surfaceView.getHolder());//把视频画面输出到SurfaceView
mediaPlayer.prepare();
mediaPlayer.start();
}
private class SurfaceListener implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {//方法在onResume()后被调用
Log.i(TAG, "surfaceCreated()");
if(position>0 && filename!=null){
try {
play();
mediaPlayer.seekTo(position);
position = 0;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "surfaceDestroyed()");
}
}
@Override
protected void onPause() {//当其他Activity被打开,停止播放
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();//得到播放位置
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onDestroy() {
if(mediaPlayer.isPlaying()) mediaPlayer.stop();
mediaPlayer.release();
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filename"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="oppo.mp4"
android:id="@+id/filename"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
android:id="@+id/play"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause"
android:id="@+id/pause"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset"
android:id="@+id/reset"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"
android:id="@+id/stop"
/>
</LinearLayout>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="240dip"
android:id="@+id/surfaceView"
/>
</LinearLayout>
---------------------------------------------------------
3.使用摄像头拍照
在main.xml布局文件添加用于显示取景画面的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />
SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = Camera.open();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();//开始预览
camera.autoFocus(null);//自动对焦
camera.takePicture(null, null, null, jpegCallback);//拍照片
camera.stopPreview();//停止预览
camera.release();//释放摄像头
---------------------------------------------------------------------
4.<uses-permission android:name="android.permission.CAMERA"/>
package cn.itcast.picture;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class TakePictureActivity extends Activity {
private static final String TAG = "TakePictureActivity";
private SurfaceView surfaceView;
private Camera camera;
private boolean preview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//高亮
setContentView(R.layout.main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(new SufaceListener());
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
}
private final class SufaceListener implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();//打开摄像头
Camera.Parameters parameters = camera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
if(preview) camera.stopPreview();
camera.release();
camera = null;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(camera!=null && event.getRepeatCount()==0){
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH:
camera.autoFocus(null);//自动对焦
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_CAMERA:
//拍照
camera.takePicture(null, null, new PictureCallbackListener());
break;
}
}
return true;
}
private final class PictureCallbackListener implements Camera.PictureCallback{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File(Environment.getExternalStorageDirectory(), "itcast.jpg");
FileOutputStream outStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outStream);
outStream.close();
//重新浏览
camera.stopPreview();
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
-----------------------------------------------------------------------------------
4.下面是视频刻录的所有源码:
a.新建android项目: videoRecorder
/videoRecorder/src/com/credream/videoRecoder/VideoRecorderActivity.java
package com.credream.videoRecoder;
import java.io.File;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
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;
import android.widget.RelativeLayout;
public class VideoRecorderActivity extends Activity {
private SurfaceView surfaceView;
private RelativeLayout layout;
private Button recordbutton;
private Button stopbutton;
private MediaRecorder mediaRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//这里是窗口没有title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//这里是要全屏显示
setContentView(R.layout.main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
//得到surfaceView
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//把的都的数据直接推到屏幕输送出来
surfaceView.getHolder().setFixedSize(176, 144);
//设置图像分辨率
surfaceView.getHolder().setKeepScreenOn(true);
//要求屏幕,不要锁屏,一直亮着
layout = (RelativeLayout) this.findViewById(R.id.layout);
recordbutton = (Button) this.findViewById(R.id.recordbutton);
stopbutton = (Button) this.findViewById(R.id.stopbutton);
}
//当用户触摸屏幕的时候会自动调用这个方法
@Override
public boolean onTouchEvent(MotionEvent event) {
//当用户触摸屏幕的时候显示两个按钮
if(event.getAction() == MotionEvent.ACTION_DOWN){
layout.setVisibility(ViewGroup.VISIBLE);
//把相对布局设置为可见。
}
//这里还要求,当刻录可用的时候,停止不可以用,
return super.onTouchEvent(event);
}
//当用户点击按钮的时候自动调用这个方法
public void record(View v){
switch (v.getId()) {
//当点击刻录按钮
case R.id.recordbutton:
try{
//这里用的是视频刻录者
//创建刻录好的文件
File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+ ".3gp");
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//指定声音的来源,mic克
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//指定视频的来源,摄像头
//刻录语音用到刻录权限和摄像头使用权限
//设置视频格式3gp
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置画面大小
mediaRecorder.setVideoSize(320, 240);
//设置视频帧数5的话画面比较清晰
mediaRecorder.setVideoFrameRate(5);
//声音编码为amr
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//视频编码采用MediaRecorder.VideoEncoder.H264这里264高级一点
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//输出这个文件,得到绝对路径
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
//设置显示的视频,这里把surfaceView直接给视频刻录者
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
//这个方法可以进行缓冲
mediaRecorder.prepare();
//开始刻录
mediaRecorder.start();
}catch (Exception e) {
e.printStackTrace();
}
//当用户点击刻录按钮的时候,点击完后,刻录按钮变为不可用
recordbutton.setEnabled(false);
//当用户点击刻录按钮的时候,点击完后,停止按钮变成可用
stopbutton.setEnabled(true);
break;
//当点击停止按钮
case R.id.stopbutton:
//当刻录者不为null
if(mediaRecorder!=null){
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
//停止刻录,并释放刻录者对象,然后付null
}
////当用户点击停止按钮的时候,点击完后,停止按钮变成不可用
recordbutton.setEnabled(true);
////当用户点击停止按钮的时候,点击完后,刻录按钮变成可用
stopbutton.setEnabled(false);
break;
}
}
}
----------------------------------------------------------------------------------------------
b./videoRecorder/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, VideoRecorderActivity!</string>
<string name="app_name">视频刻录</string>
<string name="recordbutton">刻录</string>
<string name="stopbutton">停止</string>
</resources>
----------------------------------------------------------------------------------
c.2013年5月9日
/videoRecorder/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.credream.videoRecoder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".VideoRecorderActivity"
android:screenOrientation="landscape">
<!-- 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"/>
<!-- 设置使用摄像头的权限 -->
<!-- 刻录完的文件放到sd卡需要这两个权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
---------------------------------------------------------------------------------------
d./videoRecorder/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 首先用到了帧布局
FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角
并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。
-->
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView"
/>
<!-- 这里,用SurfaceView,显示摄像头捕捉到的影像
-->
<!-- 这里用相对布局来在SurfaceView上的右下方放两个按钮-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout"
android:visibility="gone"
>
<!--先设置这个android:visibility="gone"相对布局不现实
然后当用户触摸屏幕的时候再把这个相对布局显示出来,也就是显示两个按钮-->
<!-- 设置为不显示这个相对布局-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recordbutton"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
android:id="@+id/recordbutton"
android:onClick="record"
/>
<!--android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
这里设置在屏幕的右下方,方法:在父窗口的右边,
和下边,然后在设置距离右边的组件有10个像素。宽度
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stopbutton"
android:layout_toLeftOf="@id/recordbutton"
android:layout_alignTop="@id/recordbutton"
android:layout_marginRight="30dp"
android:id="@+id/stopbutton"
android:enabled="false"
android:onClick="record"
/>
<!--android:layout_toLeftOf="@id/recordbutton"
这里用于设置该组件在recordbutton这个组件的左边
android:layout_alignTop="@id/recordbutton"
要求该组件和recordbutton这个组件顶部对齐
android:layout_marginRight="30dp"
和右边相距30
-->
<!--刚开始的时候android:enabled="false"设置停止按钮不可用-->
</RelativeLayout>
</FrameLayout>
------------------------------------------------------------------------------