Android 调用系统播放器播放视频

原文地址:https://blog.csdn.net/u010248450/article/details/75048047?locationNum=6&fps=1

1.调用播放器播放本地视频

错误演示:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory() + "/1.mp4"), "video/*");
startActivity(intent);
部分手机播放不了

正确演示:

Intent intent = new Intent(Intent.ACTION_VIEW);
String path = Environment.getExternalStorageDirectory().getPath()+ "/1.mp4";//该路径可以自定义
File file = new File(path);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "video/*");
startActivity(intent);


2.调用播放器播放网络视频

String url = "http://www.baidu.com/1.mp4";//示例,实际填你的网络视频链接
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(url), mimeType);
startActivity(mediaIntent);

Android 7.0 以上 android.os.FileUriExposedException 解决方法:

https://blog.csdn.net/dodod2012/article/details/80570163

猜你喜欢

转载自blog.csdn.net/dodod2012/article/details/80570181