MediaPlayer in service pauses after a while - android

I have implemented a media player in a service and it works fine when i have a playlist is on as long as the screen is on. However, when the screen is locked, playback stops after a couple of songs and i have to press play again in order to continue.
Is there anything that causes the service to stop or the media player to pause playback when the screen is off, even after I add a partial wake lock?

Related

Play and Pause external audio from my app?

I am currently making an application that uses the proximity sensor to play / pause and skip music. If a hand is near for under a second, it plays and pauses, if it's over a second it will skip the song. I have the "long" and "short" readouts measured properly, but I do not know how to play or pause music playing in another app from my app. (Ex: Say a person is listening to YouTube Music, I need my app to be able to toggle the play state).
What lines of code would let me play or pause or skip songs?
I am not sure about all apps. But this code works for some music app.
//Pause music
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//Play music
am.abandonAudioFocus(null);

MediaPlayer not playing sound after the screen locked

I'm using the AlarmManager for playing a sound (with MediaPlayer) at a given time.
Unfortunately the play() method doesn't seem to work when the phone is locked.
I'm developing on Marshmallow, so I already solved the Doze issues by using setAlarmClock() and using a CPU wake lock (the Partial wake lock). The strange thing is that the activity launches over the lockscreen (what it should do) when the alarm fires, but the play() method of the MediaPlayer doesn't play.
When I rotate the screen, the music starts playing.
How can I solve this, so the music start playing when the activity is launched, altough the phone is locked?
Because of the Dose Mode. When you rotate a screen Dose mode is disabled. Try to add your app in white list

Receive sound button click when the phone is locked

I need to receive long sound button press event even if my phone is locked.
I tried using SettingsContentObserver and AudioManager but it only works when the music is playing.
Source: https://stackoverflow.com/a/15292255/4757621
Currently I am creating wakelock in OnCreate method to keep CPU running when phone is locked but still, no success.
Any ideas?

Android audio playing

I am playing audio from http link using media player and I release the mediaPlayer onStop() of the activity in which the mediaPlayer plays the song. I want the song to continue playing unless user exits from the app ie exits from the home page. how can i stop and release the media player from the home page?
You're going to need a Service that plays the music and then your App will just send message to the service.

Android: Background music is playing while lock screen is on

If my activity is running in the foreground and the display is turned off and then turned back on again the background music starts to play again before the screen is unlocked. The background music is paused in onPause() and continues to play in onResume(). So when the app is pushed to the background the music stops.
Is there a way to prevent the music from playing while the lock screen is on?
//André
Take a look over here.This will solve your problem.
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

Categories

Resources