Control the default music player of android or any other music player
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";
How to control currently playing music player in android?
Have you tried this -
i.putExtra("command", "pause");
EDIT:
Alternate Solution, List programmatically all music apps using PackageManager , Intent, ResolveInfo etc. and than send command to all apps in the list.
Android, any way to controll the default music player
I haven't found anything in the API
but I want the same behavior that is
available in any headset so there must
be some API right?
No. The media player app responds to headset events.
Anyone who has an idea on how to
control the default media app?
There is no published API for the default media app AFAIK, and it is not part of the Android SDK.
Stop music of from Other APP
You will have to implement Mediaplayer's AudioFocus
. To do this you need to get an instance of the AudioManager
. Once you have the instance you can then use requestAudioFocus
.
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.d("AudioFocus", "Audio focus received");
return true;
} else {
Log.d("AudioFocus", "Audio focus NOT received");
return false;
}
}
AudioFocus
is assigned to each application that requests it. When your app receives focus it can pass an AudioManager.OnAudioFocusChangeListener
which provides callbacks for when an focus change happens.
If app gains audio focus, and another app requests audio focus, the focus will be given to the other app. Android will notify your app via an OnAudioFocusChangeListener
so that your app can respond to the change. To receive focus events, you need to pass an instance of AudioManager.OnAudioFocusChangeListener
like this.
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
// Lower the volume while ducking.
mediaPlayer.setVolume(0.2f, 0.2f);
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
pause();
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
stop();
ComponentName component =new ComponentName(AudioPlayerActivity.this,MediaControlReceiver.class);
am.unregisterMediaButtonEventReceiver(component);
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
// Return the volume to normal and resume if paused.
mediaPlayer.setVolume(1f, 1f);
mediaPlayer.start();
break;
default: break;
}
}
};
Reference Documentation: https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html
Related Topics
Making Voice Calls from Android Phone Connected to Linux
Convert File: Uri to File in Android
How to Change Styling of Textinput Placeholder in React Native
How to Display Date Picker for Android With Only Month and Year Fields
I Get Fragment Not Attached to a Context. What Context Need to Use
App Restarts Rather Than Resumes
How to Prevent User Go Back to Protected Page After Logout
How to Know Which Fragment the User Is Viewing
Android: How to Scale a Bitmap to Fit the Screen Size Using Canvas/Draw Bitmap
Possible Unhandled Promise Rejection (Id:0) Error: Network Error in React Native
Java.Io.Filenotfoundexception Open Failed Eacces (Permission Denied) on Device
Pdf Not Open in Webview from Url
Remove Old and Add New Git in Android Studio
How to Remove the Black Bar Appearing At the Bottom
How to Create a Folder in Android External Storage Directory
Is Quitting an Application Frowned Upon