• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps MusicPlayer does not play when started from locked screen with AlarmManager

I am writing an alarm (kinda) app, which registers a broadcast intent with AlarmManager with the RTC_WAKEUP flag, to go off at a specified time. Works...

When the broadcast is received, the receiver starts an activity that plays a ringtone with MediaPlayer using the STREAM_ALARM stream, and does some other stuff. All that works, but if the broadcast is received when the screen is off, the activity is starts up correctly but the ringtone does not play. Works fine and plays ringtone if received when the screen is on and unlocked.

Are there any additional flags to set? Here's the code of the activity started from the Receiver.

[HIGH]
public void onCreate(Bundle bundle) {
super.onCreate(bundle)
...
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ringtonePlayer = new MediaPlayer();
...
playRingtone();
}

private void playRingtone() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
ringtonePlayer.setDataSource(getRingTone(), RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE).toString()));

ringtonePlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
ringtonePlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
ringtonePlayer.setLooping(true);
ringtonePlayer.prepare();
ringtonePlayer.start();
Log.i(LOG_TAG, "Ringtone started");
}
[/HIGH]
 
  • Like
Reactions: mally2k

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones