package com.example.videotest;
import java.io.File;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.media.MediaRecorder.VideoSource;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
public class MainActivity extends Activity {
private SurfaceHolder mSurfaceHolder; // 画布的遥控器
private MediaRecorder mMediaRecorder; // 刻录机
private Camera mCamera; // 硬件
private SurfaceView mSurfaceView; // 画布(预览) 创建/改变/销毁
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); // 获取画布的引用
mSurfaceHolder = mSurfaceView.getHolder(); // 只能遥控器才可以控制画布
mSurfaceHolder.addCallback(new CustomCallBack()); // 回调: 创建/改变/销毁
// mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/**
* 预览/停止控制
* @param view
*/
public void stop(View view) {
mMediaRecorder.setPreviewDisplay(null); // release resouce
mMediaRecorder.stop();
mMediaRecorder.release();
freeCameraResource();
}
private class CustomCallBack implements Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
initCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.lock();
mCamera.release();
mCamera = null;
}
}
}
private void initCamera() {
if (mCamera != null) {
freeCameraResource();
}
try {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
mCamera.unlock();
} catch (Exception e) {
e.printStackTrace();
freeCameraResource();
}
}
private void freeCameraResource() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.lock();
mCamera.release();
mCamera = null;
}
}
/**
* 刻录
*
*/
public void start(View view) {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.reset();
if (mCamera != null)
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 视频源
mMediaRecorder.setAudioSource(AudioSource.MIC);// 音频源
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);// 视频输出格式
// mMediaRecorder.setVideoSize(mWidth, mHeight);// 设置分辨率:
mMediaRecorder.setVideoFrameRate(20);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1024);
mMediaRecorder.setOrientationHint(90);// 输出旋转90度,保持竖屏录制
mMediaRecorder.setMaxDuration(15000);// 最大期限
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()
+ File.separator + "test.mp4");
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
nicks
- 发布:2015-08-07 10:57
- 更新:2018-05-17 10:46
- 阅读:3983
请教官方!请教官方!!!关于视频录像文件尺寸的问题 ,我用java写了一段代码,调用原生录像,视频文件很小,但怎么把这段代码转为NJS代码?
分类:Native.js
SSDNJ
离线打包,能调用插件,来控制录制视频的分辨率,时间长短么?
2018-02-24 17:52
Android_磊子
回复 SSDNJ:是可以的,就如楼主代码。
2018-02-26 10:36