1

I'm trying to replace an ExoPlayer2 view with an ImageView if the video media file isn't available.

    if(stepVideo != null) {
        mPlaceHolderIv.setVisibility(View.GONE);
        exoPlayerView.setVisibility(View.VISIBLE);
        initializePlayer(Uri.parse(stepVideo));
        //initialize MediaSession
        initializeMediaSession();
    } else if(stepThumbUrl != null) {
        mPlaceHolderIv.setVisibility(View.GONE);
        exoPlayerView.setVisibility(View.VISIBLE);
        initializePlayer(Uri.parse(stepThumbUrl));
        //initialize MediaSession
        initializeMediaSession();
    } else {
        exoPlayerView.setVisibility(View.GONE);
        releasePlayer();
        mPlaceHolderIv.setVisibility(View.VISIBLE);
        Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(mPlaceHolderIv);
    }

I know the else is being called because the placeholder image is being shown; however, the playback controls are still visible. I've tried all the answers here and still the control view (play, pause, etc.) are shown. Any ideas?

Note: I've overwritten the custom controls with a file labled as exo_playback_control_view.xml.

Edit: Fixed the problem for showing the controls when view was GONE, but new question is why does the xml for SimpleExoPlayerView need to have a app:use_controller="false" and then set it to true in java when initialized and false again when released? Why doesn't the control view disappear with the rest of the SimpleExoPlayerView?

Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • "I know the else if is being called because the placeholder image is being shown" is the placeholder set at GONE in the layout XML as well? – Zun Jun 26 '18 at 07:43
  • It wasn't, but I just did so for both the ExoPlayerView and the IV in the .xml file, same problem. Shouldn't matter though, as this method is definitely being called when creating the activity view, so the visibility change should occur. Just not sure why the playback controls remain when I've released the player and set the view to GONE... – Mr.Drew Jun 26 '18 at 12:25

1 Answers1

1

Not sure if this can help, but if you just want to replace a view with another, why not use ViewSwitcher or ViewAnimator?

The 2 child views (the placeholder and the player view/layout) would be those that you switch between, and you call showNext to switch to the other view.

You can also use these nice helper functions:

fun ViewAnimator.setViewToSwitchTo(viewToSwitchTo: View, animate: Boolean = true): Boolean {
    if (currentView === viewToSwitchTo)
        return false
    for (i in 0 until childCount) {
        if (getChildAt(i) !== viewToSwitchTo)
            continue
        if (animate)
            displayedChild = i
        else {
            val outAnimation = this.outAnimation
            val inAnimation = this.inAnimation
            this.inAnimation = null
            this.outAnimation = null
            displayedChild = i
            this.inAnimation = inAnimation
            this.outAnimation = outAnimation
        }
        return true
    }
    return false
}

fun ViewAnimator.setViewToSwitchTo(@IdRes viewIdToSwitchTo: Int, animate: Boolean = true): Boolean {
    if (currentView.id == viewIdToSwitchTo)
        return false
    for (i in 0 until childCount) {
        if (getChildAt(i).id != viewIdToSwitchTo)
            continue
        if (animate)
            displayedChild = i
        else {
            val outAnimation = this.outAnimation
            val inAnimation = this.inAnimation
            this.inAnimation = null
            this.outAnimation = null
            displayedChild = i
            this.inAnimation = inAnimation
            this.outAnimation = outAnimation
        }
        return true
    }
    return false
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • That's cool! I'll have to try it out another time, I actually just figured out the problem. For some reason I had to ```app:use_controller="false"``` in the ExoPlayer xml view and use ```exoPlayerView.setUseController(true);``` when initializing the player. For some strange reason using ```exoPlayerView.setUseController(false);``` in the ```releasePlayer()``` method wasn't enough... strange. – Mr.Drew Jun 26 '18 at 12:36
  • I'm sure this works for any view, as long as you put it all in the correct hierarchy . – android developer Jun 26 '18 at 12:55