Implement Now Playing in Music Player

491 Views Asked by At

I am trying to make a music player in android. I have two activities. MainActivity: updates the list of songs and displays them. PlaySongActivity: implements the basic features: play, pause, next, previous.

Now, I wish to switch between the two activities. When I press back button from the PlaySongActivity, I get back to the MainActivity. But I dont know what to do when I press the NowPlaying button. Here's what I am doing.

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updatePlayList();

    Button nowPlayingBtn = (Button) findViewById(R.id.nowPlayingBtn);
    nowPlayingBtn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            intent = new Intent(MainActivity.this, PlaySongActivity.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onListItemClick(ListView list, View v, int position, long id) {
    intent = new Intent(MainActivity.this, PlaySongActivity.class);
    intent.putExtra("position",position);
    intent.putExtra("songsList", songs);
    startActivity(intent);  
}
2

There are 2 best solutions below

0
On

It's not clear what the exact question is.

Please take a look at the Android samples (located in the SDK folder) on how to create a music player (you need a service, and a Notification).

You can't go from Activity A -> to Activity B without an Intent (you can, however go back using the back button from B to A).

Why don't you want to use Intents? Intents are the communication mechanism between Activities and Applications in Android, don't try to swim against the current.

To let Activity A know that something happened in Activity B, you can use the Listener/Observer pattern, but in this case, I suppose it would be better to implement your BroadcastReceiver to listen to your Service broadcasts and when the service says: "I'm Playing!" you can do your magic.

0
On

AFAIK it is not possible to go to a new Activity without an Intent. What you can do is use Fragments instead of Activities. The other option is to use the Application Context instead of the Activity Context to manage the music and be able to keep reproducing it through activities.