简体   繁体   English

如何在android的videoview中播放.mp4视频?

[英]How to play .mp4 video in videoview in android?

I am working on a video player application, I want to play .mp4 video in the native video view.我正在开发一个视频播放器应用程序,我想在本机视频视图中播放.mp4视频。 I am not able to play video using a URL.我无法使用 URL 播放视频。 I am getting the error " Sorry this video cannot be played " and I am also not able to play downloaded video in the native video view either.我收到错误消息“对不起,无法播放此视频”,而且我也无法在本机视频视图中播放下载的视频。

My code for playing video in the video view:我在视频视图中播放视频的代码:

String mUrl = "http://www.servername.com/projects/projectname/videos/1361439400.mp4";

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
videoMediaController = new MediaController(this);
mVideoView.setVideoPath(mUrl);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.start();

Finally it works for me.最后它对我有用。

private VideoView videoView;

videoView = (VideoView) findViewById(R.id.videoView);

Uri video = Uri.parse("http://www.servername.com/projects/projectname/videos/1361439400.mp4");
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  @Override       
  public void onPrepared(MediaPlayer mp) {
       mp.setLooping(true);
       videoView.start();
    }
});

MP4 is just a container - the video and audio stream inside it will both be encoded in different formats. MP4 只是一个容器——其中的视频和音频流都将以不同的格式进行编码。

Android natively only supports certain types of formats. Android 本身仅支持某些类型的格式。 This is the list here. 这是这里的列表。

Make sure the video and audio encoding type is supported.确保支持视频和音频编码类型。 Just because it says "mp4" doesn't automatically mean it should be playable.仅仅因为它说“mp4”并不自动意味着它应该可以播放。

Use Like this:像这样使用:

Uri uri = Uri.parse(URL); //Declare your url here.

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));       
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

Another Method:另一种方法:

  String LINK = "type_here_the_link";
  VideoView mVideoView  = (VideoView) findViewById(R.id.videoview);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  Uri video = Uri.parse(LINK);
  mVideoView.setMediaController(mc);
  mVideoView.setVideoURI(video);
  mVideoView.start();

If you are getting this error Couldn't open file on client side, trying server side Error in Android .如果您收到此错误无法在客户端打开文件,请尝试 Android 中的服务器端错误 and also Refer this .并且也参考这个 Hope this will give you some solution.希望这会给你一些解决方案。

Check the format of the video you are rendering.检查您正在渲染的视频的格式。 Rendering of mp4 format started from API level 11 and the format must be mp4(H.264)从 API 级别 11 开始渲染 mp4 格式,格式必须为 mp4(H.264)

I encountered the same problem, I had to convert my video to many formats before I hit the format: Use total video converter to convert the video to mp4.我遇到了同样的问题,我必须将我的视频转换为多种格式才能达到格式:使用总视频转换器将视频转换为 mp4。 It works like a charm.它就像一个魅力。

I'm not sure that is the problem but what worked for me is calling mVideoView.start();我不确定这是问题所在,但对我mVideoView.start();是调用mVideoView.start(); inside the mVideoView.setOnPreparedListener event callback.mVideoView.setOnPreparedListener事件回调中。

For example:例如:

Uri uriVideo = Uri.parse(<your link here>);

MediaController mediaController = new MediaController(mContext);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uriVideo);
mVideoView.requestFocus();

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
     @Override
     public void onPrepared(MediaPlayer mp)
     {
          mVideoView.start();
     }
});

In Kotlin you can do as在 Kotlin 中,你可以这样做

 val videoView = findViewById<VideoView>(R.id.videoView)

       // If url is from raw
   /* val url = "android.resource://" + packageName
        .toString() + "/" + R.raw.video*/

    // If url is from network
    val url = "http://www.servername.com/projects/projectname/videos/1361439400.mp4"

    val video =
        Uri.parse(url)
    videoView.setVideoURI(video)
    videoView.setOnPreparedListener{
        videoView.start()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM