缓存视频时如何在videoview中实现progressDialog [如youtube]

时间:2016-04-28 04:41:14

标签: android progressdialog

我已经实现了这段代码。 http://www.androidbegin.com/tutorial/android-video-streaming-videoview-tutorial/

但是它显示了整个活动的progressdialog,但我的要求是仅在videoview中显示progressDialog,就像在缓存视频时在youtube中明智地显示一样。

2 个答案:

答案 0 :(得分:1)

使用以下代码,您可以在视频缓冲时检测到:

 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
                        bufferingDialog.show();
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
                        bufferingDialog.dismiss();
                    return false;
                }
            });
        }
    });
    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            bufferingDialog.dismiss();
            return false;
        }
    });

还建议您查看this库。

答案 1 :(得分:0)

这可以帮助您尝试这种方式

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    ProgressBar progressBar = null;

    VideoView videoView = null;

    String videoUrl = "video path here";

    Context context = null;

    @Override
    public void onCreate(Bundle iclic) {
        super.onCreate(iclic);

        context = null;

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        videoView = (VideoView) findViewById(R.id.videoview);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        Uri videoUri = Uri.parse(videoUrl);

        videoView.setVideoURI(videoUri);
        videoView.start();

        progressBar.setVisibility(View.VISIBLE);

        videoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                            int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });

    }

}