Android Question Play it again, Sam

boten

Active Member
Licensed User
Longtime User
My App plays a sound file with MediaPlayer (and also shows "things" on the screen)

A Button changes the PhoneWakeState between KeepAlive(True) and ReleaseKeepAlive.

When the phone is in "KeepAlive" mode, the screen doesn't turn off and sound keeps playing

When the phone is in "ReleaseKeepAlive" mode and the screen dims and then turned off, the sound stops.

for the record:
Activity_Resume always "play" the sound.
Sound stop & released on Activity_Pause (UserClosed=True)

Is it possible to continue playing the sound even when screen of off.
 

roumei

Active Member
Licensed User
On my phone, the mediaplayer continues playing if the display is turned off and mp is not stopped or paused in Activity_Pause.
 
Upvote 0

roumei

Active Member
Licensed User
You need to have a foreground service.
That's interesting because I might have to update my app if a foreground service is needed. I tested the following code on two devices (Samsung Galaxy A3 with Android 7 and Motorola moto g8 with Android 10) and the sound keeps playing when the device goes to sleep, the display is switched off or the app is send to background.
I have to test when the app is killed by Android. It's a basic B4XPages project with only the mp lines added:

[Edit] I tested both a B4XPages and an old Activity example and the music keeps playing for more than 30 minutes. There might be devices with a more aggressive power management, though.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private mp As MediaPlayer
End Sub

Public Sub Initialize

End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    mp.Initialize2("mp")
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Sub Button1_Click
    mp.Load(File.DirAssets, "bensound-jazzyfrenchy.mp3")
    mp.Looping = True
    mp.Play
End Sub
 
Last edited:
Upvote 0
Top