Search code examples
androideclipsemedia-player

Android, Can a Service control buttons?


I have an application from where a play a radio stream using a service for the media player, that works the next way: as it starts it calls the service and the radio starts playing, in the main view I have a play and stop button, so when the app starts since the radio starts right away I have set the play button GONE and the Stop button VISIBLE. so if I press the stop button it stops the service and makes the play button VISIBLE and the Stop button GONE, and the other way around. And so far if the service detects an error it send a Toast plus an update to my notification bar, but the Stop button continues to be VISIBLE and the Play button is still GONE, so my question is how can I make my service at detection of the error make the buttons change? so I could have the Play button VISIBLE and the Stop GONE to try again. this is how I have code the buttons in OnCreate in my main Activity:

 Button playBtn = (Button) findViewById(R.id.buttonPlay);
    playBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

             View b = findViewById(R.id.buttonPlay);
                b.setVisibility(View.GONE);

                View c = findViewById(R.id.buttonStopPlay);
                c.setVisibility(View.VISIBLE);

                    TextView textRadioName = (TextView)   findViewById(R.id.textView);
                    textRadioName.setText("You are listening Radio!");
                    startService(new Intent("PLAY"));
                    start();

    }});

    Button stopBtn = (Button) findViewById(R.id.buttonStopPlay);
    stopBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        View b = findViewById(R.id.buttonStopPlay);
        b.setVisibility(View.GONE);

        View c = findViewById(R.id.buttonPlay);
        c.setVisibility(View.VISIBLE);

            TextView textRadioName = (TextView) findViewById(R.id.textView);
        textRadioName.setText("Listen Radio Here");

        pause();
    }});

and this is how I code the error in my MediaPlayer Service:

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Toast.makeText(this, "Radio not connected!...." , Toast.LENGTH_LONG) .show();
    StreamError = true;
    updateNotification("Radio not connected!");
    return false;
}

I like the way it works but it looks odd having my stop button VISIBLE and not the play one, so far if I press the stop it makes my play appear and then I can try again! but is not how an application should work right? Thanks for your time. I hope someone can show me the way.


Solution

  • You can bind to your service, which will return an IBinder that you have to implement. Inside you IBinder you can put a function like:

    public void setPlayer(IPlayer player);
    

    Where your IPlayer would have the set of functionality you want to call from the service. This is where you could put the code to hide the stop button, show the play button, or anything else you want to do from the Service.

    As requested, a link to the sample in the docs: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample