使用MediaPlayer播放声音时切换片段时为什么会出现白屏

时间:2019-05-16 16:48:01

标签: android firebase android-fragments android-recyclerview android-mediaplayer

因此,当我开始播放歌曲并切换片段时,歌曲会继续播放。当我在那之后切换回去时,我得到了一个完全白色的碎片。现在我有三个片段,但是只有两个片段有白屏。片段的内容是FirebaseRecyclerView。当我完成Mediaplayer的播放后,这个问题第一次出现。

我已经尝试在切换片段时重置媒体播放器,但是我没有设法做到这一点,因为对媒体播放器的访问位于Recyclerview的Viewholder中。这是因为列表上的每个条目都有其自己的媒体播放器控件。

public class MyPostsFragment extends PostListFragment {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    public MyPostsFragment() {
    }

    @Override
    public Query getQuery(DatabaseReference databaseReference) {

        return databaseReference.child("user-posts").child(user.getUid())
                .limitToFirst(100);
    }

}

这是对白屏片段之一的查询

public abstract class PostListFragment extends Fragment {

    private RecyclerView mRecycler;
    private FirebaseRecyclerAdapter<Post, PostViewHolder> mAdapter;
    private DatabaseReference mDatabase;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.fragment_all_posts, container, false);

        mDatabase = FirebaseDatabase.getInstance().getReference();

        mRecycler = rootView.findViewById(R.id.postList);
        mRecycler.setHasFixedSize(true);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LinearLayoutManager mManager = new LinearLayoutManager(getActivity());
        mManager.setReverseLayout(true);
        mManager.setStackFromEnd(true);
        mRecycler.setLayoutManager(mManager);

        Query postsQuery = getQuery(mDatabase);

        FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>()
                .setQuery(postsQuery, Post.class)
                .build();


        mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options) {
            @NonNull
            @Override
            public PostViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
                return new PostViewHolder(inflater.inflate(R.layout.postcard, viewGroup, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull final PostViewHolder viewHolder, final int position,
                                            @NonNull Post model) {

                Post postInstance = this.getItem(position);
                viewHolder.display(postInstance);
                final DatabaseReference postRef = getRef(viewHolder.getAdapterPosition());
                final String postKey = postRef.getKey();

                viewHolder.bindToPost(model, postKey);
            }
        };

        mRecycler.setAdapter(mAdapter);
    }

    protected abstract Query getQuery(DatabaseReference mDatabase);


    @Override
    public void onStart() {
        super.onStart();
        if (mAdapter != null) {
            mAdapter.startListening();
        }
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAdapter != null) {
            mAdapter.stopListening();
        }
    }

这是每个片段实现的类

媒体播放器停止还是继续对我来说并不重要,但是白屏需要消失:-)

预先感谢您的帮助!

0 个答案:

没有答案