单击按钮时如何播放视频?

时间:2019-06-16 20:52:59

标签: java android

我只想在单击“播放”按钮时播放视频,所以有人可以帮我吗?

这是我的代码,当用户为recyclerview中的剪辑选择海报时播放视频:


 private void initWeb(String s){

        if (isPlaying){
            player.release();

        }

        progressBar.setVisibility(GONE);

        webView.loadUrl(s);
        webView.setVisibility(VISIBLE);
        imgFull.setVisibility(VISIBLE);
        playerLayout.setVisibility(GONE);

    }

    public void iniMoviePlayer(String url,String type,Context context){

        Log.e("vTYpe :: ",type);

        if (type.equals("embed") || type.equals("vimeo") || type.equals("gdrive")){
            isVideo=false;
            initWeb(url);
        }else {
            isVideo=true;
            initVideoPlayer(url,context,type);
        }
    }


    public void initVideoPlayer(String url,Context context,String type){
        progressBar.setVisibility(VISIBLE);

        if (player!=null){
            player.release();

        }

        webView.setVisibility(GONE);
        imgFullWeb.setVisibility(GONE);
        playerLayout.setVisibility(VISIBLE);

        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new
                AdaptiveTrackSelection.Factory(bandwidthMeter);


        TrackSelector trackSelector = new
                DefaultTrackSelector(videoTrackSelectionFactory);

        player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
        player.setPlayWhenReady(true);
        simpleExoPlayerView.setPlayer(player);

        simpleExoPlayerView.setControllerVisibilityListener(new PlayerControlView.VisibilityListener() {
            @Override
            public void onVisibilityChange(int visibility) {
                Log.e("Visibil", String.valueOf(visibility));
                if (visibility==0){
                    imgBack.setVisibility(VISIBLE);
                    imgExternal.setVisibility(VISIBLE);
                    imgDownload.setVisibility(VISIBLE);
                    imgFull.setVisibility(VISIBLE);
                    imgfit.setVisibility(VISIBLE);
                    if(isFullScr) {
                        brightnessLayout.setVisibility(VISIBLE);
                        volumeLayout.setVisibility(VISIBLE);
                    }

                }else {

                    imgBack.setVisibility(GONE);
                    imgExternal.setVisibility(GONE);
                    imgDownload.setVisibility(GONE);
                    imgFull.setVisibility(GONE);
                    imgfit.setVisibility(GONE);
                    if(isFullScr) {
                        brightnessLayout.setVisibility(GONE);
                        volumeLayout.setVisibility(GONE);

                    }

                }
            }
        });

        commonVideoUrl=url;
        this.type=type;
        Uri uri = Uri.parse(url);

        MediaSource mediaSource = null;

        if (type.equals("hls")){
            mediaSource = hlsMediaSource(uri,context);

        }else if (type.equals("youtube")){
            Log.e("youtube url  :: ",url);
            extractYoutubeUrl(url,context,18);
        }
        else if (type.equals("youtube-live")){
            Log.e("youtube url  :: ",url);
            extractYoutubeUrl(url,context,133);
        }
        else if (type.equals("rtmp")){
            mediaSource=rtmpMediaSource(uri);
        }else {
            mediaSource=mediaSource(uri,context);
        }

        player.prepare(mediaSource, true, false);

        player.addListener(new Player.DefaultEventListener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {


                if (playWhenReady && playbackState == Player.STATE_READY) {

                    isPlaying=true;
                    progressBar.setVisibility(View.GONE);

                    Log.e("STATE PLAYER:::", String.valueOf(isPlaying));

                }
                else if (playbackState==Player.STATE_READY){
                    progressBar.setVisibility(View.GONE);
                    isPlaying=false;
                    Log.e("STATE PLAYER:::", String.valueOf(false));
                }
                else if (playbackState==Player.STATE_BUFFERING) {
                    isPlaying=false;
                    progressBar.setVisibility(VISIBLE);

                    Log.e("STATE PLAYER:::", String.valueOf(isPlaying));
                } else {
                    // player paused in any state
                    isPlaying=false;
                    Log.e("STATE PLAYER:::", String.valueOf(false));
                }

            }
        });

    }

这是单击海报时我的回收站视图通过的内容:

    iniMoviePlayer(String url,String type,Context context)

我知道应该在onCreate方法中创建按钮,但是我不知道如何确保仅在单击按钮时播放视频。目前,它会自动加载到ExoPlayer2中。

谢谢。

0 个答案:

没有答案