Android Question AudioStream with MediaPlayerStream

Martin Domian

Member
Licensed User
Longtime User
Hi!

I want to play the Audiostream "https://s10.streamingcloud.online:13192/mobil" and like to use MediaPlayerStream.
It works only once. Now I got the COMPLETE Event after start and nothing runs.

Other Option is to use EXOPlayer, but I want to avoid then effort.
Thanks for any ideas ?

Martin

Here my Testcode:

#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private myStream As MediaPlayerStream
Private isPlayingStream As Boolean = False
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
Public streamURL As String = "https://s10.streamingcloud.online:13192/mobil"
Dim myButton As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime = True Then
myButton.Initialize ("myButton")
Activity.AddView(myButton, 0, 0, 100%x, 40dip)
myButton.Visible = True
End If
End Sub

Sub myButton_click
myStream.Initialize("myStream")
If isPlayingStream = False Then
PlayStream
Else
Log("Stream läuft bereits.")
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub myStream_Complete
Log("Stream wurde unterbrochen.")
'myStream.Stop
' StopStream
' Sleep (500) ' Optional: Stream neu starten oder stoppen
' PlayStream
End Sub

Sub myStream_Error (ErrorCode As Int, Extra As Int) As Boolean
Log($"Fehler im MediaPlayerStream: ErrorCode=${ErrorCode}, Extra=${Extra}"$)
isPlayingStream = False
DoEvents
PlayStream
Return True
End Sub

Sub PlayStream
Try
myStream.Load(streamURL)
myStream.Play
isPlayingStream = True
Log("Stream gestartet: " & streamURL)
Catch
Log("Fehler beim Starten des Streams")
isPlayingStream = False
End Try
End Sub

Sub StopStream
If myStream.IsPlaying Then
myStream.Stop
End If
isPlayingStream = False
Log("Stream gestoppt.")
End Sub
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
if you won't use exoplayer, you'll have to use an "invisible" webview. like this:
B4X:
    Dim stream As String =  "https://s10.streamingcloud.online:13192/mobil"
   Dim webview As WebView
    webview.Initialize("wv")
    Activity.AddView(webview,0%x,0%y,1%x,1%y)
    webview.LoadUrl(stream)

works fine. give it a couple seconds to get going. to stop it, add webview.LoadUrl("about:") to activity_pause. otherwise, it keeps playing in the background.
 
Upvote 0

Martin Domian

Member
Licensed User
Longtime User
Ok, that works.
Thanks for the idea.

The app plays videos, among other things, at which point the background has to be gently faded out, the video played and then the background pulled back up.

That doesn't work now, as soon as another video starts, the stream stops and doesn't come back.

I do it with the media player and MP3 files, it works perfectly, that's why I wanted to use the stream

Here you see the class:
MP3 / Stream Player:
    #Region  Service Attributes

        #StartAtBoot: False

      

    #End Region


    Sub Process_Globals

        Private myPlayer As MediaPlayer

        Private myStream As MediaPlayerStream, isPlayingStream As Boolean = False

        Private myList As List

        Private currentIndex As Int = 0

        Private mp3Folder As String

        Private streamURL As String = "https://s10.streamingcloud.online:13192/mobil"

        Private timerRestart As Timer   

        Private useStream As Boolean = False   

    End Sub


    Sub Service_Create     

      If useStream = True Then

          myStream.Initialize("myStream")

          timerRestart.Initialize("timerRestart",5000)

      Else         

          mp3Folder = File.DirRootExternal & "/Download/Music"

          myList.Initialize ' Liste initialisieren

          Dim files As List

          files = File.ListFiles(mp3Folder) ' Dateien im Ordner abrufen

          For Each F As String In files ' Schleife durch die Dateien

              If F.ToLowerCase.EndsWith(".mp3") Then

                  myList.Add(F) ' Datei zur Liste hinzufügen

              End If

          Next

        myPlayer.Initialize2("myPlayer")

        End If   

      'Service.StopAutomaticForeground   'StartForeground(1, CreateNotification("MP3-Player läuft..."))  ' Halte den Service aktiv

        'Service.StartForeground(1,Service.AutomaticForegroundNotification)

    End Sub


    Sub Service_Start (StartingIntent As Intent)   

      

        If useStream = True Then

            timerRestart.Enabled = False

            If isPlayingStream = False Then

                PlayStream

          Else

              Log("Stream läuft bereits.")           

            End If   

        Else   

          If myList.Size > 0 And myPlayer.IsPlaying = False Then

            PlayCurrentTrack

          End If

        End If   

      

    End Sub


    Sub Service_Destroy

        If useStream = True Then

            StopStream

        Else

          myPlayer.Release

        End If

          

    End Sub


    Sub timerRestart_Tick()

        timerRestart.Enabled = False

        PlayStream

    End Sub


    Sub PausePlaying

        If useStream Then

            FadeOut

        Else

          If myPlayer.IsPlaying = True Then

              FadeOut       

          End If

        End If   

    End Sub


    Sub ResumePlaying

        If useStream = True Then

            If isPlayingStream = False Then

                PlayStream

            End If

        Else

            If myPlayer.IsPlaying = False Then

                FadeIn

            End If

        End If   

    End Sub


    Sub StopPlaying   

        If useStream = True Then

            StopStream

        Else   

            myPlayer.Stop

        End If   

    End Sub


    Sub PlayCurrentTrack

        If currentIndex >=myList.Size Then currentIndex = 0  ' Loop zurück

        myPlayer.Load(mp3Folder, myList.Get(currentIndex))

        myPlayer.Play

    End Sub


    Sub myPlayer_Complete

        currentIndex = (currentIndex + 1) Mod myList.Size

        PlayCurrentTrack

    End Sub


    Sub myStream_Complete

        Log("Stream wurde unterbrochen.")

        myStream.Stop

        timerRestart.Enabled = True

    '    StopStream

    '    Sleep (500)     ' Optional: Stream neu starten oder stoppen

    '    PlayStream

    End Sub


    Sub myStream_Error (ErrorCode As Int, Extra As Int) As Boolean

        Log($"Fehler im MediaPlayerStream: ErrorCode=${ErrorCode}, Extra=${Extra}"$)

        isPlayingStream = False

        DoEvents

        PlayStream

        Return True ' Fehler wurde behandelt

      

    End Sub


    Sub FadeOut

        Dim volume As Float = 1.0  ' Startlautstärke

        Dim steps As Int = 25      ' Anzahl der Schritte für den Fade

        Dim delay As Int = 50      ' Zeit zwischen den Schritten in Millisekunden

      

        For i = 1 To steps

            volume = volume - (1.0 / steps)  ' Lautstärke schrittweise reduzieren

            If volume < 0 Then volume = 0    ' Sicherheit, falls wir unter Null gehen

            myPlayer.SetVolume (volume,volume)

            Sleep(delay)

        Next

        myPlayer.Pause

      

    End Sub


    Sub FadeIn

        Dim volume As Float = 0.0  ' Startlautstärke

        Dim steps As Int = 25      ' Anzahl der Schritte für den Fade

        Dim delay As Int = 50      ' Zeit zwischen den Schritten in Millisekunden

      

        myPlayer.play

        For i = 1 To steps

            volume = volume + (1.0 / steps)  ' Lautstärke schrittweise erhöhen

            If volume > 1 Then volume = 1    ' Sicherheit, falls wir über Eins gehen

            myPlayer.SetVolume (volume,volume)

            Sleep(delay)

        Next

    End Sub


    Sub PlayStream

    

        Try

        myStream.Load(streamURL) ' URL laden

        myStream.Play ' Wiedergabe starten

        isPlayingStream = True

        Log("Stream gestartet: " & streamURL)

      Catch

        Log("Fehler beim Starten des Streams")

        isPlayingStream = False

      End Try

      

    End Sub


    Sub StopStream

      

        If myStream.IsPlaying Then

            myStream.Stop

        End If

        isPlayingStream = False

        Log("Stream gestoppt.")

      

    End Sub

 
Last edited:
Upvote 0
Top