How to control play/pause state of MediaBrowserServiceCompat from RecyclerView?

113 Views Asked by At

I have an app that uses MediaBrowserServiceCompat to play audio. I have a RecyclerView that displays a list of media items, and I want to be able to control the play and pause state of each item from within the RecyclerView. For example, I want to be able to play a media item when the user taps on it, and pause it when the user taps again.

I'm not sure how to achieve this, as the MediaBrowserServiceCompat is responsible for handling media playback, and my RecyclerView is just a UI component. Can someone please suggest a way to control the play and pause state of a media item from within a RecyclerView that's backed by a MediaBrowserServiceCompat?

Any help or guidance would be greatly appreciated. Thank you!

1

There are 1 best solutions below

0
On

I want its should only add a new QueueItem if the uri is not the same as the previous click

and if is the same. if it has started already plying it should resume but if it has finish playing and it is the same uri it should rewind but only with a click

so i did it like this


root.binding.playChat.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        root.binding.textView40.setTag(position);
        Gson gson = new GsonBuilder().create();
        ChatMessageViewModel chat = getCurrentList().get(position);
        String attachments = gson.toJson(chat.getAttachments());
        Type collectionType = new TypeToken<Collection<Attachments>>() {}.getType();
        Collection<Attachments> enums = gson.fromJson(attachments, collectionType);
        for (Attachments a : enums) {
        String path = a.getPath();
            try {
                currentPlay = root.binding.playChat;
                seekBar = root.binding.sb;
                textView = root.binding.textView40;
                String preUrl = "";
                String newURL = domain + path + "?apikey=" + encodeValue(apikey) + "&auth=" + auth + "&secret=" + encodeValue(secret);
                String oldUrl = preUrl;
                if (!newURL.equals(url)) {
                    url = newURL;
                Uri myUri = Uri.parse(url);
                voicemailViewModel.addURItoQueueChat(MediaControllerCompat.getMediaController(ChatActivity.this), myUri);
                changePlayback();
                 }else {
                   MediaControllerCompat.TransportControls transportControls =  MediaControllerCompat.getMediaController(ChatActivity.this).getTransportControls();
                    int pbState = MediaControllerCompat.getMediaController(ChatActivity.this).getPlaybackState().getState();
                    if (pbState == PlaybackStateCompat.STATE_PLAYING) {changePlayback();
                    }else if (pbState == PlaybackStateCompat.STATE_PAUSED ) {changePlayback();
                    }else {
                        VoicemailPlayingService.getLiveState().observe(ChatActivity.this, new Observer<Integer>() {
                            @Override
                            public void onChanged(Integer integer) {
                                if (integer == ExoPlayer.STATE_ENDED){
                                    MediaControllerCompat.getMediaController(ChatActivity.this).getTransportControls().seekTo(0);
                                    MediaControllerCompat.getMediaController(ChatActivity.this).getTransportControls().pause();
                                    currentPlay.setImageResource(R.drawable.ic_outline_play_arrow_24);}

                            }
                        });
                    }
                }
            } catch (Exception e) {}
        }
    }

the changePlayback() is like this

void changePlayback() {
    int pbState = MediaControllerCompat.getMediaController(ChatActivity.this).getPlaybackState().getState();
    if (pbState == PlaybackStateCompat.STATE_PLAYING) {
        MediaControllerCompat.getMediaController(this).getTransportControls().pause();
        currentPlay.setImageResource(R.drawable.ic_outline_play_arrow_24);
    } else if (pbState == PlaybackStateCompat.STATE_PAUSED || pbState ==0 ){
        MediaControllerCompat.getMediaController(this).getTransportControls().play();
        currentPlay.setImageResource(R.drawable.ic_baseline_pause_24);}
}