'Get details of app playing audio

I'm working on an app which needs to resume audio playback after the device was rebooted.

I found out that simply sending the media play button may not start the same app the user was using. (I have spotify running, yet sending the play button after a reboot opens Google Music)

Is there a way to get details about the app playing media (before reboot) ?

I need something like app name/activity name/package name .etc. (So I can launch the same app before sending play button)



Solution 1:[1]

need permission

<uses-permission android:name="android.permission.READ_LOGS" />
new Thread(new Runnable() {
    @Override
    public void run() {
        Process mLogcatProc = null;
        BufferedReader reader = null;
        try {
            //??logcat????
            mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat","MediaFocusControl:I *:S" });
            reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));

            String line;

            while ((line = reader.readLine()) != null) {

                if (line.contains("MediaFocusControl: requestAudioFocus()")) {
                    String[] strArr = line.split(" ");
                    for (String str:
                         strArr) {
                        if (str.contains("callingPack")){
                            System.out.println(str);
                            Test.packageName = str.split("=")[1];
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).start();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58