你好,谁能告诉我如何将mp3 / mp4播放到Android默认浏览器?
我的资源中的原始文件夹中有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);
有人可以建议吗?
答案 0 :(得分:1)
您无法将应用程序的私有包中的视频播放到默认视频播放器,因为您的默认视频播放器无法识别此路径。
解决方案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)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();