Android MP3 MediaPlayer Crashes Some Handsets - android

I released my first Android app two weeks ago (an MMO called Agent Syndicate) and everything has been working well, except for sounds and music!
Using standard MP3 files created in Sony's Sound Forge Pro 10 at 128kps, I immediately received reports from many users complaining about Force Close issues on a wide variety of handsets, mostly running Android 2.1 and 2.2. I released a new version which has all sounds turned off, and the complaints ceased. I'd really like to have sounds however, so this is far from ideal.
I don't have access to the resources to test the game on anything other than my personal phone and the emulator (where everything works flawlessly), but it's obvious there is a major issue for many players!
Should I not be using MP3s? Is there a universal method that works best for all handsets for just playing simple sound files?
This is what I'm using to start my sound effects:
final MediaPlayer player = MediaPlayer.create(GlobalState.Instance, resId);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
player.setVolume(0.5f, 0.5f);
player.start();
I originally was calling player.prepare() before player.start(), but kept getting IllegalStateExceptions on some of the sound files. Some of the information out there is very confusing on when exactly to call these methods, and what the exception means. Removing player.prepare() made it work 100% of the time for me on the emulator and my local dev device.

There should be no problem using the MediaPlayer for music but for sound effect I would use the SoundPool class instead. My guess is you get the IllegalStateExceptions because you are starting the MediaPlayer before the player preperation is completed. Correct way would be to introduce a onPreparedListener for the MediaPlayer and start is from there. Are you making MediaPlayer operation on the main UI thread?

Related

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.

android media player delay

I am making a game for android which needs to play a small beep whenever a user touches a certain object.
To do this I have this simple code to create the media player. beep is a small .ogg file of ~1.5 sec length.
MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.beep);
mp.setVolume(0.5f, 0.5f);
and use it like this
mp.start();
The problem I am having is that my game uses the android canvas, and the thread which draws to the canvas also calls mp.start(). I have discovered that playing the sound is taking quite a lot of time, and it is very varied in how long it takes. Sometimes 1ms, sometimes 15ms. This appears to be causing my game to noticeably lag a little bit, as the android canvas is pretty slow anyway.
I've had a look at the logs and I don't appear to having garbage collection causing the delay. I'm wondering if anyone can give me some advice to reduce this delay
EDIT - I've just discovered that MediaPlayer is designed for longer sound files, and SoundPool is better for shorter audio files. However, I load the file into the mediaPlayer only once, so I don't see how it should make any difference. I will test it out though
sorry for my english
Use SoundPool instead of MediaPlayer cause MediaPlayer need releasing memory after each call or call method mp.release() after each call of player
SoundPool tutorial here

Uanable to create MediaPlayer

I have been using a mp3 file in my application for over 3 weeks and it was working fine.
Now it shows in logcat as unable to create media player
I have to comment out //mp.start(); to make my application work.
Does anyone know what the issue could be?
Thanks
Android VMs are rather finicky, you may want to first try creating a new VM, and starting before you try launching the application. If it works set the new VM as the default.
You will also want to put mp.start() in a try catch block and logd any exceptions. This will also let you display a message to the user on failures without crashing on a published version.
Have you started testing in a newer version of emulator. I had a similar issue with bitmap. It was working fine until I tested in Android 2.3 emulator. It took a while for me to figure out it is the new emulator that was bottleneck.
Try testing in proven emulator versions. If it works there, you can then figure out what needs to be changed to make it work in new versions.
Are you playing a number of mp3 files. If yes, you might want to call mp.release in your onCompletion Listener
sample code..
// wherever media is playing
mMedia.setOnCompletionListener(mCompletionListener);
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
setResult(RESULT_OK);
mp.release();
finish();
}
};

Preventing skipping or lag when adding music to Android game

I recently added simple looping background music to my Android game. It uses Canvas to draw to the screen, so I am very careful about optimizing performance. I added music using the standard MediaPlayer which seems to be the most common and easiest way to handing looping music. Unfortunately, I'm having trouble with the music skipping occasionally and, far worse, sometimes causing significant lag in the game play.
My only phone to test on is a Droid Eris, which is a fairly old phone, so I suspect this would not be an issue on newer Android phones, but I would like my game to be playable on all Android devices. What can I do to add music while maintaining reasonable performance?
This is the code I added:
// in setup() method of game thread
mediaPlayer = MediaPlayer.create(context, R.raw.rl_theme);
mediaPlayer.setLooping(true);
mediaPlayer.start();
// in method called when the player looses
mediaPlayer.stop();
mediaPlayer.release();
I have not ever had this issue, so I am not positive, but I think SoundPool is what you're looking for.
http://developer.android.com/reference/android/media/SoundPool.html
It looks like there isn't much you can do. Yes, adding music does slow Android apps down some, but it doesn't seem to be a major issue on newer phones. I did change the format of the audio files from OGG to MP4 to fix what appears to be a bug in the Android MediaPlayer (still looking into it, but the media server was crashing on OGG and works perfectly with MP4) and the performance seems to have gotten better. I'm not sure if that's related, though.

Android MediaPlayer causes game to freeze with "AudioHardware pcm playback is going to standby"

This is a tough one :/
I'm making a music-based Android game a la Audiosurf. It works all nice except a few seconds before end of a song (that is being played with a normal MediaPlayer) the music stops aprubtly and the whole game (including UI) freezes for several seconds.
Each time that happens I see an "AudioHardware pcm playback is going to standby"-error in logcat.
Googling has led me to the conclusion that
this could be a HTC Hero specific issue (cannot be reproduced on emulator or other devices)
this message is normally logged when a http stream isn't fast enough for MediaPlayer
Audio in Android sucks in general
As I am decoding the mp3 with the NDK + libmpg123 for audio analysis already I might aswell just play the audio myself (using a very ugly interface between NDK C code and an AudioTrack in Java).
Is there a fix/workaround for this bug or should I really go that way? (I only have limited time left to complete this project)
I appreciate every hint!
You might be stopping the music when you've buffered it all in your C code. Since the AudioTrack has a delay in it, you may need to wait longer for it to finish.
I'd need more detail about your code to help, though.

Categories

Resources