Android Question [SOLVED] ExoPlayer and SSL

MichalK73

Well-Known Member
Licensed User
Longtime User
Hello.
Is it possible to somehow set the exoplayer not to pay attention to invalid SSL certificates of the movie link?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Solution

MichalK73

Well-Known Member
Licensed User
Longtime User
Did you do everything as stated above? Maybe somewhere a small error you have, with me it worked and is ok.
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Are you using the latest versions of sdk and b4a? It is to play a video, what I don't know is when to call the event or if just putting it at the start already works, if you can give me some help to see if I'm doing something wrong
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
Here's my code from one of the AndroidTV apps. Modull B4XPages call exoplayer and play the video from the link.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private SimpleExoPlayerView1 As SimpleExoPlayerView
    Private player1 As SimpleExoPlayer
    Private socket As Socket
    Private toast As BCToast
End Sub

Public Sub Initialize
    player1.Initialize("player")
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("exo")
'    Dim frm As Form = B4XPages.GetNativeParent(Me)
    toast.Initialize(Root)
End Sub

Private Sub CreateTrustAllSSLSocket (EventName As String) As Socket
    socket.InitializeSSLAcceptAll("sock")
    Dim jo As JavaObject = socket
    jo.SetField("socket", CreateTrustAllSSLSocketFactory.RunMethod("createSocket", Null))
    Return socket
End Sub

Sub CreateTrustAllSSLSocketFactory As JavaObject
    Dim tm As CustomTrustManager
    tm.InitializeAcceptAll
    Dim SSLContext As JavaObject
    SSLContext = SSLContext.InitializeStatic("javax.net.ssl.SSLContext").RunMethod("getInstance", Array("TLS"))
    SSLContext.RunMethod("init", Array(Null, tm, Null))
    Dim Factory As JavaObject = SSLContext.RunMethod("getSocketFactory", Null)
    Return Factory
End Sub

Sub B4xPage_Appear
    Main.call=Me
    Dim jo As JavaObject
    jo.InitializeStatic("javax.net.ssl.HttpsURLConnection").RunMethod("setDefaultSSLSocketFactory", Array(CreateTrustAllSSLSocketFactory ))
    Dim sources As List
    sources.Initialize
    #if debug
    Log("exo: "&Main.linkExo)
    #end if
    If Main.linkExo <> "" Then
        If Main.linkExo.Contains(".m3u8") Then
            
            sources.add(player1.CreateHLSSource(Main.linkExo))
        End If
        
        If Main.linkExo.Contains(".mp4") Or Main.linkExo.Contains(".mkv") Or Main.linkExo.Contains(".avi") Then
            sources.Add(player1.CreateUriSource(Main.linkExo))
'            sources.Add(player1.CreateDashSource(Main.linkExo))
        End If
    End If

    player1.Prepare(player1.CreateListSource(sources))
    SimpleExoPlayerView1.Player = player1
    player1.Volume=1
    player1.Play
    toast.pnl.Color = Colors.White
    toast.DurationMs=1000
    toast.DefaultTextColor = Colors.Black
    
End Sub

Sub B4xPage_Disappear
    player1.Pause
End Sub

Sub Player_Ready
'    toast.Show("Start Video")
'    Log("Ready")
End Sub

Sub Player_Error (Message As String)
    player1.Pause
    xui.MsgboxAsync(Message, "Error")
    Log("Error: " & Message)
    B4XPages.ClosePage(Me)
End Sub

Sub Player_Complete
    player1.Pause   
    B4XPages.ClosePage(Me)
'    Log("complete")
End Sub

Private Sub Pressed(key As Int)
    If key = KeyCodes.KEYCODE_DPAD_CENTER Then
        If player1.IsPlaying Then
            player1.Pause
            toast.Show("Pauza")
        Else
            player1.Play
            toast.Show("Play")
        End If
    End If
    
    If key = KeyCodes.KEYCODE_DPAD_RIGHT Then
        player1.Position = player1.Position + 1000*30
        toast.Show(">> 30 sek.")
    End If
    
    If key = KeyCodes.KEYCODE_DPAD_LEFT Then
        player1.Position = player1.Position - 1000*30
        toast.Show("<< 30 sek.")
    End If
    
    If key = KeyCodes.KEYCODE_DPAD_UP Then
        player1.Position = player1.Position + 1000*60*5
        toast.Show(">> 5 min.")
    End If
    
    If key = KeyCodes.KEYCODE_DPAD_DOWN Then
        player1.Position = player1.Position - 1000*60*5
        toast.Show("<< 5 min.")
    End If
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Error: androidx.media3.datasource.HttpDataSource$HttpDataSourceException: javax.net.ssl.SSLPeerUnverifiedException: Hostname NAMEHOST not verified:
or
Error: androidx.media3.datasource.HttpDataSource$HttpDataSourceException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
 
Upvote 0
Top