Android 切换ExoPlayer可见性不';t显示视频

Android 切换ExoPlayer可见性不';t显示视频,android,android-fragments,exoplayer,Android,Android Fragments,Exoplayer,我有一个VideoLayout,它使用ExoPlayer显示视频。VideoLayout在片段中使用了两次(previewVideoLayout和fullVideoLayout) 默认情况下,片段显示为previewVideoLayout;如果用户按下活动中的按钮,则会在带有动画的previewVideoLayout顶部显示fullVideoLayoutpreviewVideoLayout始终播放正常,但fullVideoLayout不显示视频,尽管它播放声音(位于previewVideoLay

我有一个
VideoLayout
,它使用
ExoPlayer
显示视频。
VideoLayout
片段中使用了两次(
previewVideoLayout
fullVideoLayout

默认情况下,片段显示为
previewVideoLayout
;如果用户按下活动中的按钮,则会在带有动画的
previewVideoLayout
顶部显示
fullVideoLayout
previewVideoLayout
始终播放正常,但
fullVideoLayout
不显示视频,尽管它播放声音(位于
previewVideoLayout
声音之上)

为什么在两种布局上调用相同的方法,一个播放,而第二个不播放?如果相关;
片段
位于
查看页面

维德拉约特:

public class VideoLayout {
public static final int MUTE_VOLUME = 0;
public static final int MAX_VOLUME = 100;
public static final int UPDATE_INTERVAL = 30;
protected static final String ARG_VIDEO_URL = "VIDEO_URL";
private final Context context;

private String videoUrl;
private HttpProxyCacheServer proxy;
private boolean visibleForUser;
private SimpleExoPlayer player;
private SimpleExoPlayerView simpleExoPlayerView;
private boolean pendingPlay;
private ProgressBar pbLoadIndicator;

private TimerTask updateProgressTask;
private Timer timer;
private ProgressBar videoProgress;

private ViewGroup layout;
private VideoFragment.VideoPreviewCallbacks videoPreviewCallbacks;
private String tag;

private boolean isFullVideo;

public VideoLayout(Context context, String videoUrl, boolean isFullVideo) {
    this.context = context;
    this.isFullVideo = isFullVideo;
    this.videoUrl = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";
}

public void onAttach(Context context) {
    if (context instanceof VideoFragment.VideoPreviewCallbacks) {
        videoPreviewCallbacks = (VideoFragment.VideoPreviewCallbacks) context;
    }
}

public void onCreate() {
    this.videoUrl = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";
    initPlayer();
}

public void onCreateView(ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout = (ViewGroup) inflater.inflate(getLayoutRes(), null);
    String tag = createTag();

    if (parent.findViewWithTag(tag) == null) {
        layout.setTag(tag);
        parent.addView(layout, parent.getChildCount());
    }
}

public void onViewCreated(View view) {
    bindViews(view);
    setupPlayerView();
}

public void onResume() {
    if (pendingPlay && visibleForUser) {
        pendingPlay = false;
        startPlayer();
    }
}

public void onPause() {
    pausePlayer();
}

public void onDestroy() {
    player.release();
}


public void setUserVisibleHint(boolean isVisibleToUser) {
    visibleForUser = isVisibleToUser;

    if (layout != null) {
        layout.setVisibility(isVisibleToUser ? VISIBLE : GONE);
    }
    if (visibleForUser && simpleExoPlayerView != null) {
        startPlayer();
    } else if (!visibleForUser && simpleExoPlayerView != null) {
        pausePlayer();
    } else if (visibleForUser) {
        pendingPlay = true;
    }
}

private void bindViews(View view) {
    simpleExoPlayerView = (SimpleExoPlayerView) view.findViewById(R.id.simpleExoPlayerView);
    simpleExoPlayerView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
    pbLoadIndicator = (ProgressBar) view.findViewById(R.id.pbLoadIndicator);
    videoProgress = (ProgressBar) view.findViewById(R.id.progress_video);
}

private void setupPlayerView() {
    simpleExoPlayerView.setUseController(false);
    simpleExoPlayerView.setPlayer(player);
}

private void initPlayer() {
    if (player == null) {
        initProxy();

        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        final LoopingMediaSource loopingSource = getVideoPlayerMediaSource(bandwidthMeter);

        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
        LoadControl loadControl = new DefaultLoadControl();

        player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
        player.addListener(new PlayerEventListener() {
            @Override
            public void onLoadingChanged(boolean isLoading) {
                if (pbLoadIndicator != null)
                    pbLoadIndicator.setVisibility(isLoading ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (pbLoadIndicator != null)
                    pbLoadIndicator.setVisibility(playWhenReady ? View.GONE : View.VISIBLE);
            }
        });
        player.prepare(loopingSource);
        player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    }
}

private void initProxy() {
    proxy = VideoCache.getProxy(getContext());
}

@NonNull
private LoopingMediaSource getVideoPlayerMediaSource(DefaultBandwidthMeter bandwidthMeter) {
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(),
            Util.getUserAgent(getContext(), "lifive.buy"), bandwidthMeter);

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

    Uri url = Uri.parse(videoUrl);
    MediaSource videoSource;

    if (videoUrl.contains(".mp4")) {
        url = Uri.parse(proxy.getProxyUrl(videoUrl));
        videoSource = new ExtractorMediaSource(url,
                dataSourceFactory, extractorsFactory, null, null);
    } else {
        videoSource = new HlsMediaSource(url, dataSourceFactory, null, null);
    }


    return new LoopingMediaSource(videoSource);
}

public void enableProgress(boolean enable) {
    videoProgress.setVisibility(enable ? VISIBLE : View.GONE);
}


private void startPlayer() {
    player.setPlayWhenReady(true);
    createUpdateTimer();
}


/**
 * creates the timer that updates the progress bar
 */
private void createUpdateTimer() {
    cancelUpdateTimer();
    updateProgressTask = new TimerTask() {
        @Override
        public void run() {
            if (player != null) {
                notifyUpdate();
            }
        }
    };
    timer = new Timer();
    timer.scheduleAtFixedRate(updateProgressTask, 0, UPDATE_INTERVAL);
}

/**
 * cancels the timer that updates the progress bar
 */
private void cancelUpdateTimer() {
    if (timer != null) {
        timer.cancel();
    }
}

/**
 * notifies that the UI should be updated. See createUpdateTimer
 */
private void notifyUpdate() {
    new Handler(Looper.getMainLooper()).post(() -> {
        updateProgressBar();
        if (player.getDuration() > 0 && videoPreviewCallbacks != null) {
            videoPreviewCallbacks.onProgress(player.getCurrentPosition(), player.getDuration());
        }
    });
}

private void updateProgressBar() {
    videoProgress.setMax((int) player.getDuration());
    videoProgress.setProgress((int) player.getCurrentPosition());
}

private void pausePlayer() {
    pendingPlay = true;

    player.setPlayWhenReady(false);
    player.seekTo(1);

    cancelUpdateTimer();
}

public int getVisibility() {
    return layout == null ? GONE : layout.getVisibility();
}

public void muteSound(boolean on) {
    player.setVolume(on ? MUTE_VOLUME : MAX_VOLUME);
}

private int getLayoutRes() {
    return isFullVideo ? R.layout.layout_full_video_player : R.layout.layout_video_player;
}

private String createTag() {
    if (this.tag == null)
        this.tag = "VideoLayout" + hashCode();
    return this.tag;
}

private Context getContext() {
    return context.getApplicationContext();
}
片段:

public class VideoFragment extends Fragment {

protected static final String ARG_VIDEO_URL = "VIDEO_URL";
private static final String ARG_FULL_VIDEO_URL = "FULL_VIDEO_URL";
private VideoLayout previewVideoLayout;
private VideoLayout fullVideoLayout;

private String videoUrl;
private String fullVideoUrl;
private ViewGroup parent;

public VideoFragment() {
}

/**
 * @param videoUrl video url
 * @return A new instance of fragment VideoPreviewFragment.
 */
public static VideoFragment newInstance(String videoUrl, String fullVideoUrl) {
    VideoFragment fragment = new VideoFragment();
    Bundle args = new Bundle();
    args.putString(ARG_VIDEO_URL, videoUrl);
    args.putString(ARG_FULL_VIDEO_URL, fullVideoUrl);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof VideoPreviewCallbacks) {
        if (getCurrentLayout() != null)
            getCurrentLayout().onAttach(getContext());
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        videoUrl = getArguments().getString(ARG_VIDEO_URL);
        fullVideoUrl = getArguments().getString(ARG_FULL_VIDEO_URL);

        if (previewVideoLayout == null) {
            previewVideoLayout = new VideoLayout(getContext(), videoUrl, false);
            previewVideoLayout.setUserVisibleHint(getUserVisibleHint());
            previewVideoLayout.onCreate();
        }
        if (fullVideoLayout == null && fullVideoUrl != null) {
        }
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_video_preview, container, false);

    parent = (ViewGroup) inflate.findViewById(R.id.fragment_video_preview_root);
    previewVideoLayout.onCreateView(parent);

    return inflate;
}

@Override
public void onViewCreated(View view, Bundle savedInstance) {
    super.onViewCreated(view, savedInstance);
    previewVideoLayout.onViewCreated(view);
}

@Override
public void onResume() {
    super.onResume();
    if (previewVideoLayout != null)
        previewVideoLayout.onResume();
    if (fullVideoLayout != null)
        fullVideoLayout.onResume();
}

@Override
public void onPause() {
    super.onPause();
    if (previewVideoLayout != null)
        previewVideoLayout.onPause();
    if (fullVideoLayout != null)
        fullVideoLayout.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (previewVideoLayout != null)
        previewVideoLayout.onDestroy();
    if (fullVideoLayout != null)
        fullVideoLayout.onDestroy();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (previewVideoLayout != null)
        previewVideoLayout.setUserVisibleHint(isVisibleToUser);
    if (fullVideoLayout != null)
        fullVideoLayout.setUserVisibleHint(isVisibleToUser);
}

public void enableProgress(boolean enable) {
    if (previewVideoLayout != null)
        previewVideoLayout.enableProgress(enable);
    if (fullVideoLayout != null)
        fullVideoLayout.enableProgress(enable);
}

private VideoLayout getCurrentLayout() {
    if (fullVideoLayout != null)
        return fullVideoLayout.getVisibility() == View.VISIBLE ? fullVideoLayout : previewVideoLayout;
    return previewVideoLayout;
}

public void muteSound(boolean on) {
    if(previewVideoLayout!=null)
        previewVideoLayout.muteSound(on);
    if(fullVideoLayout!=null)
        fullVideoLayout.muteSound(on);
}

public void showFullVideo() {
    fullVideoLayout = new VideoLayout(getContext(), fullVideoUrl, true);
    fullVideoLayout.setUserVisibleHint(getUserVisibleHint());
    fullVideoLayout.onCreate();
    fullVideoLayout.onCreateView(parent);
    fullVideoLayout.onViewCreated(getView());
    fullVideoLayout.onResume();
}

public interface VideoPreviewCallbacks {
    void onProgress(long currentMillis, long durationInMillis);
}