android获取系统铃声并播放

private void startAlarm() throws IllegalStateException, IOException {
        mMediaPlayer = MediaPlayer.create(this, getSystemDefultRingtoneUri());
        mMediaPlayer.setLooping(true);//设置循环
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }

//获取系统默认铃声的Uri
    private Uri getSystemDefultRingtoneUri() {
        return RingtoneManager.getActualDefaultRingtoneUri(this,
                RingtoneManager.TYPE_RINGTONE);
    }

 发现了个问题,因为startAlarm是throw一个异常,在方法外面捕获异常无济于事(不会播放铃声),要在真正需要的地方捕获异常,所以重新重构了一下:

private void startAlarm() {
        mMediaPlayer = MediaPlayer.create(this, getSystemDefultRingtoneUri());
        mMediaPlayer.setLooping(true);
        try {
            mMediaPlayer.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaPlayer.start();
    }

 

你可能感兴趣的:(时钟,android)