How to keep MediaPlayer going no matter what - android

So I've been working on a music player for Android and I've run into a problem I'm having trouble fixing. When the user starts a song, I store the currently playing song in a public variable in my "Player" class. This is used to determine what song to play next, among other things.
The problem is that the app sometimes crashes when the user opens a lot of other memory intensive apps. The MediaPlayer stops playing, and the reference to the currently playing song is lost. I'm pretty sure this happens because these other apps claim the memory that my app was using.
My question is: How can I make sure the MediaPlayer always keeps on playing? The standard Android music player doesn't seem to have this problem so it should definitely be possible to keep it playing at all times somehow.

The best you can do is host the MediaPlayer in a foreground Service.

Related

How many MediaPlayer instances should I create for playing a music collection?

Thinks I've considered so far:
Just one for the currently playing song. For the next song a new instance is created every time. Though one also need to call prepare and I expect there to be a noticeable gap between the songs.
One MediaPlayer instance for every song. There could be more than a hundred songs though and I read that creating too many MediaPlayer instances could lead to problems.
I've tried to implement some logic to have a MediaPlayer instance for the currently playing song as well as the next and the previous (three in total). This turns out to be a lot more complicated than I expected.
Since I'm hardly to first person to implement a music player for Android, I'd like to ask, is there a generally recommended approach for how to do this?
Having multiple MediaPlayers is not a good Idea. MediaPlayers are really a pain to deal with. Once one MediaPlayer gets out of your sight (you lose the reference to it ) it keeps playing music and the only way to kill it is by killing the whole App. This is a very bad user experience. The generally recommended way is to play all of the collection with one MediaPlayer; and after the track is finished or when you want to skip to the next song then you just call:
mediaPlayer.stop()
mediaPlayer.reset()
mediaPlayer.create()
Calling this sequence will clear the MediaPlayer from old data and give it the new data and will avoid problems like having two MediaPlayers playing on top of each other.
If you want the music to play in the background you are going to need to use a Service and place the MediaPlayer in it.

How to turn off all other music when my Android app is playing music?

I was wondering how to exactly stop all other music that may be playing when my app is opened whenever there is music being played in my application. I have a couple instances where I just have sound effects, so is there anyway I can stop music that may be playing already whenever these sound effects pop up? I know I'm not giving any code, however, I have no idea how to approach this and haven't found many other questions asking similar things.Thanks!
Look at the AudioManager.requestAudioFocus API. Remember to release the focus when you're done with it, or other apps may not be able to play sounds correctly.

Playing music in PlayN: how to pause, resume?

I am playing some background music in my PlaynN game. Everything is fine except it does not pause when the activity is sent to the background. Music still plays when the game is not playing.
I can use Sound.stop() and Sound.play() whenever the window focus changes but then the music restarts from the beginning. I can instead use setVolume(0) and setVolume(1), but it still doesn't sound the way it is supposed to.
I cannot find a working example. I am currently considering the use of a platform specific music player.
Many thanks
To my knowledge, PlayN doesn't support pausing/resuming a Sound, probably because not all platforms support this. However, you might want to take a look at the Android-specific implementation of AndroidAudio, which seems to use a SoundPool backend. The GameActivity seems to already pause and resume sounds when the window gains/looses focus, so I'm not sure why it's not working for you - I believe it has for me. You might try calling these methods manually to test that they work, and if not consider filing an issue.

Android media player stops when open gallery

I developed a simple music player using MediaPlayer class. I played a song using it, minimize my music player, then I open native gallery, and my application get weird.
It becomes like stuck for a few seconds, and then it started to play new song.
I also did like that using android native player and the player paused when gallery opened.
Do you guys know what might cause my application stucked like that?
I really appreciate any help from you guys,
Regards,
Chrisma Andhika
see in your app, if you minimize your app it goes to onpause() state again come return to app it will call onresume() state. so it takes some time. there is nothing weird in this thing.

How do I stop the sound restarting when a collision occurs

Currently my code plays a sound when there is contact between any objects in my game using andEngine and Box2D and the walls, the problem I have is that when contact is made with any objects it starts again I understand why this is happening. What I want to do if possible is keep playing the sound while also playing it for another collision. I think I may need to use threads but I am unsure how to do this in java for android.
#Override
public void beginContact(Contact contact)
{
Rattle.this.mExplosionSound.play();
}
UPDATE: I don't seem to have been able to fix the issue but I know that what I need to do is play this sound multiple times simultaneously, I have tried threads and the soundPool but got no luck with either still not sure what to do.
It seems you have only one instance of the sound. Then you call play on the same sound twice, therefore it stops playing and starts from the beginning.
Imagine having one CD player - you press play, the music starts to play. You press play again and the player starts reading the track from the beginning. What you need is two players, therefore you may need two instances of the same sound.
(I don't have much experience with sound on Android so I may be wrong, please take the advice with a grain of salt.)
Even I was developing a game some time back and I found a similar problem. You should use SoundPool to play short sounds like crashing and collisions. here are some good linkswhich helped me. Link 1, link 2.

Categories

Resources