Johan
Johan

Reputation: 2159

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

Upvotes: 11

Views: 1694

Answers (7)

Tal Kanel
Tal Kanel

Reputation: 10785

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

Upvotes: 3

MBMJ
MBMJ

Reputation: 5431

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

Upvotes: 2

Daud Arfin
Daud Arfin

Reputation: 2499

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

Upvotes: 3

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

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

Upvotes: 6

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

Upvotes: 5

MKJParekh
MKJParekh

Reputation: 34301

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

Upvotes: 2

MBMJ
MBMJ

Reputation: 5431

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

Upvotes: 3

Related Questions