VideoView vs MediaPlayer? - android

Please tell me why this would work on with MediaPLayer and not in videoView? And how to make it work with a video view?
Videos are downloaded form an API and saved in this folder I created:
File mediadir = cw.getDir("tvr", Context.MODE_PRIVATE);
VideoView
final Uri uri = Uri.parse(path);
// path = /data/data/com.foo.app/tvr/video.mp4
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVisibility(View.VISIBLE);
videoView.setOnCompletionListener(this);
videoView.setVideoURI(Uri.parse(path));
videoView.start();
Error VideoView Sorry, this Video cannot be player and error (1, -2...)
MediaPlayer --- THIS WORKS
FileInputStream fileInputStream = new FileInputStream(path);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fileInputStream.getFD());
pl.prepare();
pl.start();

The basic reason is the MODE_PRIVATE, which disallow VideoView and MediaPlayer from playing non-World Readable files, unless you pass the FD as you have.
Here's a more detailed explanation

Related

How can I get video from file Explorer

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

how does android.net.Uri work

now I'm developing an android Video on Demand or Audio on Demand Player, I notice there's a function in package android.net.Uri, part of my code of play the video or audio is like this:
Play Audio part:
mPlayer = new MediaPlayer();
mPlayer = MediaPlayer.create(this, Uri.parse("http://.../MJ.mp3"));
mPlayer.setLooping(true);
mPlayer.start();
Play Video Part:
VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
String vidAddress = "https://.../ABC.mp4";
Uri vidUri = Uri.parse(vidAddress);
vidView.setVideoURI(vidUri);
vidView.start();
I'm wondering does this satisfy the video or audio on demand request? In an other word, when I play the audio or video using the URI function, does it work like Youtube, which downloads just part of the video and plays the video, or download the whole file first then play it?

How to play mp4 video in android

I want to play mp4 video from server in android.
Video URL
I tried with using video view but there is blank screen appear.
I also tried with Intent.Action view but it show message "Cant play video".
Is there any other way to do this ?
Thanks in advance.
Use Like this:
Uri uri = Uri.parse(URL); //Declare your url here.
VideoView mVideoView = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
Another Method:
String LINK = "type_here_the_link";
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(video);
mVideoView.start();

How to play a mp4 videos in Android through Server

I am using following code to play mp4 videos which is stored in sever.... And i am get error like this->
This Video can not be played????
Uri video = Uri.parse("http://129.0.0.....");
MediaController mediaController = null;
mVideoView.setMediaController(mediaController);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://129.0.0....")));
mVideoView.setVideoURI(video);
mVideoView.setVideoPath("http://129.0.0......");
mVideoView.requestFocus();
mVideoView.start();
This might be helpful to you i have used this in android 2.3.3.
public void videoPlayer(String path, String fileName, boolean autoplay){
//get current window information, and set format, set it up differently, if you need some special effects
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//the VideoView will hold the video
VideoView videoHolder = new VideoView(this);
//MediaController is the ui control howering above the video (just like in the default youtube player).
videoHolder.setMediaController(new MediaController(this));
//assing a video file to the video holder
videoHolder.setVideoURI(Uri.parse(YOUR_SERVER_VIDEOFILE_URL));
//get focus, before playing the video.
videoHolder.requestFocus();
if(autoplay){
videoHolder.start();
}
}
Always pass the mp4,3gp etc video format link in URI.Try this code:
VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse("http://hermes.sprc.samsung.pl/widget/tmp/testh.3gp"));
videoView.requestFocus();
videoView.start();

Android 2.2 VideoView problem

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

Categories

Resources