Jonh Smith
Jonh Smith

Reputation: 65

setVisibility(int) in android works after delay

Hey guys I'm trying to make an image appear on the touch of a button by using

Myimage.setVisibility(View.VISIBLE);

Here is part of the code :

case R.id.button5:
               if(myRandom == 5 )
                {
                   Myimage.setVisibility(View.VISIBLE);
                    MediaPlayer mp =  MediaPlayer.create(getApplicationContext(), R.raw.music);
                    mp.start();
                    while(mp.isPlaying())
                    {

                    }
                    found = true;

                }
                else

                 b5.setVisibility(View.GONE);
               break;

Now for b5 which is a view for a button the function works fine, the button disapears. For Myimage however getvisibility() tells me it is == visible but i cant see it on the screen, i can only see it after the switch statement is over Any thoughts?

I have tried defining a function as such so that tit runs on a seperate thread but i have the same problem of the image not appearing.

   public void startplayer()
       {
           Thread thread = new Thread(new Runnable() {
               public void run() {
                   mp = MediaPlayer.create(getApplicationContext(), R.raw.fart);
                   mp.start();
                   while(mp.isPlaying())
                   {

                   }
               }
           });
           thread.start();

Thanks.

I ended up getting it working with :

 new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.donkey);
                       mp.start();
                       while(mp.isPlaying())
                       {

                       }


                    finish();
                }
            }, 0);

Upvotes: 0

Views: 1215

Answers (2)

maxandron
maxandron

Reputation: 1660

This probably happens because you block the UI thread with your while loop. You should never do long operations or wait on the main thread.

To understand better why the main thread should be kept free, read the official docs

In your specific case you should implement a callback. Just do

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    override void onCompletion(MediaPlayer mp) {
        // Here you recieve the callback even.
    }
})

What callbacks do (and get used to them in android), is notify you when something happens. So now instead of waiting for the player to finish playing by yourself - you just get notified when it happens. Remember - the MediaPlayer knows when it stopped playing, you shouldn't do it's job for him. A nice blogpost with more info on callbacks

Every time you are thinking about doing a while(true) loop, think again - something is wrong.

Upvotes: 1

Dat Nguyen
Dat Nguyen

Reputation: 1646

I think you block the UI thread by running this code on the UI thread:

MediaPlayer mp =  MediaPlayer.create(getApplicationContext(), R.raw.music);
mp.start();
while(mp.isPlaying())
{

}

The code above will basically behave like an infinite loop on UI thread as long as your MediaPlayer object is playing. You should post this block of code onto a background thread instead, or at least the "while(mp.isPlaying())" part.

Upvotes: 0

Related Questions