Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Get current playing Media Info using Broadcast in Android

I have looked Audio Manager in doc. I want to get notify whenever any audio/video (Media) will be play in device. So is there any action of broadcast Receiver to get the history of media ? I guess, i can get it from notification changes using accessibility. But i dont want to use accessibility as i want to track all the media which will be play in any default media player or any installed software in device. Is there any way to do it?

I have tried below code for getting info about media track. but it works only for google play music. How to get track info from all the default/installed media player.

public class MainActivity extends Activity {

    public static final String SERVICECMD = "com.android.music.musicservicecommand";
    public static final String CMDNAME = "command";
    public static final String CMDTOGGLEPAUSE = "togglepause";
    public static final String CMDSTOP = "stop";
    public static final String CMDPAUSE = "pause";
    public static final String CMDPREVIOUS = "previous";
    public static final String CMDNEXT = "next";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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.d("mIntentReceiver.onReceive:  ", action + " / " + cmd);
            String artist = intent.getStringExtra("artist");
            String album = intent.getStringExtra("album");
            String track = intent.getStringExtra("track");
            Log.d("Music","artist: "+artist+"\n album: "+album+"\n track: "+track);
        }
    };

Any help would be appreciated!!

Upvotes: 0

Views: 3437

Answers (1)

Techfist
Techfist

Reputation: 4344

To answer, No there is absolutely no broadcast for a event song being played in android.

Although, Audio Manager does have an API which tells you if any thing is being played in device

AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
 {
     // Something is being played.
 }

http://developer.android.com/reference/android/media/AudioManager.html#isMusicActive%28%29

Apart than this, you can also use provider if exposed by various player e.g Google play to find out which song was played recently, but it will only work if there provider is exposed.

Upvotes: 2

Related Questions