使用插件我可以在默认媒体播放器上播放所有视频(来自 youtube 的视频除外)。但 youtube 视频仅在 android youtube 应用程序中播放。
private void playVideo(String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);
Intent intent = null;
// Check to see if someone is trying to play a YouTube page.
if (url.contains(YOU_TUBE)) {
// If we don't do it this way you don't have the option for youtube
uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
intent = new Intent(Intent.ACTION_VIEW, uri);
} else if(url.contains(ASSETS)) {
// get file path in assets folder
String filepath = url.replace(ASSETS, "");
// get actual filename from path as command to write to internal storage doesn't like folders
String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length());
// Don't copy the file if it already exists
File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename);
if (!fp.exists()) {
this.copy(filepath, filename);
}
// change uri to be to the new file in internal storage
uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename);
// Display video player
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
} else {
// Display video player
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
}
this.cordova.getActivity().startActivity(intent);
}