mediaPlayer single Instance - android

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

Related

Confused with launchMode

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.

MediaPlayer continues playing after Activity.recreate()

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);

How to play sound using service in android

I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?
Please someone help me. Thanks...
Use MediaPlayer from Android. See: Android Example
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();

Pause spotify playback intent

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.

How to know my media file playing has been stopped in android

I have a buttons called play and stop
if I press play its starting playing. after I press stop its going to home page. But I need to go home page automatically once playing is done ie. Without clicking stop button. How can I do it for android?
thanks in advance
hi shekhar you can set an oncomplete listener to the player object. The code follows:
mPlayer.setOnCompletionListener(new OnCompletionListener(){#Override public void onCompletion(MediaPlayer mp) { "your code comes here" }});
here the mPlayer is the object of the MediaPlayer which is currently running..

Categories

Resources