Search code examples
androidmultithreadingmedia-player

Prevent using a button until it is ready


So I have seen some other posts similar, but not quite answering what I am looking for.

Basically onCreate is loading in the content (being textviews and image) and it also opens up another thread to prepare the media player so that when the button is pressed it plays a specific song. If this activity is opened and the button is pressed quickly it does not play anything (which I am assuming because it hasn't had a chance to prepare). If I open the activity and give it a few moments it works just fine. I do realize that it is not best to run the content being loaded in onCreate, but I have tried to get it to load in other threads and it failed miserably. It is all fine with me what I really want to happen is this.

I need some way to prevent use of the button or let them know it is loading for about 3-5 seconds. So either make the button be faded out and unusable for 3-5 seconds and then it becomes active or a loading screen that is some what transparent that covers the activity for about 3-5 seconds. If you mention using other threads could you please demonstrate it if it isn't asking to much or show me something like a tutorial (other than googles notes). I don't understand threads very well yet (pretty new to them) and the AsyncThread is pretty confusing to me.

If you need code let me know what you would want posted. Thanks and I appreciate any help.


Solution

  • You can set an setOnPreparedListener on your MediaPlayer, in this listener you enable your button and be save that the song can be played. Docs