在Android中使用来自RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)的uri播放声音时无法创建媒体播放器

时间:2012-08-01 02:17:14

标签: android android-mediaplayer

我正在尝试使用以下代码在Android中播放默认闹钟声音 -

private void startAlarm() {
        Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alert == null) {
            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (alert == null) {
                alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
        }
        MediaPlayer mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(getActivity().getApplicationContext(), alert);
            AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            int volumen = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
            if (volumen != 0) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mMediaPlayer.setLooping(true);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

但它正在抛出以下异常 -

08-01 10:59:10.478: E/MediaPlayer(4529): Unable to to create media player
08-01 10:59:16.158: W/System.err(4529): java.io.IOException: setDataSource failed.: status=0x80000000
08-01 10:59:16.166: W/System.err(4529):     at android.media.MediaPlayer.setDataSource(Native Method)
08-01 10:59:16.174: W/System.err(4529):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:745)
08-01 10:59:16.174: W/System.err(4529):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:675)

我调试了Uri警报的代码和值

content://settings/system/alarm_alert

我无法弄清楚为什么它无法创建媒体播放器并抛出该异常?有人可以指出问题所在。

提前致谢

0 个答案:

没有答案