实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package cn.yws.video;
import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.VideoView;
//http://www.cnblogs.com/stay/archive/2011/06/24/2089129.html
public class VideoActivity extends Activity {
private File myRecAudioFile;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Button buttonStart;
private Button buttonStop;
private Button buttonPlay, buttonPlay2;
private File dir;
private MediaRecorder recorder;
private RadioButton backRadioButton, frontRadioButton;
private Camera camera;
private VideoView videoView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) findViewById(R.id.videoView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.setKeepScreenOn(true);
buttonStart = (Button) findViewById(R.id.start);
buttonStop = (Button) findViewById(R.id.stop);
buttonPlay = (Button) this.findViewById(R.id.play);
buttonPlay2 = (Button) this.findViewById(R.id.play2);
backRadioButton = (RadioButton) this.findViewById(R.id.back);
frontRadioButton = (RadioButton) this.findViewById(R.id.front);
videoView = (VideoView) this.findViewById(R.id.video_view);
File defaultDir = Environment.getExternalStorageDirectory();
String path = defaultDir.getAbsolutePath() File.separator "V"
File.separator;
// 创建文件夹存放视频
dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
recorder();
}
});
buttonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(recorder!=null)
{
recorder.stop();
recorder.reset();
}
if(camera!=null)
{
camera.stopPreview();
}
// camera.release();
// camera=null;
// recorder.release();
// recorder = null;
}
});
buttonPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (myRecAudioFile != null) {
String path = myRecAudioFile.getAbsolutePath();
Uri uri = Uri.parse(path);
Log.i("path", uri.getPath());
// videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(uri);
videoView.start();
videoView.requestFocus();
}
}
});
buttonPlay2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(VideoActivity.this,
VideoActivity2.class);
intent.putExtra("filePath", myRecAudioFile.getAbsolutePath());
startActivity(intent);
}
});
}
private void destory(boolean force) {
if (recorder != null) {
if (!force) {
recorder.stop();
}
recorder.reset();
recorder.release();
recorder = null;
}
if (camera != null) {
camera.lock();
camera.release();
camera = null;
}
}
@Override
protected void onDestroy() {
destory(true);
super.onDestroy();
}
@SuppressLint("NewApi")
public void recorder() {
try {
destory(true);
recorder = new MediaRecorder();
recorder.setOrientationHint(90);//旋转度数
int index = CameraTool.FindBackCamera();
if (frontRadioButton.isChecked()) {
index = CameraTool.FindFrontCamera();
recorder.setOrientationHint(270);//旋转度数
}
/**if(camera!=null)
{
camera.release();
camera=null;
}*/
camera = Camera.open(index);
camera.setDisplayOrientation(90);//旋转度数
// camera.setPreviewDisplay(mSurfaceHolder);
// camera.startPreview();
//http://www.cnblogs.com/stay/archive/2011/06/24/2089129.html
camera.unlock();// 注意,要在MediaRecorder设置参数之前就调用unlock来获得camera的控制权。camera是单例的嘛。如果不调用,程序就挂
recorder.setCamera(camera);
myRecAudioFile = new File(dir, "video.mp4");
// myRecAudioFile = File.createTempFile("video", ".3gp", dir);//
// 创建临时文件
recorder.setPreviewDisplay(mSurfaceHolder.getSurface());// 预览
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 视频源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 录音源为麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);// 输出格式为3gp
recorder.setVideoSize(800, 480);// 视频尺寸
recorder.setVideoFrameRate(5);// 视频帧频率
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);// 视频编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 音频编码
recorder.setMaxDuration(10000);// 最大期限
recorder.setOutputFile(myRecAudioFile.getAbsolutePath());// 保存路径
recorder.prepare();
recorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论