Weird behaviour of MediaControllerCompat's onPlayFromMediaId() method

607 Views Asked by At

I am working with a service which extends MediaBrowserServiceCompat, and also has MediaSessionCompat.Callback() with it's overridden methods inluding onPlayFromMediaId().

Inside from a fragment which has recyclerview on click of the recyclerview item, following code is called. As from the image, it is clear that mediaId as well as songBundle have values and are being passed to the onPlayFromMediaId() method.

enter image description here

The weird thing is, inside the Service class where the playFromMediaId() method is overriden, it only receives value for the string variable mediaId while the songBundle magically becomes empty. (Note- key used for putting and getting the parcelable is also same. The parcelable songsVO is a POJO which implements Parcelable interface.)

enter image description here

Apart from this another interesting thing which I noticed is, when I return to the fragment which called the onPlayFromMediaId() later after it has been executed and check values of every variable, except mediaId every other variable becomes null.

enter image description here enter image description here

RecyclerAdapter class

  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) passes only String variable while Bundle is passed empty.

Reference- This tutorial

0

There are 0 best solutions below