Android Question [SOLVED] ExoPlayer - Ensuring Video Playback Until the End Using player.duration inside a loop

jroriz

Active Member
Licensed User
Longtime User
Hello.

My challenge is to make the video play until the end using player.duration. I've tried several approaches, including player_Ready, but I still can't get it to work as expected.

I know it seems simple, but I've made multiple attempts without success and I'm wasting time on something that should be straightforward.

As you can see in the code, I need to display content (images, videos, and texts—which are actually RSS links) continuously and indefinitely.

Don't worry too much about the code itself. It was adapted from a larger project where I'm facing an issue.
Also, the layout is already handled in the final version, so there's no need to worry about that.

Any help would be greatly appreciated!

B4X:
Sub Process_Globals
    Private xui As XUI
    Public stopExibicao As Boolean = False
End Sub

Sub Globals

    Private ImageView1 As ImageView
    Private WebView1 As WebView
    Private Panel1 As Panel
    Private SimpleExoPlayerView1 As SimpleExoPlayerView
    Private xPlayer As SimpleExoPlayer

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("player")

    wait for (ExibirArquivosIndefinidamente) complete (ret As Object)

End Sub

Sub ExibirArquivosIndefinidamente As ResumableSub
    Dim pasta As String = File.DirAssets
    Dim arquivos As List = File.ListFiles(pasta)

    ' Never mind about listaFiltrada, it's different in the real code.
    ' The files are indeed downloaded, but For the sake of example, I placed them internally.
    Dim listaFiltrada As List
    listaFiltrada.Initialize
    For Each nomeArquivo As String In arquivos
        If nomeArquivo.Contains("mp4") _
            Or nomeArquivo.Contains("txt") _
            Or nomeArquivo.Contains("jpg") Then
                listaFiltrada.Add(nomeArquivo)
        End If
    Next
    
    If listaFiltrada.Size = 0 Then
        Log("Nenhum arquivo encontrado.")
        Return Null
    End If
    
    stopExibicao = False ' Reinicia o flag
    Dim i As Int = 0

    Do While stopExibicao = False
        Dim nomeArquivo As String = listaFiltrada.Get(i)
        Dim ext As String = nomeArquivo.ToLowerCase
        
        If ext.EndsWith(".jpg") Or ext.EndsWith(".jpeg") Or ext.EndsWith(".png") Then
            ImageView1.Bitmap = LoadBitmap(pasta, nomeArquivo)
            ImageView1.BringToFront
            Sleep(5000) ' Exibe a imagem por 5 segundos
        Else If ext.EndsWith(".mp4") Then

            xPlayer.Initialize("player")
            SimpleExoPlayerView1.Player = xPlayer

            xPlayer.Prepare(xPlayer.CreateFileSource(pasta, nomeArquivo))

            SimpleExoPlayerView1.BringToFront

            xPlayer.Play
            Sleep(5000) ' ??? player duration...
            

        Else If ext.EndsWith(".txt") Then
            Dim htmlContent As String = File.ReadString(pasta, nomeArquivo)

            ' Divide o conteúdo em linhas
            Dim lines() As String = Regex.Split("\r\n", htmlContent)
            Dim extractedURL As String = ""
            For Each line As String In lines
                If line.StartsWith("URL=") Then
                    extractedURL = line.SubString(4) ' Remove "URL=" e obtém a URL
                    Exit
                End If
            Next

            WebView1.LoadUrl("https://" & extractedURL)
            WebView1.BringToFront
            Sleep(5000) ' Exibe o HTML por 5 segundos
        End If
        
        ' Incrementa o índice; reinicia ao final da lista
        i = i + 1
        If i >= listaFiltrada.Size Then
            i = 0
        End If
    Loop
    
    Return Null
End Sub

Private Sub player_Complete
End Sub

Private Sub player_Ready
    Log(xPlayer.Duration)
End Sub

Private Sub player_Error (Message As String)
    Log(Message)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        stopExibicao = True
    End If
    Return False
End Sub
 

jroriz

Active Member
Licensed User
Longtime User
Why aren't you handling the Complete event?
Thank you, @Erel. Solved... :mad:

Here’s the solution in case someone comes across the same issue:

B4X:
            xPlayer.Initialize("player")
            SimpleExoPlayerView1.Player = xPlayer

            xPlayer.Prepare(xPlayer.CreateFileSource(pasta, nomeArquivo))

            SimpleExoPlayerView1.BringToFront

            xPlayer.Play
           
            Wait For player_Complete
 
Upvote 0
Top