切换ExoPlayer可见性不显示视频

时间:2017-04-01 19:17:42

标签: android android-fragments exoplayer

我有VideoLayout使用ExoPlayer显示视频。 VideoLayoutFragment(代码中为previewVideoLayoutfullVideoLayout)中使用了previewVideoLayout两次。

片段默认显示为fullVideoLayout;如果用户按下“活动”中的按钮,则会在previewVideoLayout上显示previewVideoLayout并带有动画。 fullVideoLayout总是正常播放,但previewVideoLayout 不显示视频,但播放声音(在Fragment声音之上)。

有没有理由,在两个布局上调用相同的方法,一个播放但第二个不播放?如果它是相关的; ViewPager位于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(); } 内。

VideLayout:

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);
}

片段:

{{1}}

0 个答案:

没有答案