Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 321
Johan Avatar asked Jan 03 '11 13:01

Johan


6 Answers

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();
like image 82
Shankar Agarwal Avatar answered Nov 12 '22 22:11

Shankar Agarwal


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();
like image 41
Mohammed Azharuddin Shaikh Avatar answered Nov 12 '22 22:11

Mohammed Azharuddin Shaikh


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();
}
}
}
like image 36
MBMJ Avatar answered Nov 13 '22 00:11

MBMJ


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();
like image 3
Daud Arfin Avatar answered Nov 12 '22 23:11

Daud Arfin


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

like image 3
Tal Kanel Avatar answered Nov 12 '22 23:11

Tal Kanel


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);
like image 2
MKJParekh Avatar answered Nov 13 '22 00:11

MKJParekh