Android 如何从网址播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2620049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:36:58  来源:igfitidea点击:

how to play video from url

androidvideo-streamingandroid-videoview

提问by priyanka

I am beginner in android development and try to play video from link. But it's giving error "sorry,we can't play this video". I tried so many links but for all links its show same error.

我是 android 开发的初学者,并尝试从链接播放视频。但它给出了错误"sorry,we can't play this video"。我尝试了很多链接,但对于所有链接,它都显示相同的错误。

My code is the following

我的代码如下

public class VideoDemo extends Activity {

        private static final String path ="http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";
 private VideoView video;
 private MediaController ctlr;
 @Override
 public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            setContentView(R.layout.videoview);

            video = (VideoView) findViewById(R.id.video);
            video.setVideoPath(path);

            ctlr = new MediaController(this);
            ctlr.setMediaPlayer(video);
            video.setMediaController(ctlr);
            video.requestFocus();
     }
}


Logcat shows following error message:

Logcat 显示以下错误消息:

04-12 15:04:54.245: ERROR/PlayerDriver(554): HandleErrorEvent: PVMFErrTimeout

回答by Win Myo Htet

It has something to do with your link and content. Try the following two links:

它与您的链接和内容有关。尝试以下两个链接:

    String path="http://www.ted.com/talks/download/video/8584/talk/761";
    String path1="http://commonsware.com/misc/test2.3gp";

    Uri uri=Uri.parse(path1);

    VideoView video=(VideoView)findViewById(R.id.VideoView01);
    video.setVideoURI(uri);
    video.start();

Start with "path1", it is a small light weight video stream and then try the "path", it is a higher resolution than "path1", a perfect high resolution for the mobile phone.

从“path1”开始,它是一个小的轻量级视频流,然后尝试“path”,它比“path1”更高的分辨率,完美的手机高分辨率。

回答by gtsiolis

Try this:

尝试这个:

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

回答by Vaishali Sutariya

pDialog = new ProgressDialog(this);

    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar
    pDialog.show();

    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(this);
        mediacontroller.setAnchorView(mVideoView);      

        Uri videoUri = Uri.parse(videoUrl);
        mVideoView.setMediaController(mediacontroller);
        mVideoView.setVideoURI(videoUri);

    } catch (Exception e) {

        e.printStackTrace();
    }

    mVideoView.requestFocus();
    mVideoView.setOnPreparedListener(new OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            pDialog.dismiss();
            mVideoView.start();
        }
    });
    mVideoView.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
            finish();               
        }
    });

回答by Md. Sajedul Karim

You can do it using FullscreenVideoViewclass. Its a small library project. It's video progress dialog is build in. it's gradle is :

您可以使用FullscreenVideoView类来完成。它是一个小型图书馆项目。它的视频进度对话框是内置的。它的 gradle 是:

compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.0'

your VideoView xml is like this

你的 VideoView xml 是这样的

<com.github.rtoshiro.view.video.FullscreenVideoLayout
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

In your activity , initialize it using this way:

在您的活动中,使用以下方式对其进行初始化:

    FullscreenVideoLayout videoLayout;

videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
        videoLayout.setActivity(this);

        Uri videoUri = Uri.parse("YOUR_VIDEO_URL");
        try {
            videoLayout.setVideoURI(videoUri);

        } catch (IOException e) {
            e.printStackTrace();
        }

That's it. Happy coding :)

就是这样。快乐编码:)

If want to know more then visit here

如果想了解更多,请访问这里

Edit:gradle path has been updated. compile it now

编辑:gradle 路径已更新。现在编译

compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'

回答by Nudge

Try Exoplayer2

试试 Exoplayer2

https://github.com/google/ExoPlayer

https://github.com/google/ExoPlayer

It is highly customisable

它是高度可定制的

    private void initializePlayer() {
         player = ExoPlayerFactory.newSimpleInstance(
             new DefaultRenderersFactory(this),
             new DefaultTrackSelector(), new DefaultLoadControl());

         playerView.setPlayer(player);

         player.setPlayWhenReady(playWhenReady);
         player.seekTo(currentWindow, playbackPosition);

Uri uri = Uri.parse(getString(R.string.media_url_mp3));
     MediaSource mediaSource = buildMediaSource(uri);
     player.prepare(mediaSource, true, false);
    }

private MediaSource buildMediaSource(Uri uri) {
  return new ExtractorMediaSource.Factory(
      new DefaultHttpDataSourceFactory("exoplayer-codelab")).
      createMediaSource(uri);
}

@Override
public void onStart() {
 super.onStart();
 if (Util.SDK_INT > 23) {
   initializePlayer();
 }
}

Check this url for more details

检查此网址了解更多详情

https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2

https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2

回答by Adnan Abdollah Zaki

please check this link : http://developer.android.com/guide/appendix/media-formats.html

请检查此链接:http: //developer.android.com/guide/appendix/media-formats.html

videoviewcan't support some codec.

videoview不能支持某些编解码器

i suggested you to use mediaplayer , when get "sorry , can't play video"

我建议您使用媒体播放器,当收到“抱歉,无法播放视频”时

回答by user7856586

I also got stuck with this issue. I got correct response from server, but couldn`t play video. After long time I found a solution here. Maybe, in future this link will be invalid. So, here is my correct code

我也被这个问题困住了。我得到了服务器的正确响应,但无法播放视频。很长一段时间后,我在这里找到了解决方案。也许,将来此链接将无效。所以,这是我的正确代码

    Uri video = Uri.parse("Your link should be in this place "); 
    mVideoView.setVideoURI(video); 

   mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
        @Override 
        public void onPrepared(MediaPlayer mediaPlayer) {
  // here write another part of code, which provides starting the video
  }}

回答by kontashi35

Check this UniversalVideoViewlibrary its simple and straight forward with controller as well.

检查这个UniversalVideoView库,它也很简单,直接使用控制器。

Here is the code to play the video
Add this dependancyy in build.gradle

这是播放视频的代码
在 build.gradle 中添加此依赖项

 implementation 'com.linsea:universalvideoview:1.1.0@aar'

Java Code

Java代码

        UniversalVideoView mVideoView = findViewById(R.id.videoView);
        Uri uri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/contactform-d9534.appspot.com/o/Vexento%20-%20Masked%20Heroes.mp4?alt=media&token=74c2e448-5b1b-47b7-b761-66409bcfbf56");
        mVideoView.setVideoURI(uri);
        UniversalMediaController mMediaController = findViewById(R.id.media_controller);
        mVideoView.setMediaController(mMediaController);
        mVideoView.start();   

Xml Code

xml代码

<FrameLayout
            android:id="@+id/video_layout"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/black">

            <com.universalvideoview.UniversalVideoView
                android:id="@+id/videoView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                app:uvv_autoRotation="true"
                app:uvv_fitXY="false" />

            <com.universalvideoview.UniversalMediaController
                android:id="@+id/media_controller"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                app:uvv_scalable="true" />

        </FrameLayout>

回答by p.mathew13

Check whether your phone supports the video format or not.Even I had the problem when playing a 3gp file but it played a mp4 file perfectly.

检查您的手机是否支持视频格式。即使我在播放3gp文件时也有问题,但它完美播放了mp4文件。