I am unable to stop the Video Playback when the viewpager is scrolled.
This is my MainActivity's Viewpager scrollListener code :
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
simpleExoPlayer1.stop(); //error is here
Log.e("onPageScrolled", "onPageScrolled");
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
Log.e("onPageSelected", "onPageSelected");
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
Log.e("onPageScrolStateChang", "onPageScrollStateChanged");
}
});
myAdapter = new VideoSliderAdapter(getApplicationContext(), videoPaths,
MainActivity2.this, this);
viewPager.setAdapter(myAdapter);
}
@Override
public void clicktoupdate(ImageView playPause, int position,
PlayerView playerView,
SimpleExoPlayer simpleExoPlayer1) {
// get data
Toast.makeText(this, "Playing..", Toast.LENGTH_SHORT).show();
Uri videoUri = Uri.parse(videoPaths.get(position));
playPause.setVisibility(View.GONE);
playerView.setVisibility(View.VISIBLE);
simpleExoPlayer1 = new SimpleExoPlayer.Builder(getApplicationContext())
.setSeekBackIncrementMs(5000)
.setSeekForwardIncrementMs(5000)
.build();
playerView.setPlayer(simpleExoPlayer1);
playerView.setKeepScreenOn(true);
simpleExoPlayer1.addListener(new Player.Listener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_BUFFERING) {
// progressBar.setVisibility(View.VISIBLE);
} else if (playbackState == Player.STATE_READY) {
// progressBar.setVisibility(View.GONE);
}
}
});
MediaItem mediaItem = MediaItem.fromUri(videoUri);
simpleExoPlayer1.setMediaItem(mediaItem);
simpleExoPlayer1.prepare();
simpleExoPlayer1.play();
}
This is my Adapter's Viewholder code :
public class ViewHolder extends RecyclerView.ViewHolder {
PlayerView playerView;
ImageView thumbnailImage;
ImageView playPauseBtn;
ImageView bt_fullscreen, bt_lockscreen;
SimpleExoPlayer simpleExoPlayer;
ProgressBar progressBar;
LinearLayout sec_mid, sec_bottom;
public ViewHolder(@NonNull View view) {
super(view);
playerView = view.findViewById(R.id.statusSliderVideo);
thumbnailImage = view.findViewById(R.id.statusSliderThumbnailImage);
playPauseBtn = view.findViewById(R.id.playPauseBtn);
progressBar = view.findViewById(R.id.progress_bar);
bt_fullscreen = view.findViewById(R.id.bt_fullscreen);
bt_lockscreen = view.findViewById(R.id.exo_lock);
sec_mid = view.findViewById(R.id.sec_controlvid1);
sec_bottom = view.findViewById(R.id.sec_controlvid2);
playPauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
onitemclicklisteners.clicktoupdate(playPauseBtn, position, playerView, simpleExoPlayer);
}
}
});
}
}
public interface Onitemclicklisteners {
void clicktoupdate(ImageView playPause, int position, PlayerView playerView, SimpleExoPlayer simpleExoPlayer);
}
As soon as the App starts, this is the Error I am getting :
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.SimpleExoPlayer.stop()' on a null object reference
I think I have to make the
simpleExoPlayer1 global
in MainActivity , please help me fix this issue. Thanks in advance
CodePudding user response:
Have you tried null checking? Something like this:
if (simpleExoPlayer1 != null) {
simpleExoPlayer1.stop();
}
That'll fix the crash.
But maybe a problem is elsewhere in you code; have you initialized simpleExoPlayer1
before registering OnPageChangeCallback
?
CodePudding user response:
The simpleExoPlayer1
object is being instantiated in your clicktoupdate()
interface override. The clicktoupdate()
function only gets called when the view holder is clicked. So when you first open the app, the onPageScrolled()
must be getting called for some reason and it is trying to stop the simpleExoPlayer1
. However, the simpleExoPlayer1
has not been instantiated, since the user has not clicked the view holder! So that is why it is crashing - the simpleExoPlayer1
object has not been instantiated and therefore is null.
There are a few things that you could do to solve this, but I would have thought that the simpleExoPlayer1
could be instantiated once inside the activity's onCreate()
method, so moving it there should fix your issue.
Try moving the following snippet to inside your onCreate(), before the view pager listeners are called.
@Override
public void onCreate(Bundle savedInstanceState) {
...
simpleExoPlayer1 = new SimpleExoPlayer.Builder(getApplicationContext())
.setSeekBackIncrementMs(5000)
.setSeekForwardIncrementMs(5000)
.build();
viewPager.registerOnPageChangeCallback(...);
...
}
Final tip: use camel case to define your methods & variables For example, clicktoupdate()
should be clickToUpdate()
. It makes it way more readable :)