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();
}
};
Related
Does anyone know where this exception come from, I just making a game and every click has a sound, isn't a problem for sounds to interfere? I created a class call playaudio importing audioplayer and audiocache with play and pause methods, and instantiate it when I need it.
Note: this error is happening rarely, because I'm using Timer to automate the game so it happening just in some points.
Flutter audio player Exception
My playaudio Class
Update:
I was facing this error because I was automatically runs multiple sounds in the same time, after adding some conditions lines to dominate one sound upon others, for example to prioritize win sound effect upon the restart sound effect like:
if (!win){
play(restartSound)
}
That's what solved my problem.
I'm trying to play sound from URI Using MediaPlayer Class but when i tried this my app crashed then i noticed that mediaplayer.create() function returns null what can i do to solve this problem?`
mediaplayer=MediaPlayer.create(this,Uri.parse("http://example.com/files/music.mp3"));
if (mediaplayer==null){
Toast.makeText(this,"media player is null",Toast.LENGTH_LONG).show();
}
else
mediaplayer.start();
now when i run my app it shows Toast message "media player is null"
I experienced similar problem with MediaPlayer
the create() method sometimes return null
Some of the file types I have tried are mp3, wav, and m4a
I know they aren't corrupt because I can play them in winamp/itunes
While the documentation does say that it supports those file type, it turns out that it actually doesn't
I noticed that when the audio file (regardless of file type) is 10Mb or more then the MediaPlayer will return null. It made me conclude that MediaPlayer can't play file above certain bitrates. big size = better quality.
So my solution is: Convert everything into .mp3 with 128kbps bitrate
EDIT:
It looks like the problem is with my emulator, when I used a real Android device, everything works perfectly. Make sure that you test on real Android Device. Or in my case, I created another AVD profile with better spec and it works
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?
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.
Hello I am currently new to android development I been working on the examples in the Dev Guide in the android website. I want to get a stream from a server I have to play in the emulator when I insert the url it doesn't seam to want to play. My question is there a way to get the emulator to play audio or is it all enabled also does MediaPlayer require a special kind of format like mp3 or ogg ?
This is the code i am running on my 'onCreate()' method.
MediaPlayer mp - new MediaPlayer();
mp.setDataSource(MY_URL_);
mp.prepare();
mp.start();
Hope you guys can help my out.
The code is correct, it works on my device and it doesn't work on emulator. So I guess you need device to hear the sound.
You used to be able to set a flag "-useaudio" in the command build scripts, but now it seems the emulator won't play sound, or at least mine won't.