开发者

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 media开发者_如何学JAVAController = 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();
}
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜