1

I'm making an alarm application and I'm stuck at the alarm tone phase. I'm currently trying to use the RingtoneManager to set the alarm tone however it's not working at all. Furthermore, I have a seekbar that should play the alarm tone the user chose, but it doesn't. When I set the alarm tone, I don't want it to be the default, just the tone for that alarm because the user has the option to create more than one alarm.

This is the seekbar code:

volumeBar = (SeekBar) findViewById(R.id.seekBarVolume);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mMediaPlayer = new MediaPlayer();

        volumeBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
        volumeBar.setKeyProgressIncrement(1);
        volumeBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));

        volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                try {
                    Uri alert = RingtoneManager
                            .getDefaultUri(RingtoneManager.TYPE_ALARM);

                    mMediaPlayer.setDataSource(getApplicationContext(), alert);
                    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0 && !mMediaPlayer.isPlaying()) {
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                        mMediaPlayer.setLooping(true);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, AudioManager.FLAG_SHOW_UI);
            }
        });

This is the intent for the ringtone picker:

case R.id.buttonAlarmTones:
                try {
                    mMediaPlayer.release();
                    mMediaPlayer.stop();
                } catch (Exception e) {
                    // TODO: handle exception
                }
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM));
                startActivityForResult(intent, 5);
                break;

Finally, here is the onActivityResult():

@Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);
        if (arg1 == RESULT_OK && arg0 == 5) {
            Uri uri = arg2.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);

            Uri uri2 = Uri.parse(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString());
            Ringtone ringtone2 = RingtoneManager.getRingtone(getApplicationContext(), uri2);

            if (uri != null) {
                selectedAlarmTone.setText(ringtone.getTitle(getApplicationContext()));
                RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, uri);
            } else {
                selectedAlarmTone.setText(ringtone2.getTitle(getApplicationContext()));
                RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, uri2);
            }
        }
    }

Any help would be highly appreciated. Thanks.

Navio53
  • 591
  • 1
  • 6
  • 20

1 Answers1

1

Here is an example using RingtoneManager

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
    ringtone.play();

You do not need MediaPlayer.

guycole
  • 788
  • 5
  • 10
  • How can I loop the `ringtone.play()` once the seekbar has been used? I would probably have to use a medidaplayer for that. – Navio53 Jul 05 '14 at 22:01
  • I don't see a listener, but you could poll Ringtone.isPlaying(). Not saying this is great, but it is an option. – guycole Jul 05 '14 at 22:08
  • Never mind I found out why it wasn't looping. Some of the "ringtones" were notification sounds. Anyways, thanks man you helped me solve my problem! – Navio53 Jul 05 '14 at 22:12
  • `Ringtone` is just a ringtone not an alarm sound. So if the ringtone volume is muted, `ringtone.play()` won't sound even when alarm sound is not muted. `ringtone.play()` continues to make sound whether alarm sound is muted or not. So it is just misapplication of `Ringtone` – johnkarka Aug 06 '15 at 21:29
  • code posted there http://stackoverflow.com/questions/2618182/how-to-play-ringtone-alarm-sound-in-android works perfectly for me – johnkarka Aug 06 '15 at 21:44