sum20156
sum20156

Reputation: 734

Mediaplayer not pausing while switching fragment

I am new to android studio. Please help. I have an app with 3 fragments, one have list of video view using recycler adapter, one have list of image view, other have same list of video view as no 1 fragment. First and last fragment are using same adapter. The problem is that while switching fragmment from first one to second video keeps playing.

 //fragment code
 public class funny_moments extends Fragment {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("funnymomments");
 ArrayList<funny_momments_row> data;
 funny_momments_adapter adapter;
 VideoView mVideoView;
 View pos;

private RecyclerView mRecyclerView;
public funny_moments() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_funny_moments, container, false);
    mRecyclerView =(RecyclerView) view.findViewById(R.id.recylerview);
    mVideoView = (VideoView)view.findViewById(R.id.videoView);

    data = new ArrayList<>();
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot videoSnapshot : dataSnapshot.getChildren()){
                funny_momments_row video = new funny_momments_row(videoSnapshot.child("like").getValue().toString(),videoSnapshot.child("url").getValue().toString());

                data.add(video);

            }

           adapter = new funny_momments_adapter(getActivity(),data,mCommunication);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });



    // Inflate the layout for this fragment
    return view;
}
  @Override
public void onPause() {
    adapter.mMediaPlayer.pause();
    super.onPause();

}


//adapter code
 public class funny_momments_adapter extends RecyclerView.Adapter<funny_momments_adapter.funnyVideoHolder> {
private final LayoutInflater inflator;
List<funny_momments_row> data = Collections.emptyList();
communication mCommunicator;
Context mContext;



public funny_momments_adapter(Context context, List<funny_momments_row> data, communication communicator){

    inflator = LayoutInflater.from(context);
    this.data = data;
    this.mContext = context;
    mCommunicator = communicator;
}
@NonNull
@Override
public funnyVideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = inflator.inflate(R.layout.funny_momments_row,viewGroup,false);
        funnyVideoHolder funnyHolder = new funnyVideoHolder(view,mCommunicator);
    return funnyHolder;
}

@Override
public void onBindViewHolder(@NonNull final funnyVideoHolder viewHolder, int i) {

    funny_momments_row current = data.get(i);
    VideoView f1 = viewHolder.mVideoView;
    viewHolder.mVideoView.setVideoURI(Uri.parse(current.getUrl()));

    viewHolder.likecount.setText(current.getLike());

    viewHolder.mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(final MediaPlayer mp) {
            FragmentManager manager = ((AppCompatActivity)mContext).getSupportFragmentManager();
            Fragment f = manager.findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + 2);

            mMediaPlayer = mp;

            viewHolder.playpause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mp.isPlaying()) {
                        mp.pause();
                    }
                    else {
                        mp.start();
                    }
                }
            });
        }
    });

    viewHolder.playpause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (viewHolder.mVideoView.isPlaying()) {
                viewHolder.mVideoView.requestFocus();
                viewHolder.mVideoView.pause();
            }
            else {
                viewHolder.mVideoView.requestFocus();
                viewHolder.mVideoView.start();
            }
        }
    });


}



@Override
public int getItemCount() {
    return data.size();
}

   public class funnyVideoHolder extends  RecyclerView.ViewHolder implements View.OnClickListener{

    VideoView mVideoView;
    ImageView likeicon;
    TextView likecount;
    ImageView playpause;
    communication mCommunication;
    public funnyVideoHolder(@NonNull View itemView,communication communication) {
        super(itemView);
        mVideoView = (VideoView)itemView.findViewById(R.id.videoView);
        likeicon = (ImageView)itemView.findViewById(R.id.likeicon);
        likecount = (TextView)itemView.findViewById(R.id.likecount);
        playpause = (ImageView)itemView.findViewById(R.id.playpause);
        mCommunication = communication;

    }

   @Override
   public void onClick(View v) {
        mCommunication.respond(v);
   }

} }

I want to stop playback while switching fragment.

Upvotes: 0

Views: 626

Answers (1)

touhid udoy
touhid udoy

Reputation: 4442

When you fragment from one fragment to another, previous fragment's onPause() is called (giving you the opportunity to pause the video) and new fragment's onResume() function is called. If new fragment was not already created, onStart,onCreate() etc method are called before onResume() is called.

What you can do, when a fragment gets out of focus, thus called onPause(), pause your video, and when a fragment get in focus, you can resume it if it was already create.

Fragment Lifecycle (source) Fragment Lifecycle

Upvotes: 1

Related Questions