jobe
jobe

Reputation: 325

Ringtone playing too long

I'm using a RingtoneManager with RingtonePreference. When I use the default Ringtone, there is no problem, but when i use a configured Ringtone, it's playing for minutes... I don't know if the song's duration is minutes or if it plays in a loop...

here my code:

private static void playNotificationSound(Context context) {
    RingtoneManager rm  = new RingtoneManager(context);
    String ringtone = MySharedPreferences.ringtone(context);
    Uri uri = null;
    if(ringtone == null)
        uri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    else
        uri = Uri.parse(ringtone);
    if (uri != null) {
        Ringtone rt = RingtoneManager.getRingtone(context, uri);
        if (rt != null) {
            rt.setStreamType(AudioManager.STREAM_NOTIFICATION);
            rt.play();
        }
    }
}

I use it to play a song with a notification and I don't want the phone to play for 5minutes...

Upvotes: 1

Views: 444

Answers (3)

Computer Gaga
Computer Gaga

Reputation: 161

Try adding the following Line.

ringtoneManager.stopPreviousRingtone();

Upvotes: 1

jobe
jobe

Reputation: 325

I found another solution, I set the notification.sound = uri; and don't use the RingtoneManager anymore.

with notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;

but the songs still plays in a loop. Maybe the ringtones really are 5minutes long? Or is it possible for a ringtone to be configured as a loop song?

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Can you use thread like this?

if (rt != null) {
                rt.setStreamType(AudioManager.STREAM_NOTIFICATION);

                rt.play();
            }

            Thread myThread = new Thread() {

                int wait = 0;

                @Override
                public void run() {
                    try {
                        super.run();

                        while (wait < 5000) {
                            sleep(500);
                            wait += 500;
                        }
                    } catch (Exception e) {

                    } finally {
                        if (rt.isPlaying())
                            rt.stop();

                    }
                }
            };
            myThread.start();

Upvotes: 0

Related Questions