Media Player stops when screen goes blank

vb1992

Well-Known Member
Licensed User
Longtime User
How do you stop the media player app from shutting off
when the android's phone screen time out triggers and goes blank.

I wanted the song (mp3 via media player) to keep on playing.

Is this possible?
 

vb1992

Well-Known Member
Licensed User
Longtime User
I get this message when compiling:


Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'awake' is used before it was assigned any value.
Occurred on line: 15
Awake.KeepAlive(True)


CODE SNIPLET:


Sub Activity_Create(FirstTime As Boolean)

Awake.KeepAlive(True)
​


I then

included the "PHONE LIBRARY", not sure if i had to do that?
but that didn't help.


Also, this seems like it would not dim the display or shit off the display
correct? unless they pressed the sleep button, or closed the app

What if you wanted the person to go jogging with a song
playing and you didn't want to waste his/her battery
having the display on, but wanted the song to keep playing
when the display (sleep) timed out and turned off the app.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You need to do something like this:

B4X:
Sub Globals

      Dim Ph As PhoneWakeState

...

End Sub




Sub Activity_Create(FirstTime As Boolean)

      Ph.KeepAlive(True)

...

Yes, you have to include the Phone Library.
 
Last edited:
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Thanks, that works, and keeps the phone and display and music on...

I'm just concerned about the battery draining
since the display is always on.

Wish the music player would play when the display times out...
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
I don't know if this is relevant to you but I have an mp3 player module that runs as a service and keeps playing and streaming music in the background, even when the screen goes off. I belive this is the point of services, they run in the background continuously. Don't know if that helps. I am assuming that you have an app with the audio library?
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Well I just added a new service module and in process globals:

Dim mymp3 As MediaPlayerStream
then load a URL:
mymp3.Load(http://pathtosong.mp3)
and in the mp_streamready event I play the song as soon as the url is ready:
mymp3.play
Then when the mp_complete event is raised when the song finishes I fetch a new song from the playlist (the playlists comes from mysql queries).

Note that this is all based on the mediaplayerstream because I am fetching songs directly from my machine at home so I don't have to carry songs about on my phone. If you are looking to play songs from your local storage card you would probably want to use the mediaplayer, not the mediaplayerstream. I haven't used the mediaplayer but I think it looks more versatile than the streamer.
I hope that helps!
 
Upvote 0
Top