Mediaplayer在切换片段时没有暂停

Posted

技术标签:

【中文标题】Mediaplayer在切换片段时没有暂停【英文标题】:Mediaplayer not pausing while switching fragment 【发布时间】:2018-12-22 04:27:14 【问题描述】:

我是安卓工作室的新手。请帮忙。我有一个包含 3 个片段的应用程序,一个具有使用回收器适配器的视频视图列表,一个具有图像视图列表,另一个具有与没有 1 个片段相同的视频视图列表。第一个和最后一个片段使用相同的适配器。问题是,在将片段从第一个视频切换到第二个视频时,会一直播放。

 //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);
   

我想在切换片段时停止播放。

【问题讨论】:

【参考方案1】:

当您从一个片段分片到另一个片段时,会调用前一个片段的onPause()(让您有机会暂停视频)并调用新片段的onResume() 函数。如果尚未创建新片段,则在调用 onResume() 之前调用 onStartonCreate() 等方法。

您可以做什么,当片段失去焦点时,称为onPause(),暂停您的视频,当片段获得焦点时,如果它已经创建,您可以恢复它。

片段生命周期 (source)

【讨论】:

我知道,但是如何从片段 onPause 方法暂停视频,因为所有视频视图都在回收器视图中,如何从片段活动中访问它,这可能是一个愚蠢的问题,因为我是android新手,请帮忙。 onPrepared()被调用时,给你一个MediaPlayer对象mp,将该对象赋值给一个变量,并在Fragment的onPause()方法中暂停 还是不行,我已经更新了代码检查,请问哪里出了问题

以上是关于Mediaplayer在切换片段时没有暂停的主要内容,如果未能解决你的问题,请参考以下文章

视频没有在片段 ViewPager 中暂停

如何在所有活动和片段中使用 MediaPlayer 的单个实例?

android Mediaplayer

JavaFX 媒体 - 暂停();方法使 MediaPlayer 快进?

MediaPlayer 在暂停或 seekTo 后不会继续显示字幕 (.SRT)

Mediaplayer响应第二次点击暂停