我目前正在开发一个 Android 应用程序,它会以不同的时间间隔向用户播放声音。我有工作代码(包括在下面),它在我的 Hero(运行 2.2)和 1.6 模拟器上的功能完全符合预期。但是,它在我的朋友 Xperia x8 上不起作用——没有声音播放。他设置了通知音,当他收到短信等时播放良好。
private void prepareAudio(){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mp = new MediaPlayer();
if(alert != null){
try {
mp.setDataSource(ctx, alert);
} catch (IllegalArgumentException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (SecurityException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (IllegalStateException e) {
Log.i("Prepare Audio", e.getMessage());
} catch (IOException e) {
Log.i("Prepare Audio", e.getMessage());
}
}
am = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
}
private void notifyUser(){
if(alert != null){
if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(false);
try {
mp.prepare();
} catch (IllegalStateException e) {
Log.i("Notify User", e.getMessage());
} catch (IOException e) {
Log.i("Notify User", e.getMessage());
}
mp.start();
} else {
Log.i("Notify User", "Alarm volume zero");
}
} else {
Log.i("Notify User", "Uri is null");
}
long[] pattern = {0,200,300,600};
v.vibrate(pattern, -1);
}
PrepareAudio 在初始化类时被调用,并且每次我们想要播放声音时都会调用 notifyUser。手机总是在应该振动的时候振动,所以肯定会打电话给 notifyUser。
我朋友安装的版本使用 e.printStackTrace 而不是 Log.i,所以 logcat 中没有任何内容被吐出。我将尝试获取他的手机以将其更新到带有 Log.i 的版本,但与此同时,代码是否有任何明显错误可能导致这种间歇性问题?
谢谢,