Exoplayer: CastPlayer send correct selected audio track to receiver.

Created on 4 Nov 2020  路  5Comments  路  Source: google/ExoPlayer

I have media with multiple audio tracks in the manifest, but when I create the MediaItem, I can't see a way to specify the initial audio track. Besides, I would like to be able to change the audio track in the CastPlayer during the playback, in case the user uses the sender to change the audio.

Exoplayer version: 2.12.1

// If I don't set the metadata, the title doesn't appear in the castMiniController, whereas it did appear in exoplayer 2.11.x
MediaMetadata metadata = new MediaMetadata.Builder()
                .setTitle(currentShow.getFormattedTitle()) 
                .build();

MediaItem mediaItem = new MediaItem.Builder()
                .setUri(uri)
                .setDrmLicenseRequestHeaders(hm)
                .setDrmLicenseUri(drmLicenseUrl)
                .setDrmUuid(C.WIDEVINE_UUID)
                .setMediaId(currentShow.getFormattedTitle())
                .setMediaMetadata(metadata)
                .setMimeType(mimeType)
                .build();

I can't obtain the trackSelector in the CastPlayer's implementation, as it basically returns null:
imagen

On the other hand, when we change audio tracks in settings for the local player, we do the following:

    public void onAudioSelected(AudioCheckableItem audioItem) {
        MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
        if (mappedTrackInfo == null) {
            return;
        }

        int rendererIndex = -1;
        for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
            TrackGroupArray trackGroups = mappedTrackInfo.getTrackGroups(i);
            if (trackGroups.length != 0) {
                switch (player.getRendererType(i)) {
                    case C.TRACK_TYPE_AUDIO:
                        rendererIndex = i;
                        break;
                    case C.TRACK_TYPE_VIDEO:
                    case C.TRACK_TYPE_TEXT:
                    default:
                        break;
                }
            }
        }

        if (rendererIndex == -1)
            return;

        TrackGroupArray trackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
        DefaultTrackSelector.ParametersBuilder parametersBuilder = trackSelector.buildUponParameters();
        parametersBuilder.setSelectionOverride(rendererIndex, trackGroups, new DefaultTrackSelector.SelectionOverride(audioItem.getGroupIndex(), audioItem.getTrackIndex()));
        trackSelector.setParameters(parametersBuilder);
    }

I can't find any documentation about this and the only question related (but with subtitles) is at SO and doesn't have any answers: https://stackoverflow.com/questions/61396776/change-subtitle-using-castplayer

question

Most helpful comment

I don't think you need to fork the library. You need to instantiate the CastPlayer by passing your custom MediaItemConverter implementation. I think looking into the DefaultMediaItemConverter is a good idea. Your implementation can delegate to DefaultMediaItemConverter so you only need to implement toMediaQueueItem to add the thumbnail.

All 5 comments

@marcbaechinger @AquilesCanta Please could you take a look?

@marcbaechinger @AquilesCanta Hi, can you take a look at this? I still haven't got a clue about this.

Besides, I can't find the option to add a thumbnail to the MediaItem as in the gms.cast.MediaMetadata class. Is the feature not supported in cast-extension?

Track selection is probably not straightaway to achieve I'm afraid. Tracks of a piece of media is only known after the media is prepared. When looking into CastPlayer it seems to me that once the media is prepared in the cast device, it's structur is sent back to CastPlayer where it is converted into what is returned by getCurrentTrackSelections and getCurrentTrackGroups. So as a first step, can you check what is returned by these two methods after the track is prepared on the cast device? Even if this looks good, we have to find a way how to then send the track selection choice of the user to the cast device. I don't think this is supported right now, because as you mentioned, the track selector of the cast player is not implemented. It would need to send us some command to the cast player like described here.

I can't find the option to add a thumbnail to the MediaItem as in the gms.cast.MediaMetadata

The Mediaitem of ExoPlayer does not have a field to store a thumbnail. You can create a custom MediaItemConverter and pass it to the constructor of CastPlayer. Then include all custom data that you can not store in the ExoPlayer MediaItem to an object that you use as mediaItem.playbackProperties.tag. Then in you custom MediaItemConverter you can create the cast MediaQueueItem with the properties needed for the thumbnail.

Hi @marcbaechinger thanks for the reply. Apologies, I couldn't answer earlier. I will study what you said.

As for the second part, I guess I have to do that custom MediaItemConverter anyway to support the extended controls view in the sender. If there is any guide on how to implement a custom MediaItemConverter, let me know. For the moment I will fork the library and copy the behaviour of DefaultMediaItemConverter. Thanks.

Perhaps it was too soon to deprecate the method addItem that used a MediaQueueItem in CastPlayer.

I don't think you need to fork the library. You need to instantiate the CastPlayer by passing your custom MediaItemConverter implementation. I think looking into the DefaultMediaItemConverter is a good idea. Your implementation can delegate to DefaultMediaItemConverter so you only need to implement toMediaQueueItem to add the thumbnail.

Was this page helpful?
0 / 5 - 0 ratings