I have added exoplayer in recycler view which is inside the nested scroll view. the player is initialized.but I am facing some issues with that.
Whenever I m changing the fragment or scroll to bottom(nested scroll) video keeps playing in the background.
When I change the activity and came back to the same all videos start playing automatically.
Here is the main layout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="@+id/swipe"
android:background="?android:attr/windowBackground"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ForYou">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="@dimen/_5sdp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here is sublayout
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginBottom="@dimen/_8sdp"
android:layout_width="match_parent"
android:src="@drawable/backgroundgraident"
android:layout_height="wrap_content">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_marginTop="20dp"
android:background="#000000"
android:clickable="true"
android:visibility="gone"
app:bar_height="@dimen/_1sdp"
app:use_controller="true" />
</LinearLayout>
Java Code
private void initializePlayer(MyviewHolder hol , int pos) {
player = ExoPlayerFactory.newSimpleInstance(context);
MediaSource mediaSource = buildMediaSource( mediaUri );
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.prepare(mediaSource, false, false);
player.setVideoScalingMode( C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
playerView.setControlDispatcher( new MyDefaultControlDispatcher() );
playerView.setPlayer(player);
}
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(context, "exoplayer-codelab");
return new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
}
public void releasePlayer() {
if (player != null) {
if(player.isPlaying()) {
player.stop();
}
playWhenReady = player.getPlayWhenReady();
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
player.release();
player = null;
}
}