Android Chromecast sender sdk - How to switch audio tracks from app

Viewed 49

I'm able to successfully cast and play video with chromecast, however the video I'm playing has multi audio and I would like to switch between the audio tracks in real time. I tried below methods but failed with it. Please let me know what needs to be done in order to able to switch audio tracks with chromecast.

val audioTrack = MediaTrack.Builder(1, MediaTrack.TYPE_AUDIO)
                .setName(audioFormat.label)
                .setContentId(audioFormat.format.id)
                .setLanguage("english")
                .build()
            audioFormats.add(audioTrack)

val mediaInfo: MediaInfo = MediaInfo.Builder(videoUrl)
            .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
            .setContentType(MimeTypes.VIDEO_UNKNOWN)
            .setCustomData(customData)
            .setMediaTracks(audioFormats)
            .setMetadata(movieMetadata).build()

return MediaQueueItem.Builder(mediaInfo).build()

override fun switchAudioTrack(languageId: String, langId: Long) {
       
        getRemoteMediaClient()?.setActiveMediaTracks(longArrayOf(langId))
            ?.setResultCallback { mediaChannelResult: RemoteMediaClient.MediaChannelResult ->
                if (!mediaChannelResult.status.isSuccess) {
                    Log.e(
                        this.javaClass.simpleName, "Failed with status code:" +
                                mediaChannelResult.status.statusCode
                    )
                }
            }
    }

Any help is greatly appreciated. Thanks!

0 Answers
Related