首页 > 系统 > Android > 正文

Android-录像

2019-11-08 00:36:25
字体:
来源:转载
供稿:网友
package com.example.g150825_android27;import android.media.MediaRecorder;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.SurfaceView;import android.view.View;import java.io.IOException;/** * Created by Administrator on 2017/2/22 0022. */public class mediarecorder extends AppCompatActivity{ PRivate SurfaceView searchView; private MediaRecorder mediaRecorder; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mediarecorder); searchView =(SurfaceView) findViewById(R.id.sv_main_surface); mediaRecorder = new MediaRecorder(); } public void start(View view){ mediaRecorder.reset(); //设置视频和音频的来源 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置保存的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置编码格式 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoFrameRate(3); //设置保存的路径 mediaRecorder.setOutputFile("mnt/sdcard/DCIM/Camera/G150825_"+System.currentTimeMillis()+".mp4"); //将画面展示到SurfaceView mediaRecorder.setPreviewDisplay(searchView.getHolder().getSurface()); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } public void stop(View view){ mediaRecorder.stop(); }}<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><SurfaceView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_v_surface" ></SurfaceView><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="开始" android:onClick="start" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="暂停" android:onClick="stop" /></LinearLayout></RelativeLayout>需要加权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表