Serious problems playing sounds on my app with MediaPlayer - android

My game play short sounds pressing buttons depending on sharedpreferences following this lines
SharedPreferences sound = getSharedPreferences("settings",
Context.MODE_PRIVATE);
if (sound.getInt("SOUND", 10) == -1 || sound.getInt("SOUND", 10) == 10){
mp = MediaPlayer.create(getApplicationContext(), R.raw.selection);
mp.start();
}
Normally, the sounds are played too frequently in the game, and this works fine but, at some point random errors occurs.
For example, start other activities, the sounds don't play and, in some random button press, start to play again.
Those problems began to happen after I implemented this lines in the code.
I don't know the relation with those actions here, for this I need your help.
Is something wrong here or can I implement some alternative way to play sounds?
Thanks

Related

switching from Media Player to Media Player force close

hi ive been working on my app and have found a hurdle in an activity i just cant seem to overcome. the activity is essentially a large novelty piano with 8 keys that play sounds there are 6 buttons along the side that change the picture of the notes and the notes themselves, this i accomplished :-) now i would like to add a background song to each different set of notes (instruments) only when i do the app crashes i have set the mediaplayer at the top (globally?) using MediaPlayer mp; MediaPlayer mp2; etc and im using this in my code to check if any music is playing, stop it, release it, and then play the piece i want,
if(mp!=null&&mp.isPlaying()){
mp.stop();
mp.release();
}
if(mp2!=null&&mp2.isPlaying()){
mp2.stop();
mp2.release();
}
if(mp3!=null&&mp3.isPlaying()){
mp3.stop();
mp3.release();
}
mp3 = MediaPlayer.create(MusicActivity.this, R.raw.snaps);
mp3.setLooping(true);
mp3.start();
this again works but going from one to another and then back crashes the app. is there something else i should be doing? or is just an out of memory error?
EDIT--
Thanks to ranjk89 for his help
changed to soundpools but they wont stop ranjk89 suggests referring to the stream id and not the sound id looking for a little more clarification if possible i have
farmback =0;
drumback =0;
at the top then its loaded in oncreate using
drumback = sp.load(this, R.raw.snaps,1);
farmback = sp.load(this, R.raw.oldmacdonaldbeta,1);
and then way down, in the same method i change the button icons, not the same method i change all my other sounds for my notes i call
sp.stop(drumback);
sp.play(farmback, 1, 1, 0, -1, 1);
in one and
sp.stop(farmback);
sp.play(drumback, 1, 1, 0, -1, 1);
in another but there are 6 different instruments in total that all need a different backing track which should stop when the instrument is changed and play the one associated to it so something like
if (sp !=null && sp.isplaying()){
sp.stop();
sp.play(dumback);
}
but obviously this is not possible any help appreciated
My Initial reaction is, you shouldn't be using multiple MediaPlayer at all in the first place.
I would suggest you to use SoundPool as you can load all the media(audio) initially, once they are all loaded you can play them at will. Plus you won't have multiple players which would downsize the complexity of your code.
But if you do want to use Media player,
See this. But be aware that once you release you will not be able to reuse that instance of MediaPlayer. Even if you are using multiple MediaPlayers, do not call release on them unless you are sure you don't want to use them any more. Use stop()/prepare() combination.
For application doing too much work on the Main Thread,
If you either use a SoundPool or only one MediaPlayer and still bump in to the same issue, Use AsyncTask. Android dev site has a very good tutorial on this

How to play sounds at accurate periods of time across different devices in Android

I'm developing a game in Android and I came across a very annoying, hard-to-find bug. The issue is that when you are using SoundPool to play your sounds, you can actually loop whatever sound you are playing. In this case, the issue is the "running steps" sound; this sound gets executed quite fast and continually (around every 400ms) when the main character is running.
Now when playing the sound on a regular (not so powerful) device e.g. Samsung SII, the sound is played every 500ms - however, if I run the very same code on another device (let's say, Samsung SIV, Samsung SIII), the sound plays twice or even three times faster.
It seems like the more powerful the device hardware specs are, the faster it plays. On some devices, it plays so fast that you almost hear one solid continuous sound. I've been looking for techniques to set a specific ratio on the time period between sound plays, but it doesn't work properly and the issue remains. Does anyone know how to fix it, either using SoundPool, MediaPlayer, or any other sound-controlling API on Android?
You could use an AudioTrack to play a continuous stream of PCM data, since you would pass a stream you could be sure about the interval between sounds. the downside could be a little delay when first starting the sound but it depends on the minimum buffer size, and it depends, I think, on android version and device. On my galaxy s2 android 4.1 it was about 20ms.if you think this could be an option I can post some code
The problem with just looping or using a regular interval for something like footsteps is that you have a possible decoupling of sound and visuals. If your sound gets delays or sped up, or your visuals get delayed or sped up, you would have to adjust for that delay dynamically and automatically. You already have that issue right here
A better solution would be to place a trigger on the exact event which should trigger the sound (in this case, the foot being placed down), which then plays the sound. This also means that if you have multiple sources of the sound (like multiple footsteps), you don't have to manually start the sound with the right interval.
I can't seem to replicate the issue on Galaxy Nexus and Nexus S, does that mean I fixed it? Or maybe you could show what you're doing differently from this:
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
Integer sound1 = soundPool.load(this, R.raw.file1, 1);
Integer sound2 = soundPool.load(this, R.raw.file2, 1);
playSound(sound1);
public void playSound(int sound) {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC)
/ mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundPool.play(sound, volume, volume, 1, -1, 1.0f);
}
If the problem is that you want to control the interval between the discrete sounds, The easiest way to do this is with a handler.
Basically you start a sound playing which is an asynchronous process. Then you use a handler to schedule a message to play the next sound sometime in the future. It will take some trial and error to get it right, but you will be guaranteed that the sound will start at the same interval after the previous sound on every device.
Here is some code to illustrate what I am talking about.
Here is a handler implementation you could use:
handler = new Handler() {
/* (non-Javadoc)
* #see android.os.Handler#handleMessage(android.os.Message)
*/
#Override
public void handleMessage(Message msg) {
if (msg.what == NEXT_ITEM_MSG) {
playNextSound();
}
else if (msg.what == SEQUENCE_COMPLETE_MSG) {
// notify a listener
listener.onSoundComplete()
}
}
};
Then you could write playNextSound like this:
private void playNextSound() {
if (mRunning) {
// Get the first item
SoundSequenceItem item = currentSequence.getNextSequenceItem();
if (item == null) {
Message msg = handler.obtainMessage(SEQUENCE_COMPLETE_MSG);
handler.sendMessage(msg);
return;
}
// Play the sound
int iSoundResId = item.getSoundResId();
if (iSoundResId != -1) {
player.playSoundNow(soundResId);
}
// schedule a message to advance to next item after duration
Message msg = handler.obtainMessage(NEXT_ITEM_MSG);
handler.sendMessageDelayed(msg, item.getDuration());
}
}
and your SoundSequenceItem could just be a simple class that has a sound file resource id and a duration. If you want to keep playing the sound while the character is moving you could do something like this:
public void onSoundComplete() {
if (character.isRunning()) {
currentSequence.addSequenceItem(new SoundSequenceItem(R.id.footsteps,500);
playNextSound();
}
}
Or you could modify playNextSound to continually play the same sound. Mine is written this way to be able to play different sounds in sequence.
I have had a lot of problems developing apps which used sounds and stuff like that. I would not suggest you to use SoundPool since it is bug-affected, and also be aware that looping sounds with SoundPool won't work on devices which are 4.3 and higher, see this open issue, at AOSP - Issue tracker.
I think that the solution is to go native and use OpenSL ES o similar libraries.

How to make play music button in Android?

Just to specify the question a little more, I will give an example. I have a car stereo and when I get in the car I can just press play and it plays the last song I was listening to on my phone. So what I need is like a very general Android button that does just that. I just want this button to start media. It's difficult to explain but I know like Google Music when you open it always has a song at the bottom that you can press play. So it's kind of like just continuing a previous queue? If more details are needed please let me know. Sorry I do not have any code for this problem because I have never worked with media but if you would like the source to my app that this code will go into please visit
http://mellowdev.net
The app is called Mango. Open source.
EDIT: I have tried the following but nothing plays.
MediaPlayer mediaPlayer = new MediaPlayer();
int position = mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(position);
mediaPlayer.start();
As I understand you want to play the music from last known position (or better say from the last known time of the sound). It's something similar like the user change the orientation then you want to play the sound from current position and don't want to start the sound again.
Therfore you have two method in the MediaPlayer:
int position = mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(position):
I think this post will help you (the example is for a VideoView, but it should be similar to the MediaPlayer).
Hope this will help you :)
EDIT (2013 08 16)
// create a MediaPlayer Instance with the sound you want to play
MediaPlayer mediaPlayer = MediaPlayer.create(context, mySound);
I couldnt't exactly understand your code. But if you need to play media in android, you need to create instance of MediaPlayer and can start to play using a button. Below link will give you more insight:
http://developer.android.com/guide/topics/media/mediaplayer.html
Sorry if I mis understood your question.
To just launch music player:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

Android MediaPlayer causing Not Responding error - Release not working as intended

I'm making an Android game-app in which you start as a little fish, and you need to eat smaller fish.
Each time you eat a smaller fish (by swimming against them), a sound plays. This is the code for playing the sound:
mp = MediaPlayer.create(GobbleFishGame.this, R.raw.bite);
if (!mPlaySound) {
return;
}
mp.start();
MPRelease();
This works well for the first few fishes. But after having eaten some fishes (the amount varies, but it's around 8-11 fishes), the game suddenly stops and I get the "Activity GobbleFish (in application GobbleFish) is not responding." error. I think it has something to do with too much resources being allocated to the MediaPlayer, therefore I've created a sub which releases mp's resources, and then creating a new MediaPlayer mp (this sub is called every time a fish is eaten). This is the code for the sub:
private void MPRelease(){
MPReleaseCount += 1;
if(MPReleaseCount==5){
mp.release();
mp = new MediaPlayer();
//mp = MediaPlayer.create(GobbleFishGame.this, R.raw.bite);
//mp.reset();
MPReleaseCount=0;
}
}
When MPReleaseCount is 5, mp gets released and recreated (so after 5 fishes are eaten).
I hoped that this would work, but it doesn't. I still have the problem that after some fishes are eaten (last time was 17 fishes, so MPRelease should have been called 3 times already), I get the "is not responding" error and I have to Force Close or Wait.
Can anyone tell me how I could do this to release the resources? Because I think that's the issue. I can't really tell if that's the issue because I don't get any error message in my LogCat in Eclipse. I did get this error report after Force Closing the app because of a "is not responding" error:
ANR in (my activity)
Reason: keyDispatchingTimedOut
Thanks in advance.
Don't release and create the MediaPlayer everytime you used it. Create it once with your audiosource and call a seekTo(0) in an OnCompletionListener(). Then play it again.
Also have a look at SoundPool http://developer.android.com/reference/android/media/SoundPool.html which is made for usecases like yours where you have to play the same audio multiple times. It stores the audio and plays it on demand. You also have a lower audio latency than with MediaPLayer.

Media Player crash after use

I am working on a small application that plays back eight different .5 second midi files. I have an ontouch action that gets called everytime a specified object gets touched and within that I have an if/else if statement that plays the specified midi file depending on the object touched. When I run this, It works fine for the first several touches but after a while the playback stops, and then the app eventually crashes. What is going wrong here?
Basic Structure:
public ontouch(){
if (something >= 3){
mediaplayer s = mediaplayer.create(somethingmidi2);
s.start();
}else if (something < 3){
mediaplayer s = mediaplayer.create(somethingmidi);
s.start();
}
} `
Just a guess, but you probably aren't calling the release() method of the media player, nor reset() when you are reinitializing it.

Categories

Resources