在 Android 中,我试图创建一个实例MediaPlayer
来播放警报音。我意识到有一个alarmmanager,但此时我对创建服务的麻烦不感兴趣。现在我想获取用户默认的警报音加载,循环播放。不幸的是,我的以下代码导致错误(to to 不是错字):
无法创建媒体播放器
在此错误之前,会立即记录以下调试语句:
无法在客户端打开文件,正在尝试服务器端
我已经记录了警报的路径,它被列为:/system/alarm_alert
。到目前为止,此代码仅在针对 Android 2.2 API 级别 8 的模拟器中运行。以下代码有什么问题会导致此错误?
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
// I can't see this ever being null (as always have a default
// notification) but just incase alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Log.d("alertPath", alert.getPath());
MediaPlayer mediaPlayer;
setVolumeControlStream(AudioManager.STREAM_ALARM);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, alert);
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
mediaPlayer.start();
} catch (IOException e) {
} catch (Exception e) {
Log.d("mp", e.getMessage());
}