¿Cómo hacer un bucle/repetir el tono de llamada actual en Android desde una aplicación?

I am writing an application on android 4.0 which will play the current ringtone when I press a button.

But in the ringtone is played only one time. I need it to repeat for a few times.

Mi código actual:

Uri notifi = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
final Ringtone r = RingtoneManager.getRingtone(c, notifi);
r.play();

preguntado el 05 de septiembre de 12 a las 10:09

Uri notifi = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); final Ringtone r = RingtoneManager.getRingtone(c, notifi); r.play(); -

5 Respuestas

Try this code I have used this before and able to play Ringtone continuously until you stop

try {
   Uri alert =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
  mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setDataSource(this, alert);
  final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
 mMediaPlayer.setLooping(true);
 mMediaPlayer.prepare();
 mMediaPlayer.start();
}
} catch(Exception e) {
}   

Respondido el 05 de Septiembre de 12 a las 13:09

@satya.I create MediaPlayer in Service class, as I want to use ` mediaPlayer.setDataSource(getApplicationContext(),uri);, it shows this exception: Unhandled Exceptiom:java.io.IOException`. how can i solveit? - mina dahesh

@Sam. it's too old! i don't remember why i need it. i will ask you,as i find it. - mina dahesh

Not work with ringtone! : java.io.IOException: setDataSource - Boris Salimov

Tried the above codes on lollipop and only this worked for me

  //activating looping ringtone sound
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    player = MediaPlayer.create(this, notification);
    player.setLooping(true);
    player.start();

contestado el 17 de mayo de 15 a las 22:05

You can have a timer regularly check if the ringtone is still playing. For example, every second:

mRingtone.play();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        if (!mRingtone.isPlaying()) {
            mRingtone.play();
        }
    }
}, 1000*1, 1000*1);

Respondido el 22 de Septiembre de 17 a las 20:09

I have read that ringtone has to have ANDROID_LOOP tag. Ref: http://xanderx.com/2010/08/25/making-ringtones-loop-on-android/

You can also try to play this file using AudioManager and set it looping. Ref: http://developer.android.com/reference/android/media/MediaPlayer.html#setLooping(boolean)

Respondido el 05 de Septiembre de 12 a las 10:09

I achieved this issues by set looping true with Ringtone and ringtone manager.

 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.setLooping(true);
 r.play();

Here I am starting ringtone and when we want to stop ringtone we can call

r.stop();

method to stop ringtone

Respondido el 10 de diciembre de 21 a las 10:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.