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

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?

Related

How to determine the playing state and pause state in default media player?

I am using isMusicActive function to detect the media player is running (note that this is default media app, such as com.sec.android.app.music). Now, I want to determine the pause state. Is it possible to get that state? Thank all
AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
if (mAudioManager.isMusicActive())

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.

Muting SpeechRecognizer's beep sound

I'm using SpeechRecognizer API for my app, and everytime it starts, it plays "beep" sound.
I'd like to know how to mute it, So I could implement one of my own.
Thanks.
If you are using a button to activate and deactivate the recognizer you can mute sound onclick.
This doesnt work fantastically if you have it listening constantly, however for button clicks it should be fine :)
private AudioManager manager;
manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (isChecked)
{
manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
speech.startListening(recognizerIntent);
}
else
{
manager.setStreamMute(AudioManager.STREAM_MUSIC, false);
speech.stopListening();
speech.cancel();
}
Hope this helps (sorry if this is abit of a necrothread)

How to control currently playing music player in 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.

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