Search code examples
androidandroid-audiomanager

How to apply to a standard audio player?


I need to be when you press the buttons in the usual android I could switch to music in my standard audio player for android while the music is playing. How can you interact with a standard audio player for android?


Solution

  • Music player is not standard thing on Android and vendors can come with different app than others (i.e. Sony is good exapmple). Controlling media app can be doable but final effect depends on what app is really installed (and used, as user can have more than one). You may be able to control some, but other may remain immune to your attempts. You can try this code - will work for Google Music for example):

    public static final String SERVICECMD = "com.android.music.musicservicecommand";
    public static final String CMDNAME = "command";
    public static final String CMDSTOP = "stop";
    
    AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
    if(mAudioManager.isMusicActive()) {
        Intent i = new Intent(SERVICECMD);
        i.putExtra(CMDNAME , CMDSTOP );
        YourApplicationClass.this.sendBroadcast(i);
    }
    

    other commands are

    public static final String CMDTOGGLEPAUSE = "togglepause";
    public static final String CMDPAUSE = "pause";
    public static final String CMDPREVIOUS = "previous";
    public static final String CMDNEXT = "next";
    

    Commands are taken from android/music/MediaPlaybackService.java