Android 2.2 VideoView问题

时间:2011-01-03 13:32:20

标签: android android-videoview

我想从我的SD卡上播放一部电影。我尝试使用以下代码:

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

但是当我试图播放该文件时,我收到一条错误消息。 “视频未找到”或类似的东西。当我尝试从网络流媒体时,视频工作但非常迟钝。什么是在我的应用中播放视频的最佳方式?

由于

7 个答案:

答案 0 :(得分:9)

试试这个......

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

答案 1 :(得分:5)

观察到setVideoPath()失败,而setVideoURI()适用于 Web和本地,所以我坚持要使用它。

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

答案 2 :(得分:3)

使用此代码。希望它能正常工作

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

答案 3 :(得分:3)

尝试

video_view.setVideoURI(Uri.parse(path));

如果您尝试将其设置为uri,则无法直接作为字符串路径传递。代码对我来说很好:

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

答案 4 :(得分:3)

您的问题是视频路径设置不正确:

只需切换到此代码:

final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/robot.avi";
如果视频“robot.avi”存在于SD卡的根文件夹中,

将解决您的问题

答案 5 :(得分:2)

可能是avi不支持android.convert它进入mp4或wmv或3gp。 试试这段代码

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

答案 6 :(得分:2)

您正在自己的VideoView播放视频, 但是,如果您没有任何可自定义的内容,只想在屏幕上显示视频,为什么不使用默认播放器来播放视频。

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