Android Question Question about ExoPlayer

Filippo

Expert
Licensed User
Longtime User
Hi,

I'm currently experimenting with the “ExoPlayer” library, and I've noticed that the “IsInitialized” function isn't available.
This means I have to handle this functionality differently in my code, for example like this:

B4X:
#Region  Activity Attributes 
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    ' Hier dürfen NUR Variablen ohne visuellen Bezug stehen!
    Public StreamURL As String = ""
    
    ' Der Player-Motor MUSS hierhin, damit der Stream im Prozess stabil bleibt
    Private player1 As SimpleExoPlayer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    ' Player
    Private ExoPlayerView As SimpleExoPlayerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' 1. SICHERHEITS-CHECK: Ist eine URL da?
    Log("VOLLSCREEN PLAYER STARTET. URL: " & StreamURL)
    If StreamURL = "" Then
        Log("Fehler: Keine URL empfangen!")
        Activity.Finish
        Return
    End If

    ' 2. NEU & ABSOLUT ENTSCHEIDEND: Falls der Player genullt wurde,
    ' erstellen wir die Instanz mit "Dim" komplett frisch im RAM neu!
    If player1 = Null Then
        Dim player1 As SimpleExoPlayer
    End If
    
    ' 3. Jetzt kann er gefahrlos initialisiert werden
    player1.Initialize("player")

    ' 4. Erst JETZT das Layout laden und binden
    Activity.LoadLayout("frmExoPlayerView")
    ExoPlayerView.Player = player1

    ' 5. Dem System einen winzigen Moment Zeit geben, die Grafikoberfläche zu registrieren
    Sleep(30)

    ' 6. Jetzt laden und abspielen
    Try
        Dim sources As List
        sources.Initialize
        sources.Add(player1.CreateUriSource(StreamURL))
        
        player1.Prepare(player1.CreateListSource(sources))
        player1.Play
    Catch
        Log("Fehler im Player-Try: " & LastException.Message)
        Activity.Finish
    End Try
End Sub

Sub Activity_Resume
    ' Nur abspielen, wenn das Objekt existiert
    If player1 <> Null Then
        player1.Play
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    ' Schutzprüfung vor dem Pausieren
    If player1 <> Null Then
        player1.Pause
        
        ' Wenn der Nutzer das Video abbricht (Zurück-Taste)
        If UserClosed Then
            Log("Vollbild beendet. Zerstöre Player sauber.")
            player1.Release
            
            ' Wir nullen die Variable, damit der nächste Film frisch ge-dim-t wird
            player1 = Null
        End If
    End If
End Sub

I need to reinitialize this variable because when I play the first video and then pause or stop it, the app crashes as soon as I try to play the next video.

I don't think the “If player1 <> Null Then” is particularly elegant.
Could you please implement the “If player1.IsInitialized Then”?

Thank you very much
 
Back
Top