1. 调用本地相机进行录像
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, RECORD_VEDIO);
// 接收返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECORD_VEDIO && resultCode == RESULT_OK) {
//将返回的content://地址转换为sd卡上的绝对路径
String filePath = convertContentUri2FilePath(data.getData());
}
}
// 将媒体库content://相对路径转化为普通的绝对路径(这里需要read_external_storage permission)
public String convertContentUri2FilePath(Uri contentUri) {
String filePath = null;
if (contentUri != null && "content".equals(contentUri.getScheme())) {
Cursor cursor = this.getContentResolver().query(contentUri,
new String[] { MediaStore.Video.Media.DATA }, null, null,
null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
filePath = contentUri.getPath();
}
Log.d("", "Chosen path = " + filePath);
return filePath;
}
2. 使用本地播放器进行播放
public void intentPlayVideo(String videoPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(videoPath);
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent);
this.finish();
}
}
3. 使用widget VideoView 进行播放
videoView.setKeepScreenOn(true);
videoView.setVideoPath(videoPath);
android.widget.MediaController mediaController = new android.widget.MediaController(
this);
videoView.setMediaController(mediaController);
videoView.start();
4. 使用本地播放器播放网络视频
这里需要Intent Permission
final String path_video =
"http://video.ted.com/talk/podcast/2016/None/KennethLacovara_2016-480p.mp4";
public void intentPlayVideo(String videoPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(path_video);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
this.finish();
}
5. 使用VideoView widget播放网络视频
// 由于资源问题,播放等待的时间较长
final String path_video =
"http://video.ted.com/talk/podcast/2016/None/KennethLacovara_2016-480p.mp4";
videoView.setKeepScreenOn(true);
Uri uri = Uri.parse(path_video);
videoView.setVideoURI(uri);
android.widget.MediaController mediaController = new android.widget.MediaController(
this);
videoView.setMediaController(mediaController);
videoView.start();
}
}