Android 2.2 VideoView问题

发布于 2024-10-10 10:21:47 字数 498 浏览 1 评论 0原文

我想播放 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();

但是当我尝试播放该文件时,我收到一条错误消息。 “找不到视频”或类似的内容。当我尝试从网络流式传输时,视频可以正常播放,但速度很慢。在我的应用程序中播放视频的最佳方式是什么?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一袭白衣梦中忆 2024-10-17 10:21:47

试试这个...

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

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();
你的背包 2024-10-17 10:21:47

据观察,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();

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();
格子衫的從容 2024-10-17 10:21:47

使用此代码。希望它能起作用

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

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();
}
}
}
ゝ杯具 2024-10-17 10:21:47

尝试一下,

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

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();
明月夜 2024-10-17 10:21:47

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

只需切换到此代码:

final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/robot.avi";

如果视频“robot.avi”存在于 sd 卡的根文件夹中,这将解决您的问题

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

机场等船 2024-10-17 10:21:47

您正在自己的 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);

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);
绝不服输 2024-10-17 10:21:47

可能是android不支持avi。将其转换为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();
}
}
}

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();
}
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文