Reputation: 2145
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.
Edited to add the code in the activity that plays the ringtone.
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(prefs.getString(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");
}
Upvotes: 1
Views: 915
Reputation: 11
Call play in onResume - this work for me
public class AlarmActivity extends Activity {
private Uri uri = null;
private int volume;
private MediaPlayer mp = null;
private void play() {
if (uri == null) {
return;
}
mp = new MediaPlayer();
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * volume / 100, 0);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(mpOnPrepared);
mp.setOnCompletionListener(mpOnCompletion);
try {
mp.setDataSource(this, uri);
mp.prepare();
} catch (IOException e) {
mp.release();
mp = null;
return;
}
}
private OnPreparedListener mpOnPrepared = new OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
};
private OnCompletionListener mpOnCompletion = new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
}
};
private void stop() {
if (mp != null) {
mp.release();
mp = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_alarm);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
SharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE);
uri = Uri.parse(sp.getString("uri", ""));
volume = sp.getInt("volume", 100);
}
@Override
protected void onResume () {
super.onResume();
play();
}
@Override
protected void onPause () {
super.onPause();
stop();
finish();
}
}
Upvotes: 1
Reputation: 9870
This is an older post, but I had the same problem and think I know what is missing here. It is just an assumption without seeing more of Your code, but usually, if You use a mediaPlayer it is a good practise to stop it in onStop() and in onPause() (or pause it in onPause()). If You have done this, here comes a possible solution. In onPause or onStop, don´t call:
mMediaPlayer.stop();
(and all the needed stuff like release etc). Because when You call the WakeLock and set the Flags for keeping the screen on etc., onStop() and onPause() are called multiple times for Your activity after onCreate is done. This is what I had detected on my device Samsung Galaxy s3 with Android 4.1.2 Instead, to get sure the MediaPlayer is stopped if it is finished, do this:
if(this.isFinishing()){
mMediaPlayer.stop();
}
With this You get sure it is called only when Your activity is really finishing. This should work.
Upvotes: 5
Reputation: 44571
Try adding
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
to the Activity
that starts the MediaPlayer
You need to use this to turn on the screen, and unlock it if you wish, before the Activity
will start.
Upvotes: 0