Search code examples
androidanimationframehttp-live-streaming

Why animation is not displaying front while streaming in android


I am doing an radio streaming app. I am trying to display some loading bar frame by frame animation before my stream begins. but the animations starts after playing the stream only. How could i show my anim before stream loads. Here is my code snippet. Help is appreciated...

      bhajan_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ConnectivityManager conMan = (ConnectivityManager)         getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo Info = conMan.getActiveNetworkInfo();
            if (Info == null) {
                Toast.makeText(BhajanStream.this, "POOR SIGNALS ",
                        Toast.LENGTH_LONG).show();
                // startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            }

            else {

                loadanim.setBackgroundResource(R.drawable.loader_1);
                loadanim.setBackgroundResource(R.anim.loadanim);
                loadanimation = (AnimationDrawable) loadanim
                        .getBackground();
                loadanimation.isVisible();
                effectanim.setBackgroundResource(R.drawable.effect_1);
                effectanim.setBackgroundResource(R.anim.musiceffect);
                effectanimation = (AnimationDrawable) effectanim
                        .getBackground();
                bhajan_play.setBackgroundResource(R.drawable.bhajan_start);
                bhajan_play.setVisibility(View.GONE);
                bhajan_stop.setVisibility(View.VISIBLE);
                loadanim.setVisibility(View.VISIBLE);
                effectanim.setVisibility(View.VISIBLE);
            }

            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(rs_bhajan_uri);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                mediaPlayer.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaPlayer.start();

        }

    });

      }

protected void onPreExecute() {
    // UI work allowed here

    loadanimation.start();
      }

Solution

  • You should call prepareAsync() rather than just prepare().
    From the MediaPlayer documentation:

    For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

    (Calling prepare will block your Ui Thread until the data is prepared, meaning that updating your views has to wait until the preparation is done)

    Example implementation:

        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });