Android 2.2 VideoView problem - android

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

Related

VideoView not playing video from URL in Android

I tried so many links but for all links its show same error.But it's giving error "can't play this video".
My code is the following
public class VideoDemo extends Activity {
private VideoView video;
private static final String path ="http://www.ustream.tv/embed/6540154?v=3&wmode=direct";
#Override
public void onCreate(Bundle icicle) {
setContentView(R.layout.activity_main);
videoview = (VideoView) findViewById(R.id.VideoView);
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoDemo.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
videoview.start();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
}
This is because the url you are using is not the one of a video but of a media player that plays the video.
To play the actual video you first need to find the url.
One way to find it is to use Livestreamer.
Install it following the instructions and then you can run a command like this
livestreamer http://www.ustream.tv/embed/6540154 best --stream-url
The output of this command is a url that you can use in your VideoView.
You have to permission in Your manifest file -
<uses-permission android:name="android.permission.INTERNET" >
and below is code for play video from url -
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(path));
videoView.start();

SD Card video not running in the video view

I have a video file saved on an SD Card. The path to it is saved in my database. While running the video file, I fetch the path from the database.
My video path is:
/mnt/sdcard/VideoLog/2013-01-01 11.18.57.mp4
and the code is as follows:
video_view_player = (VideoView) findViewById(R.id.videoview_player);
video_view_player.setVideoPath(filename);
//video_view_player.setMediaController(new MediaController(this));
//video_view_player.requestFocus();
video_view_player.start();
The filename in the video path is fetched from the database.
I even tried converting the .mp4 video into .3gp, but in vain.
public class MainActivity extends Activity {
VideoView vv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv=(VideoView)findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vv);
Uri video = Uri.parse("/sdcard/sample_mpeg4.mp4");
vv.setMediaController(mediaController);
vv.setVideoURI(video);
vv.start();
}
}
Try out this way:
VideoPLayer = (VideoView) findViewById(R.id.VideoPLayer);
mediaController = new MediaController(m_context);
VideoPLayer.setVideoPath(m_videoUrl);
VideoPLayer.setVideoURI(Uri.parse(videoUrl));
VideoPLayer.setMediaController(m_mediaController);
VideoPLayer.start();
mediaController.show();

video doesn't play with VideoView while play's with native player

Here is my code. In this if I launch the intent with video url it play's while it doesn't play's in videoview is there any way to get it working in VideoView
VideoView mVideoView = new VideoView(this);
String videoURL = "video_url";
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVideoURI(Uri.parse(videoURL));
setContentView(mVideoView);
while this native player plays video
Intent theIntent = new Intent();
theIntent.setDataAndType(Uri.parse(videoURL), "video/*");
I tested this on device also
Try below way
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("mp4 video link");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
Refer this link
https://stackoverflow.com/a/6410421/1441666
And also check supported formats Android Supported Media Formats
https://stackoverflow.com/a/8714189/1441666
i think this might be helpfull.
VideoView view = (VideoView) findViewById(R.id.xxxx);
MediaController mc = new MediaController(this);
mc.setMediaPlayer(view);
view.setMediaController(mc);
try{
view.setVideoURI(Uri.parse(file_path));
} catch(){
}
view.requestFocus();
try{
view.start();
}catch(){
}

How to play video from website in android?

How can I play videos from URL in android?. without new Intent?. is available?
Thanks
try this
String url="your video url";
VideoView videoView = new VideoView(this);
videoView.setVideoURI(Uri.parse(url));
setContentView(videoView);
videoView.setMediaController(new MediaController(this));
hope help
Try this code...It should work fine.
VideoView videoView = (VideoView) findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
String link = "http://www.youtube.com/watch?v=lEbxLDuecHU&playnext=1&list=PL040F3034C69B1674";
Uri videoLink = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(videoLink);
videoView.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();

Categories

Resources