I work on a mediaplayer app with different themes.
After changing themes in the SettingsActivity I recreate Settings- and MainActivity that the new theme will be shown.
MainActivity.getInstance().recreate();
SettingsActivity.getInstance().recreate();
After this recreation, my mediaplayer App continues playing.
getTransportControls().pause();
doesn't work.
Pause mediaplayer befor the activities are recreated, then
getTransportControls().pause();
works but that's not I want the player to do.
My MediaBrowserService is following this example.
https://code.tutsplus.com/tutorials/background-audio-in-android-with-mediasessioncompat--cms-27030
There is no bind between Service and mediaplayer.
1. Question: How can I continue playing after recreation?
2. Question: How can I start mediaplayer after recreation - when it was privously stopped before Activity recreation?
Can anyone help me with my problems.
Thanks
GGK
During the onDestroy() method, I had to generate a new Intent then stop the service:
Intent intent = new Intent(getApplicationContext(), AudioService.class);
stopService(intent);
Related
I made a Music Player that operates in two ways:
If started as a normal app, it continues playing what it was playing when last closed, and it should start in the background;
If an audio file is selected in a fileManager, it plays the song clicked in the foreground.
Since I want only one instance of the app running, so I added to the mainfest:
android:launchMode="singleTask"
What I did to accomplish this was to execute this onCreate:
uri = getIntent.getData();
if (uri != null) {
newsong = uri.toString()...
play(newsong);
} else {
moveTaskToBack(true);
playoldsong();
}
It works great, except for the fact that, when started as in (1) and, after a while, I select a song in a filemanager, the main activity is brought to the front (which is what I want) BUT it continues playing what it was playing before the song selection. The player ignored the new song, and I want it to switch to it.
This actually makes sense, since the only place where I call the new song is in the onCreate.
Maybe the launchMode is not the correct one? Maybe I should #Overwrite the OnNewTask(Intent) method? Which is the 'cheaper' solution?
You have to override onNewIntent() to start the song the user clicked on in the file manager.
I have background service in my android app,i start service from Splash_activity onCreate() method and it is work correctly.But how can i stop service when user press home_activity backpress.Because currently when user press home button then application close but background music continuously play. i can't stop background music. Below is my code for start service -
Intent svc = new Intent(this, BackgroundSoundServi.class);
startService(svc);
I want to stop background music in home_activity back press button because i start service in splash activity and when user back multiple(as per user needs) time then it's come in home activity and then close application. i m already try onPause(), onStop(), onBackpress() and stackoverflow all another solution and also read service full process for close services but i can't able to fix issue pls helm me i am new in android.
Also i want to settings on/off background music option what should i do for that...? and how it's work.
Inside your home_activity use:
#Override
protected void onPause() {
super.onPause();
stopService(new Intent(home_activity.this,BackgroundSoundService.class));
}
I got a bug on my MediaPlayer.
When i click on an item of my playlist, it create a mp3Player. If i press back the music play in background (all is normal). But if i choose another song from my app, the app create a new mediaplayer:
Then i got 2 music playing !
I tried to use "singleTask", "singleInstance", and it's not working.
I tried "intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);"
None of this work!
Maybe if i could resume the previous Activity, i could destroy it and then create a nex mediaplayer no?
Can someone help please? :D
You play music in backround it means you use Service ?
Problem is you need reconnect with Service (Service play music).
How to reconnect service android
Or try. Media Playback , EG: Universal Android Music Player Sample
you can simply make your activity android:launchMode="singleInstance" in manifest.
and then in your activity overide below method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// do what you want here
}
in above method, you can reset value of media player
I am looking for a way to pause Spotify playback. I have tried searching, and using the AudioManager, but I believe that only works if it is part of your activity. Does anyone know a way to pause Spotify playback?
How to stop or pause Pandora and Spotify
That issue is similar and shows how to pause and stop most media players.
I found that this works best:
Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
pauseSpotify.setPackage("com.spotify.music");
sendBroadcast(pauseSpotify);
Essentially, what you are doing is, creating a new intent which action is the built in "PLAY" action from the spotify app. Next you set the package and finally call the intent.
I got the idea from an article and applied it to normal android.
//play
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",true);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
//pause
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",false);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
I've analyzed Spotify's apk file and found the command file.
This would work perfectly on version 8.4.77
You can skip song by replacing "PLAY" to "NEXT".
I tried "PREVIOUS" but it didn't work, guessing it as spotify's default design.
I have an activity and one service. When i press the home button the activity paused and the service starts. I want, when i starts again my application to stops the service and my activity starts....Thanks.
Change your design.
There is no need to switch between playing the music in an Activity and in a Service. Instead you should keep playing in the Service, and continually update the Activity from there using for example BroadcastReceivers.
These broadcastreceivers should be registered in the activity's OnResume and unregistered in the activity's OnPause.
See Using a Service with MediaPlayer from the official android documentation
Stop the service in OnResume(). Start the service in OnPause().
If you don't want/can't change the design of your app, you must use this flag with your Intent in your service:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
If you don't want a new instance of your Activity, set launchMode for your Activity in the AndroidManifest file:
android:launchMode="singleTask"