簡體   English   中英

如何使用VideoView / MediaPlayer在Android上播放流媒體視頻

[英]How to play streaming video at Android by using VideoView/MediaPlayer

我有一個支持流媒體視頻的網絡服務器。 所以它正確處理范圍HTTP標頭。

有沒有辦法使用VideoView / MediaPlayer按塊加載視頻塊? 目前我只是使用VideoView.setVideoURI方法設置視頻網址,並開始在onPrepared處理程序上播放視頻。 正如我所見,onPrepared處理程序在加載整個視頻文件后被調用。

你可以嘗試這樣的事情:

try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

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

        } finally {
            videoview.setOnErrorListener(new OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    CommonUtilities.showToast(VideoViewActivity.this, "Video Format not supported by device.");
                    VideoViewActivity.this.finish();
                    return true;
                }
            });
        }

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

請嘗試使用此代碼,它可以緩沖並與mediacontroller一起播放。

     try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM