Search in sources :

Example 1 with PlayerViewModel

use of com.zype.android.ui.player.PlayerViewModel in project zype-android by zype.

the class VideoDetailActivity method initModel.

// //////////
// Data
// 
private void initModel() {
    final Video video = DataRepository.getInstance(getApplication()).getVideoSync(mVideoId);
    if (video != null) {
        // Load player urls
        playerViewModel = ViewModelProviders.of(this).get(PlayerViewModel.class);
        PlayerViewModel.PlayerMode mediaType = null;
        switch(mType) {
            case PlayerFragment.TYPE_AUDIO_LOCAL:
            case PlayerFragment.TYPE_AUDIO_WEB:
                mediaType = PlayerViewModel.PlayerMode.AUDIO;
                break;
            case PlayerFragment.TYPE_VIDEO_LOCAL:
            case PlayerFragment.TYPE_VIDEO_WEB:
            case BaseVideoActivity.TYPE_WEB:
                mediaType = PlayerViewModel.PlayerMode.VIDEO;
                break;
        }
        playerViewModel.init(video.id, playlistId, mediaType);
        playerViewModel.getPlayerError().observe(this, playerErrorObserver);
        playerViewModel.getPlayerUrl().observe(this, playerUrlObserver);
        if (video.isZypeLive == 0) {
            updateDownloadUrls();
        } else {
            // showVideoThumbnail();
            videoDetailViewModel = new VideoDetailViewModel(getApplication());
            final Observer<Video> videoObserver = new Observer<Video>() {

                @Override
                public void onChanged(@Nullable Video video) {
                    if (VideoHelper.isLiveEventOnAir(video)) {
                        if (videoDetailViewModel.updateVideoOnAir(video)) {
                            showPlayer();
                        }
                    } else {
                        changeFragment(isChromecastConntected());
                        videoDetailViewModel.checkOnAir(mVideoId).observe(VideoDetailActivity.this, new Observer<Video>() {

                            @Override
                            public void onChanged(@Nullable Video video) {
                                videoDetailViewModel.onCleared();
                                videoDetailViewModel.updateVideoOnAir(video);
                                showPlayer();
                            }
                        });
                    }
                    videoDetailViewModel.getVideo(mVideoId).removeObserver(this);
                }
            };
            videoDetailViewModel.getVideo(mVideoId).observe(this, videoObserver);
        }
    }
}
Also used : Video(com.zype.android.Db.Entity.Video) Observer(androidx.lifecycle.Observer) PlayerViewModel(com.zype.android.ui.player.PlayerViewModel) Nullable(androidx.annotation.Nullable)

Example 2 with PlayerViewModel

use of com.zype.android.ui.player.PlayerViewModel in project zype-android by zype.

the class PlayerFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Logger.d("onActivityCreated()");
    // Initialize Google Cast
    castContext = CastContext.getSharedInstance();
    castSessionManager = CastContext.getSharedInstance().getSessionManager();
    setupCastListener();
    initIMA();
    initMediaSession();
    sensorViewModel = ViewModelProviders.of(getActivity()).get(SensorViewModel.class);
    playerViewModel = ViewModelProviders.of(getActivity()).get(PlayerViewModel.class);
    if (playerModeObserver == null)
        playerModeObserver = createPlayerModeObserver();
    if (playerUrlObserver == null)
        playerUrlObserver = createPlayerUrlObserver();
    if (playerErrorObserver == null)
        playerErrorObserver = createPlayerErrorObserver();
    playerViewModel.getPlayerMode().observe(this, playerModeObserver);
    playerViewModel.getPlayerUrl().observe(this, playerUrlObserver);
    playerViewModel.getPlayerError().observe(this, playerErrorObserver);
    if (playerViewModel.isTrailer().getValue()) {
        ImageButton buttonCloseTrailer = getView().findViewById(R.id.buttonCloseTrailer);
        buttonCloseTrailer.setVisibility(View.VISIBLE);
        buttonCloseTrailer.setOnClickListener(v -> {
            stop();
            videoViewModel.onVideoFinished(true);
            playerViewModel.setTrailerVideoId(null);
            setPortraitOrientation();
        });
        setLandscapeOrientation();
    }
    videoViewModel = ViewModelProviders.of(getActivity()).get(VideoDetailViewModel.class);
    videoViewModel.getVideo().observe(this, video -> {
        thumbnail = VideoHelper.getThumbnailByHeight(video, 480);
        if (thumbnail != null) {
            UiUtils.loadImage(thumbnail.getUrl(), R.drawable.placeholder_video, imageThumbnail);
        }
        initProgress(video);
    });
    videoViewModel.isFullscreen().observe(this, fullscreen -> {
        if (fullscreen != null) {
            updateFullscreenButton(fullscreen);
        }
    });
}
Also used : SensorViewModel(com.zype.android.ui.player.SensorViewModel) ImageButton(android.widget.ImageButton) VideoDetailViewModel(com.zype.android.ui.video_details.VideoDetailViewModel) PlayerViewModel(com.zype.android.ui.player.PlayerViewModel)

Aggregations

PlayerViewModel (com.zype.android.ui.player.PlayerViewModel)2 ImageButton (android.widget.ImageButton)1 Nullable (androidx.annotation.Nullable)1 Observer (androidx.lifecycle.Observer)1 Video (com.zype.android.Db.Entity.Video)1 SensorViewModel (com.zype.android.ui.player.SensorViewModel)1 VideoDetailViewModel (com.zype.android.ui.video_details.VideoDetailViewModel)1