我正在我的一个Android项目中工作,我正在播放媒体播放器。当我使用this URL播放歌曲时,我得到一些IOException,如下所示,但歌曲在MediaPlayer中播放得很好。应该是什么问题?
public void startMediaPlayer(String url) {
Log.e("urlToPlay==>",url);
// start new song for play
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
}else{
mediaPlayer.reset();
}
Uri myUri1 = Uri.parse(url);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getActivity(), myUri1);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
playPause=true;
player.start();
mediaFileLengthInMilliseconds = player.getDuration(); // gets the song length in milliseconds from URL
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playPause=false;
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
Log.e("onError==>","Something went wrong"+arg1+"");
Log.e("onError==>","Something went wrong"+arg2+"");
return false;
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
mediaPlayer.release();
} catch (SecurityException e) {
e.printStackTrace();
mediaPlayer.release();
} catch (IllegalStateException e) {
e.printStackTrace();
mediaPlayer.release();
} catch (IOException e) {
e.printStackTrace();
mediaPlayer.release();
}
}
播放文件时出现以下异常
06-01 15:30:24.758: D/MediaPlayer(12450): setDataSource IOException happend :
06-01 15:30:24.758: D/MediaPlayer(12450): java.io.FileNotFoundException: No content provider: http://pleer.com/browser-extension/files/72338325d4W.mp3
06-01 15:30:24.758: D/MediaPlayer(12450): at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1053)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:907)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:834)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:969)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:923)
06-01 15:30:24.758: D/MediaPlayer(12450): at com.hyper.musicapp.fragments.SearchFragmentMusic.startMediaPlayer(SearchFragmentMusic.java:998)
06-01 15:30:24.758: D/MediaPlayer(12450): at com.hyper.musicapp.fragments.SearchFragmentMusic$SearchSoundCloudTrackAdapter$1.onClick(SearchFragmentMusic.java:473)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.view.View.performClick(View.java:4639)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.view.View$PerformClick.run(View.java:19252)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.os.Handler.handleCallback(Handler.java:733)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.os.Handler.dispatchMessage(Handler.java:95)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.os.Looper.loop(Looper.java:146)
06-01 15:30:24.758: D/MediaPlayer(12450): at android.app.ActivityThread.main(ActivityThread.java:5511)
06-01 15:30:24.758: D/MediaPlayer(12450): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 15:30:24.758: D/MediaPlayer(12450): at java.lang.reflect.Method.invoke(Method.java:515)
06-01 15:30:24.758: D/MediaPlayer(12450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
06-01 15:30:24.758: D/MediaPlayer(12450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
06-01 15:30:24.758: D/MediaPlayer(12450): at dalvik.system.NativeStart.main(Native Method)
06-01 15:30:24.758: D/MediaPlayer(12450): Couldn't open file on client side, trying server side
答案 0 :(得分:2)
尝试添加网址方案,例如" file://
"或" http://
"。当我在尝试播放本地文件时遇到同样的异常时,为我工作。