0

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

那么有人可以帮助我吗?

4

2 回答 2

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);
    }
});
于 2013-08-29T16:03:53.523 回答
0

您正在尝试从 catch 块中操作 UI 元素,但您未按要求在 UI 线程上。

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

于 2013-08-29T16:15:01.313 回答