Finding the song/track which is currently playing in Android

For my iAndroidRemote project, I had to find the currently playing song/track in Android phone and the ability to change to next/previous song/track.

After some digging, I found that there is no documented way to do it. It depends on an undocumented way which may not work well in all Android phones.

I found an undocumented way which works for HTC phones and some other stock Android phones that are using the default music player.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Copying the aidl files

Any Android app, can play music by using the com.android.music.MediaPlaybackService class. In order to find the currently playing song, we need to create a Service which will interact with this class

In order to do that, we need to copy the IMediaPlaybackService.aidl file from the source code of the default music app. For HTC phones we need to copy the IMediaPlaybackService.aidl file inside the com.htc.music package and for other Android phones we have to copy it from the com.android.music package.

Create the following packages to your android project.

  • com.htc.music
  • com.android.music

Copy the com.htc.music.IMediaPlaybackService.aidl and com.android.music.IMediaPlaybackService.aidl files to the newly created packages.

Creating the ServiceConnection class

The next step is to create the ServiceConnection class which will allow us to interact with the MediaPlaybackService class.

Copy the following code and create the MediaPlayerServiceConnection inside your activity as an inner class.

We are using the boolean isHtc to determine if we are on an HTC phone. We would be using this flag before invoking the methods on the ServiceConnection class.

Getting the current song

Now we can call the getTrackName() method on the corresponding object and we can get the track information.

Playing next song

To play the next song in the track we have to just call the next() method on the corresponding object

Playing Previous song

To play the previous song in the album we have to just call the prev() method on the correct object

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Related posts

Tags: , ,

16 Comments so far

Follow up comments through RSS Feed | Post a comment

  • Jay says:

    I am trying to implement MediplayerSerivceConnection when i use your class i get following errors.

    1) The method onServiceConnected(ComponentName, IBinder) of type MediaPlayerServiceConnection must override a superclass method
    2) isHtc cannot be resolved to a variable
    3) The method onServiceDisconnected(ComponentName) of type MediaPlayerServiceConnection must override a superclass method

    I get error no. 1 and 3 always no matter how i try to implement using different way too 🙁

  • Anonymous says:

    Maan, have you noticed that the links are pointing to java files, while they should point to aidl files?
    Thx anyway 😉

  • Bhupendra says:

    Hii

    Next and previous function not work

    Please give me solution

  • Weeber says:

    I’m using this with the stock Music app on android 2.1 and even though I can bind to the service the track name is returned as an empty string and the artist name as null. Why is this happening? Also, is there any other (more reliable) way to get basic audio information from any media player? I’m seeing that the use of this service interface is far from being standard 🙁

  • Siddarth Murthy says:

    The getTrackName is only re-starting the currently playing track. It does not display the Artist, Trackname or file name in the TextView.

  • Siddarth Murthy says:

    The above issue is based on the code at Github for ‘AdjustVolume.java’. Also can you shed some light on how the IMediaPlaybackService.java file is generated (in Gen folder). The rest of the features are working well :). Thanks in advance.

    • Sudar says:

      The Android Eclipse Plugin generates the file using the .aidl file (I guess so, it’s been 2 years since I wrote it).

      Which phone you are trying this code in?

      • Siddarth Murthy says:

        The phone OS is 2.3.3. I have also tried it on emulators for 2.2, 2.3.3, 4.0 and 4.1.

        • Siddarth Murthy says:

          I have managed to get the app working on a 2.2 emulator using getAlbumName() to return the track name of all things… wonder why getTrackName() returns a blank result???

          The same app though does not work on any OS above 2.2 (incl. 2.3.3, 4.0 & 4.1). I think there is an issue with binding of the service using the existing IMediaPlaybackService.java file that is generated using the AIDL of the same name. Is there a way to generate the interface IMediaPlaybackService.java file that is compatible with OS 2.3.3 or 4.0 or 4.1?

          • Sudar says:

            Sorry, as I siad before. I haven’t worked on it for more than two years. So I don’t know how to make it compatible with Android os > 2.2.

            May be when I get some free time, will explore it a bit more.

  • I have tried ‘Finding the song/track which is currently playing in Android’ and it is working successfully on my HTC DESIRE 500.But when i try to get the details of song from Google Play Music Player null pointer exception coming for all details.May i know what is the reason..?

  • Mansi Mehta says:

    Hello…Can any1 help me to build equalizer such as media player.I have created equalizer but it is not working as i thought. I want to make such when song bits are high it goes up and when it low its go down.

1 Tweetbacks so far

Leave a Reply

Your email address will not be published. Required fields are marked *