Is there a way we can check programmatically Which Music player is playing right now?
like Google Play, Samsung default Music Player, any 3rd party music player
Actually, we need to programmatically handle play/pause of music player. Google Play and Samsung Music works differently with code :
// Google Play do not play pause with this code
// it is using different package name i guess
CmdStop = "togglepause";
i = new Intent("com.android.music.musicservicecommand.togglepause");
i.putExtra(CmdName, CmdStop);
context.sendBroadcast(i);
Any help is appriciated
Thank you
you don't need a broadcast receiver for this - AudioManager is your friend:
AudioManager.isMusicActive() does the job you want, have a closer look here for details: AudioManager
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or do it not
}
stackoverflow answer
Related
I am new to community and just started with android development and I am trying to develop a music app for android.
In most of the media players for android, there is an feature that music is paused when you get a call or any other notification sound is played and music resumes when it ends. I am trying to achieve a similar behaviour.
I got a solution at here but I am not very clear about how to use it. Also source for Android Player is not available any more.
I have written
AudioManager.OnAudioFocusChangeListener focusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int i) {
Log.d("Focus Changed",""+i);
}
};
just to see behaviour of focus change but don't find a way to use it in my media player.
Can someone please guide on this?
I'll Play some notification alerts from my app.. At the time system should pause the music running in the device. For that i'm sending Broadcast event to pause the media.
Intent audioIntent = new Intent("com.android.music.musicservicecommand.pause");
MYActivity.sendBroadcast(audioIntent);
This code is only working and pausing the Google Play Music and Default Music Player.. But this code is not working for Poweramp and VLC.. Kindly post your solution..
You really should supply the code you have written for the broadcast, this is a very hard question to answer without your code... Something like this is what most people use:
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
YourApplicationClass.this.sendBroadcast(i);
I am not able to mute the incoming call ring tone and play my own media file using MediaPlayer class.
So far I have tried the following in my Activity that pops on top of incoming call screen, started using a BroadcastReceiver; Activity shows up but audio doesn't play:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_RING,true);
am.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
Then I start playing the audio file. The sound plays perfectly in normal mode, so I also set the audio mode to MODE_NORMAL using am.setMode method but no luck.
Is there any way around? I am currently mobile and so I could not post full code. If anyone has done this before, a block of code would be a great help. Moreover, I dug up the documentation for AudioManager class and found setRoute or similar method which looks good but it has been deprecated long ago so I didn't want to implement it.
Does this approach have different reaults when screen is on or off during an incoming call?
Can anyone please show me how and where can I modify the Phonegap source to play audio through the earpiece?
Try this code:
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
Also need to add new permission:
MODIFY_AUDIO_SETTINGS
Either put this code in your activity start (onCreate()) or you need to write a plugin to enable/disable it based on your requirement from the javascript.
Check this post for more
Android - Getting audio to play through earpiece
I am fairly new to Android app development and I need some direction.
I have written an app that plays mp3 files from the internet via the Android MediaPlayer either one at a time or from a playlist.
The user can play one mp3 at a time or queue up several mp3's, go to a playlist screen and hear each one after the other.
I have a progress bar, start, stop, pause, and continue buttons on the screen that plays a single mp3.
On the playlist screen there is no progress bar, but there are start, stop, pause, and continue buttons.
I want the following behavior but I am not sure how to implement it correctly:
when an mp3 is playing and an incoming phone call is received, the mp3 is paused; when the user hangs up, the mp3 is resumed automatically
when an mp3 is playing, and the user presses the phone's "home" button, the mp3 continues to play while the user is free to do other things (like check email for example);
Do I need to implement the media player as a service?
Do I need a separate thread to run the media player?
I am doing neither at the moment.
Is there a good tutorial on this?
I have tried the following tutorial in a separate app that implements the media player as a service and it seems to do most of what I want but I haven't been able to figure out how to incorporate a "pause" and "continue" button.
"ServicesDemo - Using Android Services": http://marakana.com/forums/android/examples/60.html
As a followup question, are there canned media players that can be purchased or available as a free download that already have this functionality that can be included in my app?
I'm not posting any code here yet as this is more a general question, but will as a followup.
Thanks in advance,
Dave
Yes, you need service for your media player, and how to handle phone calls read about AUDIO_FOCUS.
Try this Code To Stop and Resume Song in between calling
PhoneStateListener phoneStateListener=new PhoneStateListener()
{
#Override
public void onCallStateChanged(int state, String phoneNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING )
{
MP.Pause();
}
else if(state==TelephonyManager.CALL_STATE_OFFHOOK )
{
MP.Pause();
}else if (state==TelephonyManager.CALL_STATE_IDLE)
{
MP.Start();
}
}
};
TelephonyManager manger = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(manger!= null) {
manger.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}