我需要在我的应用程序中流式传输 mp3 文件,如果缺少位于服务器上的 mp3 文件,我会遇到问题。所以我的代码运行良好,除非该位置没有 mp3。每当发生这种情况时,我的应用程序就会崩溃。有人可以帮我发现这个错误,并在该位置没有 mp3 音频时禁用应用程序崩溃。
这是我的代码:
mediaPlayer.setDataSource(data.get(position)
.getSONG_MP3());
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
bufferingDialog.setMessage(activity
.getString(R.string.buffering));
bufferingDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("--", "4");
bufferingDialog.dismiss();
((AlbumDetails) activity)
.changePicToPlay(position);
playSong.setBackgroundResource(R.drawable.play_nr);
Toast.makeText(activity,
"Song not existing",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mediaPlayer.start();
return null;
}
@Override
protected void onPostExecute(Void result) {
if (bufferingDialog.isShowing())
bufferingDialog.dismiss();
super.onPostExecute(result);
}
}.execute();
这是 logCat 输出:
08-29 17:54:54.811: E/AndroidRuntime(8701): FATAL EXCEPTION: AsyncTask #3
08-29 17:54:54.811: E/AndroidRuntime(8701): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.lang.Thread.run(Thread.java:856)
08-29 17:54:54.811: E/AndroidRuntime(8701): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4050)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.View.requestLayout(View.java:12677)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.View.requestLayout(View.java:12677)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.View.requestLayout(View.java:12677)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.View.requestLayout(View.java:12677)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.view.View.requestLayout(View.java:12677)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.widget.AbsListView.requestLayout(AbsListView.java:1690)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:801)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5592)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:35)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
08-29 17:54:54.811: E/AndroidRuntime(8701): at com.outsourcefarm.musicagratis.activities.AlbumDetails.changePicToPlay(AlbumDetails.java:228)
08-29 17:54:54.811: E/AndroidRuntime(8701): at com.outsoucefarm.musicagratis.functions.AlbumDetailsAdapter$2$1.doInBackground(AlbumDetailsAdapter.java:194)
08-29 17:54:54.811: E/AndroidRuntime(8701): at com.outsoucefarm.musicagratis.functions.AlbumDetailsAdapter$2$1.doInBackground(AlbumDetailsAdapter.java:1)
08-29 17:54:54.811: E/AndroidRuntime(8701): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-29 17:54:54.811: E/AndroidRuntime(8701): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-29 17:54:54.811: E/AndroidRuntime(8701): ... 5 more
那么有人可以帮助我吗?