Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load multiple subtitles before preparing player and switch subtitles on runtime dynamically without preparing the player again? i am using exo player version 2.16.1 #9955

Closed
smkhurramkhan opened this issue Feb 9, 2022 · 9 comments
Assignees
Labels

Comments

@smkhurramkhan
Copy link

smkhurramkhan commented Feb 9, 2022

this is how i am loading subttiles. it's working fine for one but i want to pass list of subtitles before preparing media player and than switch subtitles from the list

 private fun preparePlayer(videoUrl: String) {
        videoWatchedTime = simpleExoplayer.currentPosition
        val uri = Uri.parse(videoUrl)


        val mediaItemBuilder = MediaItem.Builder()
            .setUri(uri)

        val uriSubtitle =
            Uri.parse(subTitle)


    
        val subtitle = subTiltesList?.let {
            listOf(
                MediaItem.Subtitle(
                    Uri.parse(it[i]),
                    MimeTypes.TEXT_VTT,  // The correct MIME type.
                    "en",
                    C.SELECTION_FLAG_DEFAULT
                ) )
        }


    Timber.d("immutable list is ${immutableListOf(subtitle).size}")
    val mediaItem = mediaItemBuilder
            .setSubtitles(subtitle)
            .build()


        val mediaSource =
            DefaultMediaSourceFactory(dataSourceFactory).createMediaSource(mediaItem)


        simpleExoplayer.prepare(mediaSource)
        simpleExoplayer.seekTo(videoWatchedTime)


}
@smkhurramkhan smkhurramkhan changed the title How to load multiple subtitles before preparing player and switch subtitles on runtime dynamically without preparing the player again? How to load multiple subtitles before preparing player and switch subtitles on runtime dynamically without preparing the player again? i am using exo player version 2.16.1 Feb 9, 2022
@ojw28 ojw28 self-assigned this Feb 11, 2022
@ojw28
Copy link
Contributor

ojw28 commented Feb 12, 2022

I think you're creating the source correctly, except that generally you'd expect each subtitle to differ somehow other than just the Uri. For example, are all of the subtitles really in English? If not you should be specifying a different language when you create each MediaItem.Subtitle. Also, normally only one of the subtitles will use C.SELECTION_FLAG_DEFAULT, since this flag is indicating which subtitle you'd like to be selected by default (i.e., you have to pick one!).

During playback, you can change the selected subtitle track using ExoPlayer's track selection APIs. these are documented here.

@smkhurramkhan
Copy link
Author

yes the subtitles are of different language i.e Arabic and English but the thing is i can only parse one uri of subtitle before preparing the player. all i need to know is how to load list of subtitles and than prepare media player than i can change them dynamically without preparing the media player again.

@ojw28
Copy link
Contributor

ojw28 commented Feb 12, 2022

You need all of the subtitle URIs available in advance to build the MediaItem.Subtitle list. If you don't have the list of URIs in advance then this is a duplicate of #1649, which is a feature request (i.e., it's not yet supported).

@smkhurramkhan
Copy link
Author

No i have list of subtitles before preparing the media player. All i want to do is load that list of subtitles before preparing the player and than i want to change the subtitles on button click. i have two button in the app one for english and one for arabic when i click on arabic it should show arabic subtitles and when click on english should show english subtitles. i want to load both subtitles before preparing the player. right now i am loading single subtitle and it is working good. but my question is how to load both subtitles before preparing media player.

@ojw28
Copy link
Contributor

ojw28 commented Feb 12, 2022

I think I've already answered the question.

  1. Pass a list of MediaItem.Subtitle elements to setSubtitles when building the MediaItem, rather than just a single subtitle. I'd expect each of the Subtitle objects to have a different Uri and a different language, and at most one of them should use C.SELECTION_FLAG_DEFAULT.
  2. Use ExoPlayer's track selection APIs to switch between them during playback. These APIs are documented here.

@smkhurramkhan
Copy link
Author

val subtitle = subTiltesList?.let {
listOf(
MediaItem.Subtitle(
Uri.parse(it[i]),
MimeTypes.TEXT_VTT, // The correct MIME type.
"en",
C.SELECTION_FLAG_DEFAULT
) )
}
val mediaItem = mediaItemBuilder
.setSubtitles(subtitle)
.build()

see this i have list but Uri.parse(it[i]) it takes one item not list. also mediaitem.Builder.setSubtitles(subtitles) it takes one item. how i should pass a list?

@smkhurramkhan
Copy link
Author

can you provide me a sample code to load subtitles list that will be really helpful

@ojw28
Copy link
Contributor

ojw28 commented Feb 12, 2022

MediaItem.Builder.setSubtitles takes a list, as per our documentation here:

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/MediaItem.Builder.html#setSubtitles(java.util.List)

and the actual source code here:

https://github.com/google/ExoPlayer/blob/r2.16.0/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java#L369

If you need help on creating a list in Kotlin, I'd suggest you ask on StackOverflow since this is a general programming question unrelated to ExoPlayer.

@ojw28 ojw28 closed this as completed Feb 12, 2022
@smkhurramkhan
Copy link
Author

Thanks that's what i needed

@google google locked and limited conversation to collaborators Apr 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants