我正在尝试从我的Android应用程序(目标5.0+)播放.opus媒体文件。该文件的来源是设备外部存储。
MediaPlayer继续抛出“java.io.IOException:Prepare failed:status = 0x1”。 要注意我能够播放其他文件类型,如mp3和aac
注意:已授予READ_EXTERNAL_STORAGE权限。
提前谢谢。
位于Android Fragment内的部分代码:
public void playVoiceNote(VoiceNoteBO voiceNoteBO){
try{
Uri myUri = Uri.fromFile(new File(voiceNoteBO.getFileName())); //this resolves to the full path of the file.
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getActivity().getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exception e){
//TODO
e.printStackTrace();
}
}
堆栈跟踪:
06-04 18:31:46.129 1134-27920/? E/GenericSource: Failed to init from data source! 06-04 18:31:46.129 27425-27443/? E/MediaPlayer: error (1, -2147483648) 06-04 18:31:46.129 27425-27425/? W/System.err: java.io.IOException: Prepare failed.: status=0x1 06-04 18:31:46.130 27425-27425/? W/System.err: at android.media.MediaPlayer._prepare(Native Method) 06-04 18:31:46.130 27425-27425/? W/System.err: at android.media.MediaPlayer.prepare(MediaPlayer.java:1135) 06-04 18:31:46.130 27425-27425/? W/System.err: at com.frankyapps.privateread.prws.fragments.VoiceNotesFragment.playVoiceNote(VoiceNotesFragment.java:166) 06-04 18:31:46.130 27425-27425/? W/System.err: at com.frankyapps.privateread.prws.fragments.VoiceNotesFragment$1.onClick(VoiceNotesFragment.java:76) 06-04 18:31:46.130 27425-27425/? W/System.err: at com.frankyapps.privateread.prws.fragments.VoiceNotesFragment$RecyclerTouchListener.onInterceptTouchEvent(VoiceNotesFragment.java:217) 06-04 18:31:46.130 27425-27425/? W/System.err: at android.support.v7.widget.RecyclerView.dispatchOnItemTouch(RecyclerView.java:2261) 06-04 18:31:46.130 27425-27425/? W/System.err: at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2389) 06-04 18:31:46.130 27425-27425/? W/System.err: at android.view.View.dispatchTouchEvent(View.java:8471)
答案 0 :(得分:1)
尝试使用create()方法创建MediaPlayer对象。 这就是你如何做到的:
MediaPlayer mediaPlayer = MediaPlayer.create(context, myUri);
mediaPlayer.start();
无需调用prepare()方法(如果使用create()实例化MediaPlayer对象),因为create()将由create()隐式调用。
此链接可以帮助您确定:https://developer.android.com/reference/android/media/MediaPlayer.html