[Android] Is there a way to get a file path for music that's playing in another app?

Asked 2 years ago, Updated 2 years ago, 30 views

It's been changed from marshmallows to authority. We can't use the existing application that has been suspended, so we're making it ourselves. I want to get music information from the Sony music app through the extension provided by Sony, but the example of the manufacturer uses getIntent(). But if you flip the track, you can't bring the next song, but you keep bringing the first song crying Is there a way?

android

2022-09-22 21:51

1 Answers

When a sound source is played in a music app, the broadcast receiver can receive information about the sound source that is currently being played (in other apps). Querying your phone's media DB using the information from the sound source delivered through the broadcast receiver allows you to get the file path of the sound source you are currently playing.

In more detail, Intent, which is passed to the broadcast receiver, contains the following information:

action: com.android.music.playstatechanged
[duration=287124]
[playstate=false]
[woodstock=false]
[currentContainerName=Stop the Clocks]
[artist=Oasis]
[domain=0]
[currentSongLoaded=true]
[preparing=false]
[isPodcast=false]
[rating=0]
[albumId=920369031]
[songId=1303]
[currentContainerTypeValue=7]
[currentContainerId=920369031]
[playing=false]
[streaming=false]
[inErrorState=false]
[currentContainerExtData=null]
[album=Stop the Clocks]
[local=true]
[track=Dont Look Back in Anger]
[videoId=null]
[position=9394]
[currentContainerExtId=null]
[videoThumbnailUrl=null]
[isDoubleclickAd=false]
[supportsRating=true]
[ListSize=1]
[previewPlayType=-1]
[trackMetajamId=null]
[isSoundAd=false]
[isSkipLimitReached=false]
[ListPosition=0]

For your information, the amount of information you have depends on the music app. The above result is the result of checking with the Google Music app of Nexus 6P.

When I tested it on some music apps, track in the above information means the title. Using this track information, you can query the media DB and obtain the information contained in the DATA column to know the file path of the sound source. (There are no exceptions, so please refer to it.)

Cursor cursor = null;
try {
    cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        null,
        MediaStore.Audio.Media.TITLE_KEY + "=?",
        new String[] {MediaStore.Audio.keyFor(intent.getExtras().getString("track"))},
        null);
    if (cursor != null) {
        List<String> paths = new ArrayList<>();
        while (cursor.moveToNext()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            paths.add(path);
        }
    }
} } finally {
    if (cursor != null) {
        cursor.close();
    }
}

After playing a song in Google Music, you can use the above code to get the path of the song being played as follows.

[/storage/emulated/0/KakaoTalkDownload/oasis_Dont_Look_Back_in_Anger.mp3]

Additionally, the actions of the intents delivered by the music apps may not work because they are different. Please refer to the following article.


2022-09-22 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.