简体   繁体   English

Android VideoView在特定时间后停止流式传输

[英]Android VideoView stop streaming after specific time

I am playing live stream from this URL in android 我正在从Android中的此URL播放实时流

So far, I have managed to play the video in videoView, the problem was the video stopped exactly after 23 seconds, so I have used videoview method setOnCompletionListener (...) in order to start the video again, however, this provide bad experience for the viewer, because it stop every 23 seconds and start again, also miss several frames. 到目前为止,我已经设法在videoView中播放视频,问题是视频在23秒后恰好停止了,因此我使用videoview方法setOnCompletionListener (...)来重新启动视频,但是,这提供了糟糕的体验对于观看者来说,因为它每23秒停止一次并重新开始,所以也会错过几帧。

So my question is " how to make videoView buffer next part of video while playing current buffered video. 所以我的问题是“在播放当前缓冲的视频时如何使videoView缓冲视频的下一部分。

here is my code 这是我的代码

    public class TvActivity extends Activity {

    // Declare variables
    ProgressDialog pDialog;
    VideoView videoview;


    // Insert your Video URL
    String VideoURL = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the layout from video_main.xml
        setContentView(R.layout.videoview_main);
        // Find your VideoView in your video_main.xml layout
        videoview = (VideoView) findViewById(R.id.VideoView);
        // Execute StreamVideo AsyncTask
        VideoURL = "http://ns3622101.ip-149-202-201.eu:8000/live/fr443500/75019pa/286.ts";

        // Create a progressbar
        pDialog = new ProgressDialog(TvActivity.this);
        // Set progressbar title
        pDialog.setTitle("Video Streaming ");
        // Set progressbar message
        pDialog.setMessage("buffering ...");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(true);
        // Show progressbar
        pDialog.show();

        try {
            // Start the MediaController
            final MediaController mediacontroller = new MediaController(
                    TvActivity.this);
            mediacontroller.setAnchorView(videoview);

            // Get the URL from String VideoURL
            final Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);
            videoview.requestFocus();
            videoview.setOnPreparedListener(new OnPreparedListener() {

                // Close the progress bar and play the video
                public void onPrepared(MediaPlayer mp) {
                    pDialog.dismiss();
                    videoview.start();
                }
            });

            videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    videoview.stopPlayback();
                    videoview.setVideoURI(video);
                    videoview.requestFocus();
                    videoview.start();
                }
            });

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }


    }



}

As you are playing video from Live Stream URL I suggest you to Stream video by using third party Player to stream video and for buffer the next part of video, I suggest you to use Android GirrafePlayer it's good Player get it from here 当您从Live Stream URL播放视频时,建议您使用第三方播放器流式传输视频并缓冲视频的下一部分,建议您使用Android GirrafePlayer,这是很好的播放器,请从此处获取

And as you are Streaming .ts file I don't think android videoview will stream this video file so try to stream this video file with .ts file streaming supported Player. as you are Streaming .ts file我认为android videoview不会流式传输该视频文件,因此请尝试使用.ts文件流式支持的播放器来流式传输该视频文件。

For Live streaming and also for off-line audio or video playing I suggest used ExoPlayer 对于实时流媒体以及离线音频或视频播放,我建议使用ExoPlayer

Which is very good library for playing audio and video developed by Google. 这是用于播放Google开发的音频和视频的很好的库。 Short Intro is given bellow: 简短的介绍如下:

"ExoPlayer is an application level media player for Android. It provides an alternative to Android's MediaPlayer API for playing audio and video both locally and over the Internet. ExoPlayer supports features not currently supported by Android's MediaPlayer API, including DASH and SmoothStreaming adaptive playbacks. Unlike the MediaPlayer API, ExoPlayer is easy to customize and extend, and can be updated through Play Store application updates." “ ExoPlayer是适用于Android的应用程序级媒体播放器。它提供了Android MediaPlayer API的替代方案,可以在本地和Internet上播放音频和视频。ExoPlayer支持Android MediaPlayer API当前不支持的功能,包括DASH和SmoothStreaming自适应播放。 Mediaolayer API,ExoPlayer易于定制和扩展,可以通过Play商店应用程序更新进行更新。”

for more info search ExoPlayer on Github . 有关更多信息,请在Github上搜索ExoPlayer。

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

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