我正在尝试播放闹钟声但是我收到了这个错误,我听不到声音。它不会在应用程序或崩溃中造成任何问题,但没有声音。
03-13 00:35:01.138: D/MediaPlayer(2580): Couldn't open file on client side, trying server side
03-13 00:35:01.138: E/MediaPlayer(2580): Unable to create media player
03-13 00:35:01.138: I/alarmReceiver(2580): No audio file founded!
根据我的调试,此错误发生在此代码行运行之前:
mMediaPlayer.setDataSource(context, alert);
这不仅发生在模拟器中,也发生在真实设备中;我听不到任何声音。
这是我的代码:
playSound(this, getAlarmUri());
private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.reset();
**mMediaPlayer.setDataSource(context, alert);**
final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Log.i("getStreamVolume", am.getStreamVolume(AudioManager.STREAM_ALARM)+"");
if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (Exception e) {
// TODO: handle exception
Log.i("alarmReceiver", "No audio file founded!");
}
}
private Uri getAlarmUri()
{
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);
}
}
Log.i("alert", alert.toString());
return alert;
}
答案 0 :(得分:0)
尝试在重置媒体播放器之前设置数据源。
...
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, alert);
try
{
mMediaPlayer.reset();
...
答案 1 :(得分:0)
好的家伙我找到了解决问题的方法。
我在我的res文件夹下创建了原始文件夹,并将我的song.mp3放在那里
mMediaPlayer = new MediaPlayer();
try {
final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer = MediaPlayer.create(context, R.raw.song);
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
完成后,不要忘记停止并释放mediapleayr。
mMediaPlayer.stop();
mMediaPlayer.release();