Search code examples
androidandroid-activityserviceandroid-mediaplayerinternet-radio

Android - Check which radio I'm playing


I have 2 activities:

Activity A - Listview/Adapter

Activity B - Radio

In activity A, I choose a radio and B plays that radio (service).

But everytime that I choose another radio on the list, activity B is instanciated another time and the radio stops and plays again.

Situation example:

# 1 - I'm playing Radio X, I choose X on the list
# 2 - A new instance is created (service is in onCreate() of Activity B)
# 3 - Radio X playing (play() is in onStart() of service)
# 4 - I go back to the list
# 5 - I want to play Radio Y
# 6 - A new instance is created (service is in onCreate() of Activity B)
# 7 - Radio Y playing (play() is in onStart() of service)
# * In onCreate() of service isn't doing nothing

Everything is fine, but what happens if I go back to the list and choose the same radio, for example:

# 1 - Radio Y playing
# 2 - I go back to the list
# 3 - I wanna go to Radio Y again
# 4 - A new instance is created (service is in onCreate() of Activity B) (I don't want this)
# 5 - Radio Y stops and plays again (I don't want this)

I'd like to have a way to check if the radio is playing is the same radio that I wanna go, and don't create a new instance and don't stop and play the same radio again.

Edit:

ListView

if (item == "Radio 1"){
       Intent intent = new Intent(getBaseContext(), Radio.class);
       intent.putExtra("radio", "http://test1.com");
       this.startActivity(intent);
} else if (item == "Radio 2"){
       Intent intent = new Intent(getBaseContext(), Radio.class);
       intent.putExtra("radio", "http://test2.com");
       this.startActivity(intent);
}

Radio.java

@Override
public void onCreate(Bundle icicle) {
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    super.onCreate(icicle);
    setContentView(R.layout.main);

    Intent music = new Intent(getApplicationContext(), Service.class);
    music.putExtra("url", this.getIntent().getStringExtra("radio"));
    startService(music);
}

Service.java

@Override
public void onStart(Intent intent, int startid) {
    Multiplayer m = new MultiPlayer();
    m.playAsync(intent.getExtras().getString("url"));
}

Solution

  • To clarify my answer a bit:

    You have 2 options (depending on :

    Store the playing url in the Service (in onStart) and compare it with the new url being send.

    private String mCurrentUrl;
    
    @Override
    public void onStart(Intent intent, int startid) {    
        String newUrl = intent.getExtras().getString("url");
    
        if ((newUrl.equals(mCurrentUrl)) {
            mCurrentUrl = newUrl;
            Multiplayer m = new MultiPlayer();  
            m.playAsync(mCurrentUrl );
        }
    }
    

    or:

    Define a interface of the Service (AIDL) which retrieves the current radio channel. If you let the activity bind to it you can call this method to retrieve the current channel. Note: that you have to start the Service using startService and then bind to it directly. (Else you Service is killed after your Activity is killed)