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?
Related
Steps to reproduce the problem:
Start a application with a in-app WebView.
Call webview.loadUrl("http://n-g.nxtomo.hk/CYvsCP/app.html?nocache=true");
Click the web page once time and start to play a background music.
Press HOME button that the app will go to background, but the music keep playing.
Expected the WebView being pause when the activity go to background.
I've tried to call:
webview.onResume();
webview.onPause();
or
Class.forName("android.webkit.WebView").getMethod("onResume", (Class[]) null).invoke(webview, (Object[]) null);
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webview, (Object[]) null);
or
wv.resumeTimers();
wv.pauseTimers();
Both ways are not working (means no effect and keep playing sound in background) for Android 4.4.2 and 5.0 , I just have two devices.
And I found a suggestion is to remove the MediaPlayer's callback that force stop the sound play.
#Override
protected void onPause() {
super.onPause();
((AudioManager) getSystemService(
Context.AUDIO_SERVICE)).requestAudioFocus(
null,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
It's work for stop the sound.
BUT! When I resume the app that the sound will not auto play continually. So, I'm looking for a officially way to fix this problem. :(
Refer to
[Issue 532359: Cannot pause HTML5 sound on Android in-app WebView when application in background. - Sep 16, 2015] https://code.google.com/p/chromium/issues/detail?id=532359
[Issue 10282: Public API for WebView.onPause and WebView.onResume - Aug 7, 2010] https://code.google.com/p/android/issues/detail?id=10282
I have the exact same problem and looking for an official fix.
Only workaround I have found was to
loadUrl("");
but it's far from a good fix. As soon as you multitask the page reloads, this can lead to a lot of problems if you need the webview session to be kept alive or persistent.
Reply from Google:
https://code.google.com/p/chromium/issues/detail?id=532359#c5
Hm, if you only want to change the Android app, you're probably out of
luck with what you can do there.
If you can change the page, you may look into page visibility API to
pause/resume playback (Tim may know if it's enabled for WebView or
not).
But ultimately, since it's WebView, your app can communicate with the
page and cause it to play/pause playback in the app's activity
onPause/onResume.
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
I am using this tutorial and the RemoteController to control the currently playing music player:
http://forum.xda-developers.com/showthread.php?t=2579415
Is there a way to get the application/package of the currently playing music application? I want to be able to open it.
I'm sure it is possible somehow as Aviate manages to do it, but afaict this class does not provide that information:
https://developer.android.com/reference/android/media/RemoteController.OnClientUpdateListener.html
Maybe there is some unrelated way of getting the currently playing music player...
Thanks :)
I worked it out... had to use reflection on the RemoteController though.
Method method = mRemoteController.getClass().getDeclaredMethod("getRemoteControlClientPackageName");
if (method != null) {
return (String) method.invoke(mRemoteController);
}
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);
}
Is there a simple way to tell the default media player to change track back or forward?
I want the ability to send commands to the system media player (Music) to change track back and forward from within my app.
Is there a simple way? Code examples or descriptive explanation please, I have not developed for Android before.
Update: Is it just the HTC Music that isn't part of the SDK or even the stock one? Either player would be fine if I could manage way to change tracks.
The HTC Lock screen has some method of changing tracks in the music player. Is it possible I could get hold of this and use it?
Baksmali?
I want the ability to send commands to
the system media player (Music) to
change track back and forward from
within my app.
The Music application is not part of the Android SDK, so there are no documented and supported Intents for moving from track to track.
Sorry!
by ACTION_MEDIA_BUTTON using intent ??
http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON
Intent media_intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
media_intent.setPackage(DEFAULTPLAYER);
synchronized (this) {
media_intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keycode));
mContext.sendOrderedBroadcast(media_intent, null);
media_intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, keycode));
mContext.sendOrderedBroadcast(media_intent, null);
}
however DEFAULTPLAYER needs to be assigned...
:S :S :S