如何从m3u8网址中找出质量并在exoplayer / videoview android中设置质量?

时间:2019-04-04 11:34:47

标签: android android-videoview exoplayer

如何从m3u8网址中检测出哪些类型的质量(240p,360p,720p,1080p等)可用?我猜改变比特率会改变播放器中视频的质量,但是改变比特率后会不会输出与质量相同的输出?

public void initExoPlayer(字符串url,int windowPosition,int playerPosition){

    try {

        playerView = findViewById(R.id.video_player);

        trackSelector = new DefaultTrackSelector();
        defaultTrackParam = trackSelector.buildUponParameters().build();
        trackSelector.setParameters(defaultTrackParam);
        DefaultTrackSelector.Parameters parameters = trackSelector.getParameters();


        Log.d(TAG, "maxVideoBitrate " + trackSelector.getParameters().maxVideoBitrate);

        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
    playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
        playerView.setPlayer(player);

        fac = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)));

        Log.d(TAG, "initExoPlayer Url " + videoUrl);
        videoSource = new HlsMediaSource.Factory(fac).createMediaSource(Uri.parse(url));
        player.prepare(videoSource);

        // Plays the video twice.
       player.addListener(new Player.EventListener() {
            @Override
            public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

            }

            @Override
            public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

                try {
                    Log.d(TAG, "trackGroups.length " + trackGroups.length + " trackSelections.length " + trackSelections.length);
                   }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onLoadingChanged(boolean isLoading) {
                if (isLoading) {
                    progress_videoDetail.setVisibility(View.VISIBLE);
                } else {
                    progress_videoDetail.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                switch (playbackState) {
                    case ExoPlayer.STATE_READY:
                        progress_videoDetail.setVisibility(View.GONE);
                        duration = player.getDuration();
                        //Toast.makeText(activity,"Duration "+duration,Toast.LENGTH_SHORT).show();
                        break;
                    case ExoPlayer.STATE_BUFFERING:
                        progress_videoDetail.setVisibility(View.VISIBLE);
                        break;
                    case ExoPlayer.STATE_ENDED:
                        progress_videoDetail.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onRepeatModeChanged(int repeatMode) {

            }

            @Override
            public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {
                progress_videoDetail.setVisibility(View.GONE);
                Snackbar.make(ll_mainVideoDetail, getResources().getString(R.string.some_error_occurrred), Snackbar.LENGTH_SHORT).show();
                releasePlayer();
            }

            @Override
            public void onPositionDiscontinuity(int reason) {
                progress_videoDetail.setVisibility(View.GONE);
            }

            @Override
            public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

            }

            @Override
            public void onSeekProcessed() {

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

1 个答案:

答案 0 :(得分:0)

您必须使用TrackSelectorinitExoPlayer中初始化trackSelector = new DefaultTrackSelector(/* context= */ this, trackSelectionFactory);

然后您可以使用此:

MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
int videoRenderer;
if (mappedTrackInfo != null) {
 for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
   if (mappedTrackInfo.getTypeSupport(C.TRACK_TYPE_VIDEO){
     videoRenderer = i;
   }
 }
}

TrackGroupArray qualityArray = mappedTrackInfo.getTrackGroups(videoRenderer).get(0);