android mediaplayer - play ringtone one time
Asked Answered
J

2

2

I try to play a device ringtone one time with the MediaPlayer class (I have to use MediaPlayer as I use the ringtones together with other audio sources in my app) So i try the following to play a random ringtone yet I cant stop it looping, it keeps playing. Is it possible to play a ringtone only one time with MediaPlayer.

Thanks

try {
    RingtoneManager mRing = new RingtoneManager(RingtoneActivity.this);
    int mNumberOfRingtones = mRing.getCursor().getCount();
    Uri mRingToneUri = mRing.getRingtoneUri((int) (Math.random() * mNumberOfRingtones));
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(RingtoneActivity.this, mRingToneUri);
    mMediaPlayer.prepare();
    mMediaPlayer.setLooping(false);
    mMediaPlayer.start();

} catch (Exception ignore) {
}

UPDATE:

I just have posted a new question related to this one as I was able to find the specific cause of the trouble in my case.

Jarboe answered 11/10, 2012 at 5:13 Comment(0)
B
9

I found simple solution with use events - confirm answer since it solves your problem :)

mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        mediaPlayer.stop();
                    }
                });

http://developer.android.com/reference/android/media/MediaPlayer.html

Remember about release().

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.release();
                }
            });

Full code it your want to use need set ring tone to something what you would like.

        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        int volume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
        Uri notificationSoundUri = Uri.parse(prefs.getString("notification_sound", "DEFAULT_SOUND"));

        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(view.getContext(), notificationSoundUri);
        } catch (Exception e1) {
            e1.printStackTrace();
            mediaPlayer.release();
            return;
        }
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
            }
        });
        try {
            mediaPlayer.prepare();
        } catch (Exception e1) {
            e1.printStackTrace();
            mediaPlayer.release();
            return;
        }
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {

            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {
                // TODO Auto-generated method stub
                mediaPlayer.stop();
                mediaPlayer.start();
            }
        });
        mediaPlayer.setVolume(volume, volume);
        mediaPlayer.start();
Boogie answered 26/1, 2013 at 20:58 Comment(0)
C
0

use Notifications

   NotificationManager nManager = 
           (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    
   NotificationCompat.Builder notifBuilder = 
           new NotificationCompat.Builder(context);
   notifBuilder.setSound(sound);
   notification = notifBuilder.build();

   nManager.notify(SOME_INT_CONSTANT, notification);
Cuckoo answered 3/12, 2013 at 1:17 Comment(1)
thanks - sorry i have no android development setup right now to test it (traveling ...)Jarboe

© 2022 - 2024 — McMap. All rights reserved.