简体   繁体   中英

How to Set the Alarm Ringtone from Within My Alarm Application in Android

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM