Catch exception in streaming mp3

advertisements

I need to stream mp3 file in my app, and I face with a problem if a mp3 file that is located on the server is missing. So my code works great except when there is no mp3 on that location. Whenever that happens my app crashes. Can someone help me catch this error, and disable the app from crashing when there is no mp3 audio on that location.

Here is my code:

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();

This is the logCat output:

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

So can someone help me?


playSong.setBackgroundResource(R.drawable.play_nr);

you can't edit your layout from inside a thread, define a handler at the beginning of your activity's class

Handler handler = new Handler();

then use it inside doInBackground:

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