Android Question select audios or substitutes that are within the video with exoplayer

nedium

Active Member
Licensed User
Longtime User
Hello everyone and thank you for your help.

Looking a bit about the 'Exoplayer' library, I found the following code where it allows you to see the audio and subtitles that are in the video.

B4X:
Sub Player_TrackChanged
   Dim jo As JavaObject = player1
   Dim TrackGroups As JavaObject = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
   For i = 0 To TrackGroups.GetField("length") - 1
       Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
       For j = 0 To TrackGroup.GetField("length") - 1
           Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
           Dim mime As String = Format.GetField("sampleMimeType")
           Log(mime)
           If mime.StartsWith("audio") Then
               Log("audio track")
           End If
          
       Next
   Next
End Sub


When using this code, I find the audio and subtitle tracks, but I don't know how to select any of the audio tracks (Audios of different languages) or the subtitles.
--
I know that 'exoplayer' has the option of being able to change the audio track or select subtitle and make it visible in the current player.

If anyone can help me or give me an idea of how to work with the 'exoplayer' library using Javaobject.

regards
 

nedium

Active Member
Licensed User
Longtime User
Hello

searching the internet I found an example of what I wanted in case I did not know how to express myself previously.

This would be an example of what I would like to add, that you can select any format or track that is within the video.



regards
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Hello,

If it works for me with exoplayer in b4x I send a copy of the code I use.

B4X:
 If FirstTime Then
        player1.Initialize("player")
        Dim sources As List
        sources.Initialize
        
        sources.Add(player1.CreateUriSource("http://vod.tuxchannel.tv/peliculas/720/late%20night%202019.mkv"))
        
        player1.Prepare(player1.CreateListSource(sources))   
        
    End If
    Activity.LoadLayout("1")
    SimpleExoPlayerView1.Player = player1
    player1.Play
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Hello,

I have been searching the internet regarding the subject, I found some exoplayer code to change the audio or subtitle, I would like to know how I can use this code in b4x

trackSelector.setParameters(
trackSelector
.buildUponParameters()
.setMaxVideoSizeSd()
.setPreferredAudioLanguage("deu"));


I do not know, if it is the right one for what I need but any help is good, greetings
 
Upvote 0

mehdipass

Member
Hi @mehdipass , I'm not doing tests yet but I can't find how to select an audio track or subtitles
hi nedium,
I wrote this code to change resolution manually but I don't know why it doesn't work. (It runs without error but not work)

B4X:
Sub Globals
    Dim VIDEOS_TRACK_GROUP_INDEX As Int
    Dim TrackGroups As JavaObject
End Sub

Sub Player_TrackChanged

    Dim jo As JavaObject = player1
    TrackGroups = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
    For i = 0 To TrackGroups.GetField("length") - 1
        Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
        For j = 0 To TrackGroup.GetField("length") - 1
            Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
            Log(Format) 'ignore
            Dim mime As String = Format.GetField("sampleMimeType")
            If mime.StartsWith("video") Then
                If VIDEOS_TRACK_GROUP_INDEX = 0 Then VIDEOS_TRACK_GROUP_INDEX = i
            End If
        Next
    Next

End Sub

Sub TrackSelection(Selection as int)

    Dim TRACK_TYPE_VIDEO As JavaObject
    TRACK_TYPE_VIDEO = TRACK_TYPE_VIDEO.InitializeStatic("com.google.android.exoplayer2.C").GetField("TRACK_TYPE_VIDEO")

    Dim SelectionOverride As JavaObject
    Dim int_Selection(1) As Int
    int_Selection(0) = Selection
    SelectionOverride.InitializeNewInstance("com.google.android.exoplayer2.trackselection.DefaultTrackSelector.SelectionOverride",Array(VIDEOS_TRACK_GROUP_INDEX,int_Selection,TRACK_TYPE_VIDEO,0))

    Dim DefaultTrackSelector As JavaObject
    DefaultTrackSelector.InitializeNewInstance("com.google.android.exoplayer2.trackselection.DefaultTrackSelector",Null)
    Dim Parameters As JavaObject = DefaultTrackSelector.RunMethodJO("buildUponParameters",Null)
    Parameters = Parameters.RunMethodJO("setSelectionOverride",Array(VIDEOS_TRACK_GROUP_INDEX,TrackGroups,SelectionOverride))
    Dim build As JavaObject
    build.InitializeNewInstance("com.google.android.exoplayer2.trackselection.DefaultTrackSelector",Null)
    build.RunMethodJO("setParameters",Array(Parameters))

End Sub
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Hello @mehdipass

have you verified that the video has several formats?
I think your code could help me change the audio or subtitles but I don't know how to do it, it would be something similar but with audio or subtitles

I think it does not work for you because if it does not exist, the format does not take it
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Hello @mehdipass

Look at this new library that does respond to this previous thread.


 
Upvote 0
Top