I have to play video from SD card one by one but SD card path having some issue which give error like :-
Unable to open content: file:///storage/emulated/0/sachin.mp4
My code :-
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
File file = null;
file = new File(SDCardRoot, "sachin.mp4");
Uri uri = Uri.fromFile(file);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
Did u give this permission in manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Related
I have been modifying an example video player code; the example code played the video from the program resource directory. I copied the video to the phone's /storage/emulated/0/DCIM/ directory and add three lines:
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
to replace:
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This change causes a file not found error. The video is in the Phone storage DCIM directory. I also tried adding the .mp4 to the video name.
In debug mode the uri appears to be the correct value: /storage/emulated/0/DCIM/steprock
Can anyone spot the code error? A partial listing is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView = (VideoView) findViewById(R.id.video_view);
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
//videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This is code which play video from path you describe for video in VideoView.
// Video path.
path = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock.mp4";
videoView = (VideoView) findViewById(R.id.video_view);
final MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse(path);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
I found the error, forgot to add the read permission below to the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application ...
I developed android application about video saving and play it from phone storage. I saved it but I don't get it and I don't save it. I shared photo location in below how can I get it and play it? I tried like below but I don't do it.
SDCardRoot = new File(getFilesDir() + "/videos");![picture shows video location][2]
File[] videos = SDCardRoot.listFiles();
try {
FileInputStream fi = new FileInputStream(videos[1]);
Log.i("fii", "" + fi);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fi.getFD());
pl.prepare();
pl.start();
VideoView vv;
vv = (VideoView) findViewById(R.id.videoView1);
vv.setVisibility(View.VISIBLE);
Uri uri = Uri.parse(sUriPath);
vv.setVideoURI(uri);
vv.setMediaController(new MediaController(MainActivity.this));
vv.requestFocus();
vv.start();
I downloaded the video from server url and store to sdcard, it's stored fine as a .mp4 format. But when i access this video from VideoView for playing video using MediaController, i'm getting can't play this video error. The folder name is Video.
MediaController mediaController = new MediaController(ImageTargets.this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
String sdpath=Environment.getExternalStorageDirectory().getAbsolutePath();
String video_path=sdpath+"Video" +"focusvideo.mp4";
videoView.setVideoPath(video_path);
videoView.start();
Environment.getExternalStorageDirectory() method return type is File instead of sdcard path as String. To get sdcard path call getAbsolutePath() of file as:
String sdpath=Environment.getExternalStorageDirectory().getAbsolutePath();
String video_path=str_path+"/Video/" +"focusvideo.mp4";
videoView.setVideoPath(video_path);
try this :-
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath(SrcPath);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
I have path (String path) to my file .3gp on phone and I want to get Uri from that path so I can play in VideoView. I have tried like
video= Uri.fromFile(new File(path)));
videoView.setVideoURI(video);
videoView.start();
but it doesn't work. can someone show me how to find Uri when I have string path ?
I think you can use
videoView.setVideoURI(Uri.parse(path));
Or,
videoView.setVideoPath(path);
Try like this,
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("/sdcard/sticky.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
I want to play a movie from my sd-card. Ive tried using the following code:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String("/sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
But when Im trying to play the file i get an error message. "video not found" or something similar. When i tried streaming from the web, the video worked but was very laggy. Whats the best way to play videos in my app?
Thanks
Try this...
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory()+"/sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
It is observed that setVideoPath() fails, while setVideoURI() works well for both Web and Local so I insist you to use this.
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String("file:///sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoURI(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
Use this code.Hope it will work
public class VideoPlayActivity extends Activity {
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
File clip=new File(Environment.getExternalStorageDirectory(),
"haha.mp4");
if (clip.exists()) {
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
}
}
}
Try with
video_view.setVideoURI(Uri.parse(path));
you can not pass directly as a string path if you are trying to set as a uri. The code which is working fine for me :
path = Environment.getExternalStorageDirectory() + "/file_name";
// Add controls to a MediaPlayer like play, pause.
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
// Set the path of Video or URI.
video_view.setVideoURI(Uri.parse(path));
// Set the focus.
video_view.requestFocus();
video_view.start();
your problem is that the video path is not set the right way:
just switch to this code:
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/robot.avi";
that will solve your problem if the video "robot.avi" exists on the root folder of the sd card
You are playing your video in your own VideoView,
But if you have nothing to customize and just want to show the video in the screen,why dont you use the default player to play the video.
File imgFile = new File(Environment.getExternalStorageDirectory()+"FileName");
//make sure the video is in SDCard,
//if its located in any folder care to pass full absolute path
Intent tostart = new Intent(Intent.ACTION_VIEW);
tostart.setDataAndType(Uri.parse(imgFile.getPath()), "video/*");
startActivity(tostart);
May be avi does not support in android.convert it into mp4 or wmv or 3gp.
try this code
public class VideoPlayActivity extends Activity {
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
File clip=new File(Environment.getExternalStorageDirectory(),
"robot.mp4");
if (clip.exists()) {
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
}
}
}