1.录制视频
1、添加各种权限及横屏属性
//传感器决定
参考:
Activity的screenOrientation属性详解
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
同时在activity中添加各种权限的动态请求
private String permssionRequestCode []={Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,Manifest.permission.CAMERA};
private void requestPermission(){
//动态请求各种权限
ActivityCompat.requestPermissions(this,permssionRequestCode, 0);
ActivityCompat.requestPermissions(this,permssionRequestCode, 1);
ActivityCompat.requestPermissions(this,permssionRequestCode,2);
ActivityCompat.requestPermissions(this,permssionRequestCode,3);
ActivityCompat.requestPermissions(this,permssionRequestCode,4);
}
完整Manifest文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.fengray.myex027recordvideo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".PlayActivity"></activity>
<activity
android:name=".BrowserActivity" android:screenOrientation="fullSensor"
android:label="@string/title_activity_browser" />
<activity android:name=".MainActivity" android:screenOrientation="fullSensor">
<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.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>
2、主布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/record"
android:src="@drawable/recode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/stop"
android:src="@drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/borowser"
android:src="@drawable/filedir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字的提示信息"/>
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3、文件列表布局文件recode_files.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="@+id/icon"
android:src="@drawable/files"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/filename"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
4、视频预览列表布局文件browser.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/info"
android:textSize="25sp"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="视频文件列表"/>
<ImageButton
android:id="@+id/back"
android:src="@drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ListView
android:id="@+id/videoList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
5、播放/停止布局文件player.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/player"
android:src="@drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/stoper"
android:src="@drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/backer"
android:src="@drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
6、创建BrowerActivity
public class BrowserActivity extends AppCompatActivity {
private ImageButton back=null;
private ListView videoList=null;
private SimpleAdapter adapter=null;//视频文件列表适配器
private List<Map<String,Object>> recordFiles=null;//视频文件列表数据
private String recDir="fengrayVido";//文件目录
private File recordeVideoSaveFileDir=null;//视频保持路径
private boolean sdcardExits=false;//sd卡存在标记
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
back=findViewById(R.id.back);
videoList=findViewById(R.id.videoList);
//sd卡文件路径检查及目录创建
if (this.sdcardExits= Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//检测文件是否挂载
recordeVideoSaveFileDir=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+recDir+File.separator);
if (!recordeVideoSaveFileDir.exists()){
recordeVideoSaveFileDir.mkdirs();//创建文件夹
}
}
back.setOnClickListener(new BackOnclickOnListenerImpl());
//获取并显示文件列表
getRecordefiles();
//为浏览视频文件增加事件
videoList.setOnItemClickListener(new VideoOnItemClickListenerImpl());
}
private class VideoOnItemClickListenerImpl implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (adapter.getItem(position) instanceof Map){
Map<?,?> map= (Map<?, ?>) adapter.getItem(position);
Intent intent=new Intent(BrowserActivity.this,PlayActivity.class);
intent.putExtra("filepath",BrowserActivity.this.recordeVideoSaveFileDir.toString()
+File.separator+map.get("filename"));
startActivity(intent);
}
}
}
//获取录制的视频文件
private void getRecordefiles(){
//初始化List列表
recordFiles=new ArrayList<>();
if (sdcardExits){
File files[]=recordeVideoSaveFileDir.listFiles();//获取路径下所有文件的对象数组
//遍历files数组中的file对象并取其文件名,将文件名添加到List集合中
for (int i = 0; i <files.length ; i++) {
Map<String,Object> fileInfo=new HashMap<>();
fileInfo.put("filename",files[i].getName());
recordFiles.add(fileInfo);
}
//创建adpter对象
adapter=new SimpleAdapter(BrowserActivity.this,recordFiles,R.layout.recode_files,
new String[]{"filename"},new int[]{R.id.filename});
//为list设置adapter
videoList.setAdapter(adapter);
}
}
//后退时间处理
private class BackOnclickOnListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent=new Intent(BrowserActivity.this,MainActivity.class);
startActivity(intent);
}
}
}
7、创建plaerActivity
public class PlayActivity extends AppCompatActivity {
private ImageButton player,stoper,backer;
private MediaPlayer mediaPlayer=null;
private SurfaceView surfaceView=null;
private SurfaceHolder surfaceHolder=null;
private String filepath=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
filepath=getIntent().getStringExtra("filepath");
player=findViewById(R.id.player);
stoper=findViewById(R.id.stoper);
backer=findViewById(R.id.backer);
surfaceView=findViewById(R.id.surfaceview);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(filepath);
} catch (IOException e) {
e.printStackTrace();
}
player.setOnClickListener(new PlayOnclickOnListenerImpl());
stoper.setOnClickListener(new StoperOnclickOnListenerImpl());
backer.setOnClickListener(new BackerOnclickOnListenerImpl());
}
//后退时间处理
private class PlayOnclickOnListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class StoperOnclickOnListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
mediaPlayer.stop();
}
}
private class BackerOnclickOnListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent=new Intent(PlayActivity.this,BrowserActivity.class);
startActivity(intent);
}
}
//响应按钮事件的回调方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
mediaPlayer.stop();
mediaPlayer.release();
finish();
}
return false;
}
}
8、主activity
public class MainActivity extends AppCompatActivity {
private String permssionRequestCode []={Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,Manifest.permission.CAMERA};
private ImageButton recorde,stop,brower;
private TextView info=null;
private MediaRecorder mediaRecorder=null;
private boolean sdcardExits=false;//sd卡存在标记
private File recordeVideoSaveFileDir=null;//视频保持路径
private File recordeVideoSaveFile=null;//视频文件对象
private String recordeVideoSaveFileName=null;//视频保存的名称
private String recDir="fengrayVideo";//保存文件夹
private boolean isRecord=false;
private SurfaceView surface=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
init();
//surface初始化
surface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surface.getHolder().setFixedSize(480,800);
//sd卡文件路径检查及目录创建
if (this.sdcardExits= Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//检测文件是否挂载
recordeVideoSaveFileDir=new File(Environment.getExternalStorageDirectory().toString()+File.separator);
if (!recordeVideoSaveFileDir.exists()){
recordeVideoSaveFileDir.mkdirs();//创建文件夹
}
}
//按钮点击监听
recorde.setOnClickListener(new RecOnClickListenerImpl());
stop.setOnClickListener(new StopOnClickListenerImpl());
brower.setOnClickListener(new BroOnClickListenerImpl());
//暂停停止按钮
stop.setEnabled(false);
}
private class RecOnClickListenerImpl implements View.OnClickListener{
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
//确保sd卡存在
if (sdcardExits){
recordeVideoSaveFileName=recordeVideoSaveFileDir.toString()+File.separator+"fengrayvideo"
+System.currentTimeMillis()+".3gp";//录制后文件的路径
recordeVideoSaveFile=new File(recordeVideoSaveFileName);
//创建一个多媒体录制对象
mediaRecorder=new MediaRecorder();
mediaRecorder.reset();//重置一下对象
//设置音频来源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频源
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设置视频源
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置操作格式为3gp
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//设置视频编码格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码格式
mediaRecorder.setOutputFile(recordeVideoSaveFileName);//设置输出文件地址
mediaRecorder.setVideoSize(320,240);//设置图像大小
mediaRecorder.setVideoFrameRate(10);//设置帧率
mediaRecorder.setPreviewDisplay(surface.getHolder().getSurface());//设置视频预览
try {
mediaRecorder.prepare();//录制视频对象的准备
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();//开始录像
info.setText("正在录像中……");//设置提示信息
stop.setEnabled(true);//停止按钮可用
recorde.setEnabled(false);//录制按钮不可用
isRecord=true;//录音状态flag设置
}
}
}
private class StopOnClickListenerImpl implements View.OnClickListener{
@Override
public void onClick(View v) {
if (isRecord){
mediaRecorder.stop();//停止录制
mediaRecorder.reset();//释放资源
stop.setEnabled(false);//停止按钮不可用
recorde.setEnabled(true);//录音录像按钮可用
info.setText("录像结束,文件路径为:"+recordeVideoSaveFile);
}
}
}
//浏览按钮响应事件
private class BroOnClickListenerImpl implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,BrowserActivity.class);
startActivity(intent);
}
}
//响应按钮事件的回调方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
//初始化各类组件
private void init(){
recorde=findViewById(R.id.record);
stop=findViewById(R.id.stop);
brower=findViewById(R.id.borowser);
surface=findViewById(R.id.surface);
info=findViewById(R.id.info);
}
//动态请求各种权限
private void requestPermission(){
//动态请求各种权限
ActivityCompat.requestPermissions(this,permssionRequestCode, 0);
ActivityCompat.requestPermissions(this,permssionRequestCode, 1);
ActivityCompat.requestPermissions(this,permssionRequestCode,2);
ActivityCompat.requestPermissions(this,permssionRequestCode,3);
ActivityCompat.requestPermissions(this,permssionRequestCode,4);
}
}
结果:
部分实现:没有完美实现
点击下载代码文件