Activity ringtone playback stops abruptly

In my Android app, I'm trying to play the current default onCreate ringtone of my activity. But the ringtone will not just play completely and stop at an arbitrary length every time.

The code is as simple as possible, any help would be appreciated.

Uri currentUri = RingtoneManager.getActualDefaultRingtoneUri(this.context, 
    RingtoneManager.TYPE_RINGTONE);
Ringtone ringtone = RingtoneManager.getRingtone(this.context,currentUri);
if (ringtone != null){
    ringtone.play();
}
+5
source share
2 answers

I use TYPE_NOTIFICATION, but the appart from the fact that my code was more or less exactly the same.

I found that playing sound inside a new handler helped in my case:

        Uri currentUri = RingtoneManager.getActualDefaultRingtoneUri(
                this.context, RingtoneManager.TYPE_RINGTONE);
        final Ringtone ringtone = RingtoneManager.getRingtone(this.context,
                currentUri);
        if (ringtone != null) {
            new Handler().post(new Runnable() {

                @Override
                public void run() {
                    ringtone.play();
                }
            });
        }

Why did this solve the problem?

, RingtoneManager , , / , .

, , .

, : post?

0

, .

, ( )

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder;
        mBuilder =
                new NotificationCompat.Builder(this)
                .setSound(alarmSound)
                .setVibrate(new long[]{1000,1000});

        mNotificationManager.notify(ANY_INT_WILL_DO_HERE, mBuilder.build());

. , .

, ,

0

All Articles