我正在使用扩展的服务MediaBrowserServiceCompat
,并且还具有MediaSessionCompat.Callback()
它的覆盖方法,包括onPlayFromMediaId()
.
在单击 recyclerview 项目时具有 recyclerview 的片段内部,调用以下代码。从图像中可以清楚地看出mediaId
,还有songBundle
值并被传递给onPlayFromMediaId()
方法。
奇怪的是,在该playFromMediaId()
方法被覆盖的 Service 类中,它只接收字符串变量的值,mediaId
而songBundle
神奇地变为空。(注意,用于放置和获取parcelable的key也是一样的。parcelablesongsVO
是一个实现Parcelable
接口的POJO。)
除此之外,我注意到的另一件有趣的事情是,当我返回到onPlayFromMediaId()
执行完后调用的片段并检查每个变量的值时,除了mediaId
其他所有变量都变为空。
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)
仅传递字符串变量,而 Bundle 传递为空。
参考 -本教程