0

So I am trying to create a basic music app on android which displays the songs of the device in a list view and when one taps a particular song the app should play it on a different activity.

So far I've been able to extract the songs from sd card, create the second activity for playing the song (which is provided by a play/pause button and a seekbar).

But how would I pass the song tapped by the user in the list view to the second activity in order to play it.

3 Answers3

0

You can use either a singleton in this case so you can create a SongManager which will be your singleton which store your song and playable in the second activity or you can show the listview in a fragment so your SongManager is NOT a singleton but just a simple object managed by the activity which shows the fragment, when you want to play the song you can open an another fragment which will ask the song to play to the activity.

So

ACTIVITY (create the object `SongManager`)
   FRAGMENT A (ListView)
   FRAGMENT B (Play)
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
0

I would suggest just passing the filename as a String, i.e.

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("SongKey", songFileName);
startActivity(intent);
Isaac
  • 11
  • 1
  • 2
  • Which function on MediaPlayer should I use to play the song which I will pass through the Intent (by your recommended method). What I mean to say is that now I'm using the raw folder and using create(..) function of MediaPlayer class. Now that i'll be passing the song title then what function should I use to play that song – Prakhar Singh Aug 10 '16 at 11:48
  • If you're using MediaPlayer, have a look at the reference: https://developer.android.com/reference/android/media/MediaPlayer.html especially setDataSource(). You can just put the absolute file path as the parameter of setDataSource(). But be sure to follow all the other steps for initializing MediaPlayer and playing. – Isaac Aug 12 '16 at 00:08
  • https://stackoverflow.com/questions/38880528/media-player-in-android-using-mediastore This is the problem I'm facing. Please have a look at it. I tried reading the resource you have provided bu wasn't quite able to understand it fully. – Prakhar Singh Aug 12 '16 at 10:45
  • Sorry, it's hard to know what's going wrong without seeing more code.. Your last questions was: "To put it simply how should I pass the same song selected in the previous intent to the provided activity so that it can be played." That basically sounds like the original question in this thread.. so your problem now is that the song is not playing? Do you see any errors/exceptions? Where is the song stored? Maybe your app needs READ_EXTERNAL_STORAGE permission? – Isaac Aug 14 '16 at 04:32
  • No errors or exception. Yes I've mentioned the permission too. The question is how to pass a song from one activity and playing it on another activity. – Prakhar Singh Aug 16 '16 at 15:12
0

you need to parsing mp3 path to new activity, so new activity can play your song

              Intent intent = new Intent(getActivity(),Play.class);
                    intent.putExtra("title", titleX);
                    intent.putExtra("path", mp3path);
                    startActivity(intent);

then in play.class activity

        title = getIntent().getStringExtra("title");
        location = getIntent().getStringExtra("path");
        //other function
        mediaPlayer.setDataSource(location);
        mediaPlayer.prepare();
Muklas
  • 557
  • 7
  • 11
  • But how do I get the path. I am struggling with getExternalStorageDirecotry() method to get the path for the song. Is it the right approach? – Prakhar Singh Sep 20 '16 at 13:02
  • if you can extract the songs from sd card, so you can get path from it.. String fullpath = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA)); – Muklas Sep 21 '16 at 14:45
  • and then do I have to pass this fullpath variable to another Intent through a Bundle. `String filePath = "somepath/somefile.mp3"; File file = new File(filePath); FileInputStream inputStream = new FileInputStream(file); mediaPlayer.setDataSource(inputStream.getFD()); inputStream.close();` and then use this code – Prakhar Singh Sep 21 '16 at 16:52