1
votes

Hello can anyone tell me how to play mp3/mp4 to android default browser?

I have mp3/mp4 files in my raw folder in resources.

I have tried following code but got disregards and not working, It will open default player but didn't play the selected file.

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);

Can anyone please suggest?

1

1 Answers

1
votes

You can't play video from application's private package to default video player, because your default video player can't recognize this path.

Solution 1: copy the file in sd card

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);

Solution 2: start videoview video player

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();