MediaControllerCompat的onPlayFromMediaId()方法的奇怪行为

时间:2017-09-01 09:47:25

标签: android android-service android-mediaplayer mediabrowserservicecompat

我正在使用一个扩展MediaBrowserServiceCompat的服务,并且还MediaSessionCompat.Callback()使用了onPlayFromMediaId()覆盖的方法。

在点击recyclerview项目时具有recyclelerview的片段内部,调用以下代码。从图像中可以看出,mediaId以及songBundle显然有值,并且正在传递给onPlayFromMediaId()方法。

  

enter image description here

奇怪的是,在覆盖playFromMediaId()方法的Service类中,它只接收字符串变量mediaId的值,而songBundle神奇地变为空。 (注意 - 用于放置和获取parcelable的键也是相同的.parcelable songsVO是一个实现Parcelable接口的POJO。)

  

enter image description here

除了这个另外有趣的事情,我注意到,当我返回到已经执行后调用onPlayFromMediaId()的片段并检查每个变量的值,除了mediaId每个其他变量变为空。

  

enter image description here   enter image description here

RecyclerAdapter类

  class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.layout_song_item, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                    songPicked(position);
                songsVO=songsVOArrayList.get(position);
                mediaId=songsVO.getFilePath();
                songBundle=new Bundle();
                songBundle.putParcelable("song",songsVO);
//                    
  MediaControllerCompat.getMediaController(getActivity()).getTransportControls().playFromMediaId(mediaId, songBundle);


MediaControllerCompat.getMediaController(getActivity()).getTransportControls().play();
                currentState = STATE_PLAYING;

 holder.imageView.setImageBitmap(songsVOArrayList.get(position).getAlbumArt());
        holder.title.setText(songsVOArrayList.get(position).getTitle());
        holder.artist.setText(songsVOArrayList.get(position).getArtist());
        holder.duration.setText(songsVOArrayList.get(position).getDuration());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        ImageView imageView;
        TextView title, artist, duration;

        public ViewHolder(View itemView) {
            super(itemView);

            cardView = (CardView) itemView.findViewById(R.id.layout_song_item);
            imageView = (ImageView) itemView.findViewById(R.id.img_albumart);
            title = (TextView) itemView.findViewById(R.id.txt_title);
            artist = (TextView) itemView.findViewById(R.id.txt_artist);
            duration = (TextView) itemView.findViewById(R.id.txt_duration);
        }
    }
}

TLDR- onPlayFromMediaId(String, Bundle)只传递String变量,而Bundle传递为空。

参考 - This tutorial

0 个答案:

没有答案