1

I'm making a simple mediaplayer app that downloads a song from a predetermined URL then let's the user play, pause and stop the song, among other things. My only problem right now is that if the user tries to play the song before they download it the application force closes.

My first instinct is to create a global bool flag, set it to true when my download service downloads the file, and then use that flag to error check before I let the play.onclick button run the play service.

So for example:

play.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (downloadFlag == true){
                Intent intent = new Intent(getApplicationContext(), PlayService.class);
                intent.putExtra("key", 0);
                intent.putExtra("nkey", 0);
                startService(intent);   
                String artistFirst = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('_'));
                String artistLast = url.substring(url.lastIndexOf('_')+1, url.lastIndexOf('-'));
                String song = url.substring(url.lastIndexOf('-')+1, url.lastIndexOf('.'));
                tv_artist.setText("Artist: "+artistFirst+" "+artistLast);
                tv_song.setText("Song: "+song);
                final ImageView album = (ImageView) findViewById(R.id.imageView1);
                album.setImageResource(R.drawable.album);
            }
        }
    });

Is this a good way to approach this problem or should I pursue a more elegant solution?

Edit::Solution found

At start of the application I disable the play button and then enable it in my broadcastreceiver (which catches when the download finishes)

 Button play = (Button) ((Activity) context).findViewById(R.id.play);
    play.setEnabled(true);
Connor Black
  • 6,921
  • 12
  • 39
  • 70

1 Answers1

0

Add the listener after the song is downloaded (in the same place you would set your flag to true). Or disable the button and enable it when the song is downloaded. That way you don't have to use a flag.

play.setEnabled(false)
//...
play.setEnabled(true)
User
  • 31,811
  • 40
  • 131
  • 232
  • How do you disable/enable buttons from a service and activity? – Connor Black Jun 16 '12 at 23:04
  • My problem is I have a Broadcastreceiver to catch when the download is completed and I'm trying to call Button play = (Button) findViewById(R.id.play); play.setEnabled(false); but findviewbyid doesn't work in a boradcast receiver – Connor Black Jun 16 '12 at 23:10
  • Just make that the Broadcast receiver notifies in some way your activity (depending of how exactly you implemented it), and then enable / disable the button from inside the activity. There's a lot of info about this, like here: http://stackoverflow.com/questions/6661801/how-can-i-notify-running-activity-from-broadcast-receiver – User Jun 16 '12 at 23:15
  • You can modify the button from the broadcast receiver – Connor Black Jun 16 '12 at 23:26
  • Uhm, well, if you define the broadcast receiver as an inner class of the activity, yes. But you don't give information about how it is implemented... – User Jun 16 '12 at 23:29