你好,谁能告诉我如何在 android 默认浏览器上播放 mp3/mp4?
我在资源中的原始文件夹中有 mp3/mp4 文件。
我已经尝试了以下代码,但遭到忽视并且无法正常工作,它将打开默认播放器但没有播放所选文件。
String dataResourceDirectory = "raw";
String dataResoruceFilename = "armin";
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + dataResourceDirectory + "/" + dataResoruceFilename);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(uri);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
谁能推荐一下?
最佳答案
您无法将应用程序私有(private)包中的视频播放到默认视频播放器,因为您的默认视频播放器无法识别此路径。
解决方案 1:复制 sd 卡中的文件
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myfolder");
File video = new File (sdCard.getAbsolutePath() + "/myfolder/video.mp4");
if(!dir.isDirectory()) dir.mkdirs();
if(!video.isFile()){
InputStream ins = getResources().openRawResource(R.raw.test);
int size;
try {
size = ins.available();
byte[] buffer = new byte[size];
ins.read(buffer);
ins.close();
FileOutputStream fos = new FileOutputStream(new File(dir,"video.m4v"));
fos.write(buffer);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File myvid = new File(dir, "video.m4v");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(myvid), "video/*");
this.startActivity(intent);
解决方案 2:启动 videoview 视频播放器
VideoView videoView = (VideoView)findViewById(R.id.myvideoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_name_without_extension;
videoView.setVideoPath(path);
videoView.start();
关于android - 如何将 raw 文件夹中的 mp3/mp4 播放到 android 中的默认播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173848/