How to control currently playing music player in android? - android

I use this code to send actions like play/pause for music player.
// play / pause
i.putExtra("command", "togglepause");
context.sendBroadcast(i);
But it works only for default music player (phone's own music player). Is there any way to control other music players while they're playing?

From API level 19, new API dispatchMediaKeyEvent() is introduced.
Sends a simulated key event for a media button. To simulate a key press, you must first send a KeyEvent built with a ACTION_DOWN action, then another event with the ACTION_UP action.
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
am.dispatchMediaKeyEvent(downEvent);
upEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
am.dispatchMediaKeyEvent(upEvent);
This media event will be dispatched to the current running music player only, not to all players.
Thanks

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.

Related

MediaSession onMediaButtonEvent works for a few seconds then quits - Android

I've been trying to listen to media button intents on this Bluetooth button I bought off eBay for the past week and cannot figure out how to do it practically and reliably on my Oreo device. The first thing I tried was overriding onKeyDown in my main activity, which worked perfectly every time. However, I found it was impossible to listen to these calls once the application is minimized.
Next I tried a very hacky solution of reading the logcat line by line and checking if dispatched media key KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_NEXT was present in that line. It worked alright until I again exited the app, which then it would completely stop.
Finally I tried using a MediaSession and the onMediaButtonEvent method, which works perfectly for a few seconds to minutes after I start my activity then it just completely ceases to receive these intents.
Here's my code right now (I copied another answer on StackOverflow who included playing a dummy audio clip to try and get focus):
MediaSession ms = new MediaSession(getApplicationContext(), getPackageName());
ms.setActive(true);
ms.setCallback(new MediaSession.Callback() {
#Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyEvent.getKeyCode()) {
//do button specific stuff here
}
}
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
ms.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
at.play();
// a little sleep
at.stop();
at.release();
I'm just trying to figure out now, why it seems to work for a few seconds perfectly, then it just stops out of nowhere. I don't have any other media app like Spotify open or playing whatsoever, and if that was the issue, I've tried running the audio code every 5 seconds or so which still has no effect. The only thing that will reset it is fully restarting the app.
This code is in a service I have created. It only stops working if I am not in the app and simply press the home button to minimize it after a few seconds. I have a loop running to tell me that the service is still running, and it's seems it's running when it quits receiving the intents.
Any help would be appreciated.
I've tested this code, it seems the bluetooth button press is only listened to by your app if music (or silent sound) is currently playing. Otherwise the button press is not detected.

How to know if the media player is opening in background?

I am using Android 5.0 and 6.0. I want to check my default media player (calls from intent) is running/playing in background. Currently, I used the function
AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
if(mAudioManager.isMusicActive()){
Log.d("TAG","Running");
}
else{
Log.d("TAG","Not running");
}
However, it cannot know if the media player is in background but it is in stop/pause mode (looks like when you scroll down the notification bar and click the pause button in the media player). Do we have any function which can determine the media player is opened in background, but it is in stop/pause mode?

How to start a media player object in a activity, and then use that same object and another activity?

Example:
Activity 1:
main screen.
player = new media player()
player.start() //the sound began
now i have to equalize this same sound in another Activity...
Activity 2:
edition screen
the sound keeps playing and want to stop
example:
player.setVolume(0.0)
player.stop()
thank you
Declare player as public static in screen1
then you can access this media player in screen 2
like screen1
:
public static MediaPlayer player;
player=new MediaPlayer();
=================
===========
write your code
Screen 2 ::-
if you want to use media player in screen 2 use this code ::-
screen1.player.start(); screen1.player.stop();
You must create Service. Service to host the MediaPlayer and have your Activities communicate with the Service to play and stop songs. Don't forget to call release on the MediaPlayer when you are done. Bind the activity to service
For the sample Equalizer sample. The sample is not integrated with Service it just a seperate unit.
Obtain the sessionid of the MediaPlayer and pass it to equalizer.
Usually when we are using MediaPlayer, as playing music in itself doesnt really require to have a graphical interface, we usually use a service because only the sound resulting by the playback is needed.
- Create the mediaplayer in a service
- Send somes request to the Service after binding to it, or even by sending broadcasts to it so that it can play, stop, pause, set volume whatever you want.

Android service timer

Can an audio service be set to a timer? Example would be for a sleep machine type app where the user clicks the time they want the sound to play and then the sound plays for that time. I was told that using a service to handle the audio was the proper way but I do not know how to get it to work with a timer for a user selected input
Short answer, yes it is possible.
I have done audio recording using a background service, have also done FTP downloads in a background service.
You would use a started service in this case, it will play audio using following:
http://developer.android.com/reference/android/media/MediaPlayer.html
You may pass a value from Activity to Service using intent extras, for example
Intent intService = new Intent(AndroidSignageActivity.this, SignageDownloadService.class);
intService.putExtra("TIMEVAL_MILLISEC", 1000);//
startService(intService);
In the service, you can extract the value and use it in onStartCommand method as following:
long lTimeout = 0;
if(intent.hasExtra("TIMEVAL_MILLISEC")){
lTimeout = intent.getLongExtra("TIMEVAL_MILLISEC", 100);
}

Adding mute button to application in android?

I my game application am using musics. I need mute and unmute button to put mob i silent mode.
Music runs in every activity like playing game and checking score.But mute button is added in menu activity.
I googled didn't get any exact result.
Use audio manager and set volume
AudioManager audioManager = (AudioManager) getSystemService(ctx.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
I'd choose to set the volume to 0 (mute) or 1 (unmute) with MediaPlayer.setVolume(float volumeLeft, float volumeRight). Also preserves the users volume settings. Trigger by Button is easily achieved with onClickListener().

Categories

Resources