我正在下载带DownloadManager
的MP3,下载完成后我尝试用MediaPlayer
播放MP3。
下载完成后,当我得到length()
文件返回ZERO导致我得到java.io.IOException:来自MediaPlayer.setDataSource()
的setDataSourceFD失败错误。如果我等待几秒钟然后为MediaPlayer设置数据源,那么工作正常!
public void initMediaPlayer() {
if (mediaPlayer == null)
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
try {
File audioDir = new File(getExternalFilesDir(AUDIO_SERVICE), activeAudio.getAudioBooksTableOfContentsID() + ".mp3");
if (audioDir.exists()) {
FileInputStream is = new FileInputStream(audioDir);
Log.d("audioDir.length()", String.valueOf(audioDir.length()));
mediaPlayer.setDataSource(is.getFD()); //audioDir.getPath()
mediaPlayer.prepareAsync();
is.close();
}
} catch (Exception e) {
//TODO
e.printStackTrace();
}
}
错误:
W/System.err: java.io.IOException: setDataSourceFD failed.: status=0x80000000
W/System.err: at android.media.MediaPlayer._setDataSource(Native Method)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1153)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1136)
W/System.err: at com.example.services.MediaPlayerService.initMediaPlayer(MediaPlayerService.java:280)
W/System.err: at com.example.services.MediaPlayerService.playNewAudio(MediaPlayerService.java:757)
W/System.err: at com.example.services.MediaPlayerService.handleIncomingActions(MediaPlayerService.java:718)
at com.example.services.MediaPlayerService.onStartCommand(MediaPlayerService.java:132)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3327)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
抱歉我的英文不好