流式传输时捕捉异常

时间:2013-08-29 15:57:14

标签: android android-asynctask android-mediaplayer

我需要在我的应用程序中传输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

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

playSong.setBackgroundResource(R.drawable.play_nr);

您无法在线程内编辑布局, 在活动类的开头定义一个处理程序

Handler handler = new Handler();

然后在doInBackground中使用它:

handler.post(new Runnable() {
    @Override
    public void run() {
      playSong.setBackgroundResource(R.drawable.play_nr);
    }
});

答案 1 :(得分:0)

您正在尝试从catch块操纵UI元素,但您不是根据需要在UI线程上。

如果成功, doInBackground 返回 true ,如果失败则返回 false (将 Void 结果更改为布尔)并将所有与UI相关的代码移至 onPostExecute