34

I am implementing an application related to getting current music track information.

I am using the following code to get that:

public class CurrentMusicTrackInfoActivity extends Activity {

    public static final String SERVICECMD = "com.android.music.musicservicecommand";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter iF = new IntentFilter();
        iF.addAction("com.android.music.metachanged");
        iF.addAction("com.android.music.playstatechanged");
        iF.addAction("com.android.music.playbackcomplete");
        iF.addAction("com.android.music.queuechanged");

        registerReceiver(mReceiver, iF);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String cmd = intent.getStringExtra("command");
            Log.v("tag ", action + " / " + cmd);
            String artist = intent.getStringExtra("artist");
            String album = intent.getStringExtra("album");
            String track = intent.getStringExtra("track");
            Log.v("tag", artist + ":" + album + ":" + track);
            Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show();
        }
    };

}

It is working fine for some mobiles only. I want to implement code to get current music track info in all Android devices (e.g. HTC and Samsung devices).

Is there any way of doing this?

Braiam
  • 1
  • 11
  • 47
  • 78
kiran
  • 3,244
  • 7
  • 34
  • 57
  • 1
    i have a issue with this on ICS. I am making a media player widget for ICS. As soon as i launch my first widget & thr is some music playing in native player, i get metachanged intent with the correct song details & in a few milliseconds, i get playstate changed intent, with some other song name. Any idea how to resolve this ? – AndroidGuy May 24 '12 at 10:40
  • provide sample to get carrect playing song details – kiran May 24 '12 at 10:43
  • Sorry, but i didnt get u kiran, i am using the same code u mentioned above. My onReceive is called twice, once for com.android.music.metachanged & immediately for com.android.music.playstatechanged, but the track i am getting from the both intents are different. MetaChanged gives correct name, but Playstate change gives a different name. This happens only for initial launch. How do i bypass Playstate change for the first launch. – AndroidGuy May 24 '12 at 11:24
  • If I have third party Music player and I am playing some song from that player, do I get those intent like metachanged? please help – Sagar May 31 '12 at 06:33
  • @kiran, have you considered the possibility that since devices seem to be handling it differently, that you will need to check for the type of device and implement a different method depending on the device they are using? Which type of devices does this work on? – gkiar Jun 27 '12 at 13:51

3 Answers3

47

Check this this is what am used in my app working well

    iF.addAction("com.android.music.metachanged");

    iF.addAction("com.htc.music.metachanged");

    iF.addAction("fm.last.android.metachanged");
    iF.addAction("com.sec.android.app.music.metachanged");
    iF.addAction("com.nullsoft.winamp.metachanged");
    iF.addAction("com.amazon.mp3.metachanged");     
    iF.addAction("com.miui.player.metachanged");        
    iF.addAction("com.real.IMP.metachanged");
    iF.addAction("com.sonyericsson.music.metachanged");
    iF.addAction("com.rdio.android.metachanged");
    iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
    iF.addAction("com.andrew.apollo.metachanged");
hderanga
  • 1,269
  • 12
  • 12
  • it works :) thanks. Plus, when I tested with my "Samsung Galaxy Note5", "com.android.music.metachanged" is called. – iroiroys Sep 11 '17 at 14:26
7

This might help. I use this to get metadata on audio files in one of my apps:

String scheme = mAudioUri.getScheme();
String title = "";
String artist = "";
if(scheme.equals("content")) {
    String[] proj = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST};
    Cursor cursor = this.getContentResolver().query(mAudioUri, proj, null, null, null);
    if(cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        if(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE) != -1) {  
            title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        }
    }
}

There are lots of columns in the MediaStore.Audio.Media class which you can use to find what you're looking for I think.

JMRboosties
  • 15,500
  • 20
  • 76
  • 116
  • Does the above code captures the Music details from third party music players or only Android's default music player? – tamil Sep 04 '13 at 10:18
  • It captures music details from a standard media file, I think its based on the ID3 tags rather than whatever program is playing the file. – JMRboosties Sep 04 '13 at 17:44
  • Fancy assisting me with this? – Broak Nov 20 '13 at 16:38
  • 1
    what is mAudioUri and how would I go about getting it – BionicSheep Aug 03 '14 at 15:19
  • It is the Uri of the audio file in question. How are you getting the audio you want to get id3 tags for? – JMRboosties Aug 04 '14 at 16:44
  • @JMRboosties, but your code does not provide info about the currently playing music instead it always gets the first item's info due to cursor.moveToFirst();. Am I right? – Co Koder Aug 07 '14 at 21:57
  • @CoKoder this requires you have the Uri for the current playing track. I think a combination of the answer below mine and my own is the "complete" solution here. You have an intent filter looking to get a message to your receiver whenever a track begins to play, and in the `onReceive` method, `intent.getData()` should return the Uri of the track now playing. Using that Uri, you make a query to the content resolver like I demonstrated above and that will give you all the data for the track. – JMRboosties Aug 08 '14 at 18:27
  • @JMRboosties, I thought your solution can work for all Android devices, but you still rely on "Uri of the track now playing" which is obtained from the onRecive(). It can work for Android's Music player but it may not work for other app like Amazon MP3. So, the question is how to get "Uri of the track now playing" that work in all Android devices (e.g. HTC and Samsung devices)? – Co Koder Aug 08 '14 at 21:02
4

Amazon MP3 (Build 2.8.1) seems to be prefixing the extras with com.amazon.mp3 so for com.amazon.mp3.metachanged I used:

    String artist = "";
    String track = "";
    if (action.equals("com.amazon.mp3.metachanged")) {
        artist = intent.getStringExtra("com.amazon.mp3.artist");
        track = intent.getStringExtra("com.amazon.mp3.track");
    } else {
        artist = intent.getStringExtra("artist");
        track = intent.getStringExtra("track");

    }

This was handy for checking the extra names:

    Set<String> extrasList = intent.getExtras().keySet();

    for (String str : extrasList) {
        Log.d(TAG, str);
    }
chughes
  • 81
  • 5