Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control the default music player of android or any other music player

Tags:

How to control the default music player of android or any other player? By controlling i mean pause, play, next etc. Do i have to bind the service? I have tried to use the IMediaPlaybackService but it is not working. There is certainly a way out as i have seen apps in the android market which controls the music player. Any idea?

like image 910
Naddy Avatar asked Sep 14 '13 09:09

Naddy


People also ask

How do I change my default music player on Android?

You can change default app for music player by go to Settings -> Apps and clicking the app and click "Set Default" .

How can I change my default music player?

To find the music settings for Google Assistant and change your defaults, open the Google app on your phone and tap the More tab at the bottom. There, select Settings. On the resulting screen, tap Google Assistant to open its settings, then scroll down and tap the Music entry.

What is the default Android music player?

It was clear from the start that YouTube Music was intended as a direct replacement for Google Play Music, and Google just announced that YouTube Music will be the default, preinstalled music player for new Android 10 and Android 9 devices.

What is your default music player app?

Your Android phone should automatically play music using Spotify whenever you ask Google Assistant to play a song, artist, or album.


2 Answers

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  if(mAudioManager.isMusicActive()) {     Intent i = new Intent(SERVICECMD);     i.putExtra(CMDNAME , CMDSTOP );     YourApplicationClass.this.sendBroadcast(i); } 

you can by getting the audiomanager then sending commands to it.

these are the commands.

 public static final String CMDTOGGLEPAUSE = "togglepause";  public static final String CMDPAUSE = "pause";  public static final String CMDPREVIOUS = "previous";  public static final String CMDNEXT = "next";  public static final String SERVICECMD = "com.android.music.musicservicecommand";  public static final String CMDNAME = "command";  public static final String CMDSTOP = "stop"; 
like image 160
Ahmed Ekri Avatar answered Sep 26 '22 20:09

Ahmed Ekri


Problem with sending Intent for broadcast receiver is that, user has to open Music Player at least once, also, It does not work for many third party music players.

The code given below works on all music players.

long eventtime = SystemClock.uptimeMillis();  Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null);  Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); sendOrderedBroadcast(upIntent, null);  /*NEXT*/ Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN,   KeyEvent.KEYCODE_MEDIA_NEXT, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null);  /*PREVIOUS*/ Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null); 

I have tested this code on Sony, Htc and Samsung devices.

This code imitates play/pause button action from hands-free(ear phones).

like image 25
4 revs, 3 users 69% Avatar answered Sep 22 '22 20:09

4 revs, 3 users 69%