Android Question Keeping web radio app's sound active during phone sleep mode with ExoPlayer

hub73

Active Member
Licensed User
Longtime User
Hello !
How to keep the sound of my web radio app active which stops after 5 minutes when the phone automatically goes into sleep mode. I am using ExoPlayer and here is the code for my application:

My code:
'#BridgeLogger:True

#Region Project Attributes
    #ApplicationLabel: Radio Les Floralies - RLF2
    #VersionCode: 4
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    '#BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #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 Xui As XUI
    
    Private MonTimer As Timer
    
    Private Lecteur As SimpleExoPlayer
    
    Private OldIntent As Intent
        
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Private Bouton_jouer_stop As Button
    Private WebView_principal As WebView
    
    Private Label_message As Label
    
    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    
    Private CONST VERSION_APPLI As String = "Version 2.0"
    
    Private Const URL_SITE As String = "https://www.lesfloralies.info"
    Private Const MAIL_CONTACT As String = "[email protected]"
    
    Private Const URL1_WEBVIEW As String = "https://www.lesfloralies.info/programme/html/encours_android.html"
    Private Const URL2_WEBVIEW As String =  "https://www.lesfloralies.info/flash/flash_android.php"
    Private Const URL3_WEBVIEW As String =  "https://lesfloralies.info/podcasts/html/podcasts_liste_android.html"
    Private Const URL4_WEBVIEW As String =  "https://lesfloralies.info/index_android.php"
    
    Private Const URL_ECOUTE As String =  "http://lesfloralies.ovh:8000/floralies.mp3"
    
    Private Const PHRASE_ECOUTER_LA_RADIO As String = " Écouter"
    Private Const PHRASE_ARRETER_ECOUTE As String = " Arrêter"
    
    Private Const CONTENU_ENCOURS As Int = 1
    Private Const CONTENU_PODCAST As Int = 2
    Private Const CONTENU_FLASH As Int = 3
    Private Const CONTENU_SITE As Int = 4
    
    Private Contenu_affiche As Int
    
End Sub

Sub ImageView1_Click
    Afficher_site
End Sub

Sub ImageView2_Click
    Afficher_flash
End Sub

Sub ImageView3_Click
    Afficher_podcasts
End Sub

Sub Afficher_site

    Stop
    WebView_principal.LoadUrl (URL4_WEBVIEW)
    Label_message.Text = "Le site de la webradio"
    Contenu_affiche = CONTENU_SITE
    
End Sub


Sub Afficher_en_cours

    WebView_principal.LoadUrl (URL1_WEBVIEW)
    Label_message.Text = "Bonne écoute à vous !"
    Contenu_affiche = CONTENU_ENCOURS
    
End Sub

Sub Afficher_flash

    Stop
    WebView_principal.LoadUrl (URL2_WEBVIEW)
    Label_message.Text = "Le flash du jour"
    Contenu_affiche = CONTENU_FLASH
    
End Sub


Sub Afficher_podcasts

    Stop
    WebView_principal.LoadUrl (URL3_WEBVIEW)
    Label_message.Text = "Les podcasts"
    Contenu_affiche = CONTENU_PODCAST
    
End Sub


Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout_principal")
    
    If FirstTime Then
        MonTimer.Initialize("Timer", 1000)
        Lecteur.Initialize("Lecteur")
        Label_message.Text = "Un moment de flânerie"
    End If
    
    
    MonTimer.Enabled = True
    
    Label_message.Text = "Touchez ci-dessus !"
    
    Label_message.Text = "Bonjour."

    Activity.AddMenuItem ("En cours de diffusion","Menu_encours")
    Activity.AddMenuItem ("Le Flash du jour","Menu_flash")
    Activity.AddMenuItem ("Les Podcasts","Menu_podcasts")
    Activity.AddMenuItem ("Nous écrire","Menu_ecrire")
    Activity.AddMenuItem ("À propos","Menu_a_propos")
    Activity.AddMenuItem ("Quitter","Menu_quitter")

    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "radio1.png")
    ImageView2.Bitmap = LoadBitmap(File.DirAssets, "radio2.png")
    ImageView3.Bitmap = LoadBitmap(File.DirAssets, "radio3.png")
    
    Afficher_en_cours
    
    Lecteur.Prepare(Lecteur.CreateUriSource (URL_ECOUTE))
        
End Sub


Sub Menu_encours_Click

    Afficher_en_cours

End Sub

Sub Menu_flash_Click
    
    Afficher_flash

End Sub

Sub Menu_podcasts_Click

    Afficher_podcasts

End Sub

Sub Menu_quitter_Click
    
    Quitter
    
End Sub

Sub Menu_ecrire_Click

    Xui.MsgboxAsync("Radio les floralies - RLF2" & Chr(13) & Chr(10) & "Pour nous écrire envoyez un mail à : " & Chr(13) & Chr(13) & MAIL_CONTACT & Chr(13) & Chr(13) & Chr(10) & "Merci à vous.", "Nous écrire")

End Sub


Sub Menu_a_propos_Click

    Xui.MsgboxAsync("Radio les floralies - RLF2" & Chr(13) & Chr(10) & "L'application Android" & Chr(13) & Chr(10) & URL_SITE  & Chr(13) & Chr(10) & MAIL_CONTACT & Chr(13) & Chr(10) & VERSION_APPLI , "À Propos")

End Sub

Sub Timer_Tick
    
    Select Contenu_affiche
    
        Case CONTENU_ENCOURS
            Afficher_en_cours ' rafraichit la page
        Case CONTENU_PODCAST
            'Afficher_podcasts ' affiche la page des podcasts
        Case CONTENU_FLASH
            ' ne fait rien pour ne pas rafraichir la page ! Technologie Ajax
        Case CONTENU_SITE
            ' ne fait rien pour ne pas rafraichir la page !
    End Select
        
End Sub

Sub Activity_Resume
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        If in.HasExtra("Notification_Tag") Then
            Log("Activity started from notification. Tag: " & in.GetExtra("Notification_Tag"))
        End If
    End If
End Sub

Sub Lecteur_Ready
    
    ' ne fait rien et surtout ne joue pas !
    Jouer
    
End Sub

Sub Lecteur_Error (Message As String)
    
    Xui.MsgboxAsync("Problème, veuillez relancer l'écoute","Oups! '" + Message + "' !")
    'Log("Error: " & ErrorCode & ", " & ExtraData)
    
End Sub

Sub Bouton_jouer_stop_Click
    
    Afficher_en_cours
    If Lecteur.IsPlaying = False Then
        Jouer
    Else
        Stop
    End If
        
End Sub

Public Sub Jouer
    
    If Lecteur.IsPlaying = False Then
        Lecteur.Play
        Lecteur.Volume=100
        Bouton_jouer_stop.Text = Chr(0xF04D) & PHRASE_ARRETER_ECOUTE
        Label_message.Text = "Bonne écoute !"
    End If
        
End Sub

Public Sub Stop
    
    If Lecteur.IsPlaying = True Then
        Lecteur.Pause
        Bouton_jouer_stop.Text = Chr(0xF04B) & PHRASE_ECOUTER_LA_RADIO
        Label_message.Text = ""
    End If
        
End Sub

Public Sub Quitter
    
    Bouton_jouer_stop.Text = Chr(0xF04B) & PHRASE_ECOUTER_LA_RADIO
    Label_message.Text = ""
    Lecteur.Release
    Activity.Finish
    ExitApplication
    
End Sub

Sub Demander_quitter
    
    Msgbox2Async ("Voulez-vous quitter l'application ?","Radio Les Floralies","Oui","","Non",Null, True)
    Wait for MsgBox_result (Result As Int)
    If Result = DialogResponse.POSITIVE Then
        Quitter
    End If
End Sub

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

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Many thanks for your help.
 

hub73

Active Member
Licensed User
Longtime User
Many thanks Erel

Main:
#Region  Project Attributes
    #ApplicationLabel: Radio Les Floralies - RLF2
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

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

'#BridgeLogger: True

Sub Process_Globals
    Public ActionBarHomeClicked As Boolean
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

'Template version: B4A-1.01
#Region Delegates

Sub Activity_ActionBarHomeClick
    ActionBarHomeClicked = True
    B4XPages.Delegate.Activity_ActionBarHomeClick
    ActionBarHomeClicked = False
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
    B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Sub Create_Menu (Menu As Object)
    B4XPages.Delegate.Create_Menu(Menu)
End Sub

#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;
    
}
#End If
#End Region

'Program code should go into B4XMainPage and other pages.

and

B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    
    Private Root As B4XView
    Private xui As XUI
    
    Private MonTimer As Timer
    
    Private Lecteur As SimpleExoPlayer
    
    Private Bouton_jouer_stop As Button
    Private WebView_principal As WebView
    
    Private Label_message As Label
    
    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    
    Private CONST VERSION_APPLI As String = "Version 2.0"
    
    Private Const URL_SITE As String = "https://www.lesfloralies.info"
    Private Const MAIL_CONTACT As String = "[email protected]"
    
    Private Const URL1_WEBVIEW As String = "https://www.lesfloralies.info/programme/html/encours_android.html"
    Private Const URL2_WEBVIEW As String =  "https://www.lesfloralies.info/flash/flash_android.php"
    Private Const URL3_WEBVIEW As String =  "https://lesfloralies.info/podcasts/html/podcasts_liste_android.html"
    Private Const URL4_WEBVIEW As String =  "https://lesfloralies.info/index_android.php"
    
    Private Const URL_ECOUTE As String =  "http://lesfloralies.ovh:8000/floralies.mp3"
    
    Private Const PHRASE_ECOUTER_LA_RADIO As String = " Écouter"
    Private Const PHRASE_ARRETER_ECOUTE As String = " Arrêter"
    
    Private Const CONTENU_ENCOURS As Int = 1
    Private Const CONTENU_PODCAST As Int = 2
    Private Const CONTENU_FLASH As Int = 3
    Private Const CONTENU_SITE As Int = 4
    
    Private Contenu_affiche As Int
    
    Private pws As PhoneWakeState
    
    Private s_Menu() As String
    
    
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

    MonTimer.Initialize("Timer", 1000)
    Lecteur.Initialize("Lecteur")
    
End Sub


'This event will be called once, before the page becomes visible.

Private Sub B4XPage_Created (Root1 As B4XView)
    
    Root = Root1
    Root.LoadLayout("Layout_principal")
    
    B4XPages.SetTitle(Me, "Radio Les Floralies - RLF2")
    
    Label_message.Text = "Un moment de flânerie"
    
    MonTimer.Enabled = True
    Label_message.Text = "Touchez ci-dessus !"
    Label_message.Text = "Bonjour."

    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "radio1.png")
    ImageView2.Bitmap = LoadBitmap(File.DirAssets, "radio2.png")
    ImageView3.Bitmap = LoadBitmap(File.DirAssets, "radio3.png")
    
    s_Menu = Array As String("En cours de diffusion", "Le Flash du jour",  "Les Podcasts", "Nous écrire", "À propos", "Quitter")
    
    For Each sName As String In s_Menu
        B4XPages.AddMenuItem(Me, sName)
    Next
    
    Afficher_en_cours
    
    Lecteur.Prepare(Lecteur.CreateUriSource (URL_ECOUTE))
    
    pws.KeepAlive(True)
    
End Sub


Sub GetIndexInArray(arr() As String, txt As String) As Int
    Dim iIndex As Int = -1, iCount As Int = 0
    For Each sTxt As String In arr
        If sTxt = txt Then iIndex = iCount
        iCount = iCount + 1
    Next
    Return iIndex
End Sub

Sub B4XPage_MenuClick (Tag As String)
    
    If Tag = "menu" Then 'für iOS Hauptmenü, quasi Sub pg_BarButtonClick (Tag As String)

    Else
        Dim iIndex As Int = GetIndexInArray(s_Menu, Tag)
        Select iIndex
            Case 0 ' diffusion
                Afficher_en_cours               
            Case 1 ' flash
                Afficher_flash
            Case 2 ' podcast
                Afficher_podcasts
            Case 3 ' ecrire
                Afficher_ecrire
            Case 4 ' a propos
                Afficher_a_propos
            Case 5 ' quitter
                Quitter
        End Select
    End If
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.


Sub ImageView1_Click
    Afficher_site
End Sub

Sub ImageView2_Click
    Afficher_flash
End Sub

Sub ImageView3_Click
    Afficher_podcasts
End Sub

Sub Afficher_site

    Stop
    WebView_principal.LoadUrl (URL4_WEBVIEW)
    Label_message.Text = "Le site de la webradio"
    Contenu_affiche = CONTENU_SITE
    
End Sub


Sub Afficher_en_cours

    WebView_principal.LoadUrl (URL1_WEBVIEW)
    Label_message.Text = "Bonne écoute à vous !"
    Contenu_affiche = CONTENU_ENCOURS
    
End Sub

Sub Afficher_flash

    Stop
    WebView_principal.LoadUrl (URL2_WEBVIEW)
    Label_message.Text = "Le flash du jour"
    Contenu_affiche = CONTENU_FLASH
    
End Sub


Sub Afficher_podcasts

    Stop
    WebView_principal.LoadUrl (URL3_WEBVIEW)
    Label_message.Text = "Les podcasts"
    Contenu_affiche = CONTENU_PODCAST
    
End Sub



Sub Afficher_ecrire

    xui.MsgboxAsync("Radio les floralies - RLF2" & Chr(13) & Chr(10) & "Pour nous écrire envoyez un mail à : " & Chr(13) & Chr(13) & MAIL_CONTACT & Chr(13) & Chr(13) & Chr(10) & "Merci à vous.", "Nous écrire")
End Sub


Sub Afficher_a_propos

    xui.MsgboxAsync("Radio les floralies - RLF2" & Chr(13) & Chr(10) & "L'application Android" & Chr(13) & Chr(10) & URL_SITE  & Chr(13) & Chr(10) & MAIL_CONTACT & Chr(13) & Chr(10) & VERSION_APPLI , "À Propos")

End Sub

Sub Timer_Tick
    
    Select Contenu_affiche
    
        Case CONTENU_ENCOURS
            Afficher_en_cours ' rafraichit la page
        Case CONTENU_PODCAST
            'Afficher_podcasts ' affiche la page des podcasts
        Case CONTENU_FLASH
            ' ne fait rien pour ne pas rafraichir la page ! Technologie Ajax
        Case CONTENU_SITE
            ' ne fait rien pour ne pas rafraichir la page !
    End Select
        
End Sub

Sub Lecteur_Ready
    
    ' ne fait rien et surtout ne joue pas !
    Jouer
    'xui.MsgboxAsync("Tout va bien" , "coucou")
    
End Sub

Sub Lecteur_Error (Message As String)
    
    xui.MsgboxAsync("Problème, veuillez relancer l'écoute","Oups! '" & Message & "' !")
    Log("Erreur")
    
End Sub

Sub Bouton_jouer_stop_Click
    
    Afficher_en_cours
    If Lecteur.IsPlaying = False Then
        Jouer
    Else
        Stop
    End If
        
End Sub

Public Sub Jouer
    
    If Lecteur.IsPlaying = False Then
        Lecteur.Play
        Lecteur.Volume=100
        Bouton_jouer_stop.Text = Chr(0xF04D) & PHRASE_ARRETER_ECOUTE
        Label_message.Text = "Bonne écoute !"
    End If
        
End Sub

Public Sub Stop
    
    If Lecteur.IsPlaying = True Then
        Lecteur.Pause
        Bouton_jouer_stop.Text = Chr(0xF04B) & PHRASE_ECOUTER_LA_RADIO
        Label_message.Text = ""
    End If
        
End Sub

Public Sub Quitter
    
    Bouton_jouer_stop.Text = Chr(0xF04B) & PHRASE_ECOUTER_LA_RADIO
    Label_message.Text = ""
    Lecteur.Release
    
    ExitApplication
    
End Sub

Sub Demander_quitter
    
    Msgbox2Async ("Voulez-vous quitter l'application ?","Radio Les Floralies","Oui","","Non",Null, True)
    Wait for MsgBox_result (Result As Int)
    If Result = DialogResponse.POSITIVE Then
        Quitter
    End If
End Sub
 
Upvote 0
Top