Reputation: 1600
So basically I use a Spinner widget and pass it the RingtoneManager Picker action, the user then selects their ringtone. Then I call onActivityResult() and get the uri for the ringtone. Finally I pass the uri to another activity where I have a alarm setup to go off after a specific amount of time.
THE PROBLEM >>> when I get the uri for the ringtone in the 2nd activity and let mediaPlayer play it, It...it doesn't stop. No matter WHAT I try.
This is the 2nd activity and the mediaPlayer that never stops.
Uri ringtone;
ringtone = Uri.parse(musixType);
//mMediaPlayer.setDataSource(DisplayNotification.this, ringtone);
mMediaPlayer = MediaPlayer.create(DisplayNotification.this, ringtone);
mMediaPlayer.start();
mMediaPlayer.setVolume(100, 100);
}
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
while (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
});
How do I get it to stop @.@
Edit: Could the reason it doesn't stop playing be that it is a ringtone from the RingtoneManager? I don't know why this would matter but I'm grasping at straws at this point.
Edit: Is there a way to specify a certain length of time for mediaPlayer to run and disregard the data passed to it?
Upvotes: 2
Views: 2024
Reputation: 1835
I had this problem too and solved it like this:
player.setDataSource(this, ringtone);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
mp.stop();
mp.release();
}
}, mp.getDuration()); <-- Make a postDelayed runnable that has the duration of the file as its cut off. Once the song plays, it will stop the mediaPlayer.
mp.start();
}
});
Upvotes: 0
Reputation: 17850
Since you're not calling setLooping(true);
on your mMediaPlayer
referenced object, looping should not be the issue here as default is set false
. Make sure you're not actually calling this piece of code multiple times from outside. Put a Log
line before mMediaPlayer = MediaPlayer.create(DisplayNotification.this, ringtone);
and see how many times it gets logged.
Upvotes: 0
Reputation: 54672
did you use MediaPlayer.setLooping method
use mMediaPlayer.setLooping(false);
Upvotes: 2