Home > Net >  When TimeBar scrubbed video change position accordingly in ExoPlayer
When TimeBar scrubbed video change position accordingly in ExoPlayer

Time:08-29

I'm using StyledPlayerView for controls. I want to implement that while user moves TimeBar video also change its position simultaneously(not after user stopped scrubbing). I know that I can use TimeBar.OnScrubListener(), but I cannot find a way to implement that method to default StyledPlayerView. Is there a way to do that? Thanks.

public class ShowTutorialFragment extends Fragment {

private ExoPlayer player;
private NavController navController;
private ImageView imageViewClose;
private StyledPlayerView videoViewTutorial;
private boolean goBackLockScreen = false;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    if (bundle != null && bundle.getString("whereItCame") == "LockScreen")
        goBackLockScreen = true;
    return inflater.inflate(R.layout.fragment_show_tutorial, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initialize(view);
    setListeners();

}

@Override
public void onPause() {
    super.onPause();
    player.pause();
}

private void initialize(View view) {
    player = new ExoPlayer.Builder(requireContext()).build();
    navController = Navigation.findNavController(view);
    imageViewClose = view.findViewById(R.id.image_view_close_dialog);
    videoViewTutorial = view.findViewById(R.id.video_view_tutorial);
    playVideo();
}

private void playVideo() {
    String path = "android.resource://"   getActivity().getPackageName()   "/"   R.raw.tutorial_video;
    MediaItem mediaItem = MediaItem.fromUri(path);

    videoViewTutorial.setPlayer(player); // Set the media item to be played.
    videoViewTutorial.setShowPreviousButton(false);
    videoViewTutorial.setShowNextButton(false);
    player.setMediaItem(mediaItem); // Prepare the player.
    player.prepare();
    player.play();
    Log.i("track", "playVideo: "  player.getTrackSelector().toString());
   /* setXButtonVisible();*/
}

private void setListeners() {
    imageViewClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            player.release();
            if (goBackLockScreen) {
                navController.navigate(R.id.action_showTutorialFragment_to_lockScreenFragment);
            } else navController.navigate(R.id.action_showTutorialFragment_to_mainMenu);
        }
    });

    videoViewTutorial.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            player.pause();
            videoViewTutorial.hideController();
            return false;
        }
    });

}

}

my xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/layout_fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context="com.atgeotech.erpintel.views.tutorial.ShowTutorialFragment">


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"

    >

    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@ id/video_view_tutorial"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:longClickable="true"
        app:show_buffering="when_playing"
        app:show_shuffle_button="true"
        app:surface_type="surface_view"
        tools:ignore="SpeakableTextPresentCheck" />

    <ImageView
        android:id="@ id/image_view_close_dialog"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_gravity="end"
        android:layout_margin="8dp"
        android:background="@color/faded_black"
        android:clickable="true"
        android:focusable="true"
        android:visibility="visible"
        app:srcCompat="@drawable/ic_close_dialog"
        tools:ignore="SpeakableTextPresentCheck" />

</FrameLayout>

CodePudding user response:

Issue which I tried to explain is that if I want to change video position during scrubbing, I must use TimeBar.OnScrubListener.onScrubMove(). In order to use this, function must access to DefaultTimeBar inside StyledPlayerView, and I did not know how to do it, yet solution is easy.

I just need to add global DefaultTimeBar variable,

private DefaultTimeBar timeBar;

then initialize it through videoViewTutorial variable

timeBar = videoViewTutorial.findViewById(R.id.exo_progress);//Solution

then I just use the listener to achieve my desired result

timeBar.addListener(new TimeBar.OnScrubListener() { //Solution
        @Override
        public void onScrubStart(TimeBar timeBar, long position) {

        }

        @Override
        public void onScrubMove(TimeBar timeBar, long position) {
            player.seekTo(position);
        }

        @Override
        public void onScrubStop(TimeBar timeBar, long position, boolean canceled) {

        }
    });
  • Related