mainActivity如下:
[java]
package c.c;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
/**
* Demo描述:
* 利用Su易做图ceView拍摄视频
*
* 注意:
* 1 严重注意:MediaRecorder参数的设置.因手机不同而有差异
* 2 在设置MediaRecorder的参数时,应先设置:
* setVideoSource(),setAudioSource(),setOutputFormat(),setVideoEncoder(),setAudioEncoder
* 然后再设置其余的参数,查看方法对应的API有提示
*
*/
public class MainActivity extends Activity implements Su易做图ceHolder.Callback{
private Button mStartButton;
private Button mStopButton;
private Su易做图ceView mSu易做图ceView;
private Su易做图ceHolder mSu易做图ceHolder;
private MediaRecorder mMediaRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 设置横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
init();
}
private void init(){
mStartButton=(Button) findViewById(R.id.start_button);
mStartButton.setOnClickListener(new ButtonClickListenerImpl());
mStopButton=(Button) findViewById(R.id.stop_button);
mStopButton.setOnClickListener(new ButtonClickListenerImpl());
mSu易做图ceView=(Su易做图ceView) findViewById(R.id.su易做图ceView);
mSu易做图ceHolder=mSu易做图ceView.getHolder();
mSu易做图ceHolder.addCallback(this);
mSu易做图ceHolder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void initMediaRecorder(){
mMediaRecorder=new MediaRecorder();
//设置视频源
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
//设置音频源
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
//设置文件输出格式
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置视频编码方式
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置音频编码方式
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//设置视频高和宽,注意文档的说明:
//Must be called after setVideoSource().
//Call this after setOutFormat() but before prepare().
//设置录制的视频帧率,注意文档的说明:
//Must be called after setVideoSource().
//Call this after setOutFormat() but before prepare().
mMediaRecorder.setVideoFrameRate(20);
//设置预览画面
mMediaRecorder.setPreviewDisplay(mSu易做图ceHolder.getSu易做图ce());
//设置输出路径
mMediaRecorder.setOutputFile
(Environment.getExternalStorageDirectory()+File.separator+System.currentTimeMillis()+".mp4");
}
private class ButtonClickListenerImpl implements OnClickListener{
public void onClick(View v) {
if (v.getId()==R.id.start_button) {
try {
initMediaRecorder();
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (v.getId()==R.id.stop_button) {
if (mMediaRecorder!=null) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder=null;
}
}
}
}
//Su易做图ceHolder.Callback接口
public void su易做图ceCreated(Su易做图ceHolder holder)
补充:移动开发 , Android ,