MediaPlayer.prepare() throws IllegalStateException on Android L - android

I have code that plays mp3 file from assets directory:
MediaPlayer mediaPlayer = new MediaPlayer();
descriptor = context.getAssets().openFd("beep.mp3");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(soundLevel, soundLevel);
descriptor.close();
mediaPlayer.setLooping(false);
mediaPlayer.prepare();
mediaPlayer.start();
This code was working fine on every device and every Android version. Until after Android L preview came available.
mediaPlayer.prepare();throws IllegalStateException on Nexus 5 running Android L.
Emulator version of Android L works completely fine, I don't own Nexus 5 so can't say what exactly is causing the issue. I am thinking about descriptor.close() line before prepare() called, but not sure.

public void myBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("beep.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
};

Related

play mp3 file in resource folder not working

I want to play an mp3 file in my res/raw folder.
But i get error as "error (1, -2147483648)" and IOException on mp.prepare()
My code
try {
MediaPlayer mPlayer = MediaPlayer.create(NavigationHome.this, R.raw.notfy);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
I also tried with
try {
mp.setDataSource(NavigationHome.this, Uri.parse("android.resource://com.hipay_uae/res/raw/notfy"));
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
Another solution that I tried
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
These too didn't work for me.
It will help more if you can post the StackTrace in your question.
But, as per the information in your question, the below code should work for playing the media file from the raw resource folder.
If you use the create() method, prepare() gets called internally and you don't need to explicitly call it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();
But, the point to consider is that prepare() generally throws an IllegalStateException, and in your case, you are getting an IOException. So it would be worth checking if the file is in fact present in raw folder and/or the file is corrupt.
Try to initialize your media player before preparing it or setting data source to it
Play From external directory
String filePath = Environment.getExternalStorageDirectory()+"/folderName/yourfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
From raw folder
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.song);
mediaPlayer.start();
Try this
String fname="your_filename";
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();

How to play music from receiver at android 5.1, it is different form android 4.2

I want to play music from the receiver instead of loud-speaker, I use below code:
am.setMode(AudioManager.MODE_IN_CALL);
mMediaPlayer = MediaPlayer.create(this, R.raw.src2k);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
It works well in android 4.2, but it does not work in android 5.1, at android 5.1 it still play from loud-speaker. I have tested several cell phones at android 4.2 and 5.1. Anyone can give me some advice? Thanks.
I have solved this problem by myself, below code works well:
mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.STREAM_MUSIC);
mAudioManager.setSpeakerphoneOn(false);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.reset();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Uri uri=Uri.parse("android.resource://com.ee.ff/"+R.raw.music);
try
{
mMediaPlayer.setDataSource(this, uri);
}
catch (Exception e)
{
e.printStackTrace();
}
mMediaPlayer.setLooping(true);
try
{
mMediaPlayer.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
mMediaPlayer.start();

Android Media player error (1,-4) while playing an audio from Assets folder

I need your help. I tried to play an audio file stored in Assets folder but an error occurred.
Here are my code:
try{
if (player.isPlaying()) {
player.stop();
player.release();
}
}catch(Exception e){
Toast.makeText(this, "an exception occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try{
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets().openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
}catch(Exception e){
e.printStackTrace();
}
And here are my logcat:
06-16 22:39:53.330: W/MediaPlayer(13490): info/warning (1, 26)
06-16 22:39:53.330: E/MediaPlayer(13490): error (1, -4)
Could you please explain what's wrong with my code?
Thank you in advance
Regards,
Priska
This issue has been SOLVED.
The asset file descriptor must be closed before preparing the player. This is how I solved the problem:
player = new MediaPlayer();
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets()
.openFd("sounds/"+file);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
afd.close();**//just added this line**
player.prepare();
player.start();
Here you can see all Error codes Media player Error codes
-4 error code indicates you have given invalid arguments.
Put your code in try catch block.
Try Using
try {
AssetFileDescriptor afd = CustomListViewActivity.this.getAssets()
.openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Unfortunately there is very little information about MediaPlayer error codes available for some reason. However I suggest you try putting your sound file inside res/raw/ instead of your assets.
EDIT:
Start here with the Using the MediaPlayer section in the developer docs. This will show you how to set up and play the sound properly.
EDIT 2:
turns out that can do it from assets see this question: Play audio file from the assets directory
I don't think that wma files are supported.
http://developer.android.com/guide/appendix/media-formats.html
I noticed that you didn't specify the audioStreamType
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MISIC);
use this way it will solve your problem :)
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
}
}

android MediaPlayer is playing the wrong mp3 file in assets directory

I'm trying to play an mp3 file from the assets directory, but when I start it up with the MediaPlayer, something completely different plays. Here's the code:
String mp3File = "dir/a/music.mp3"; //the path here is file:///android_asset/dir/a/music.mp3;
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();
Instead of playing mp3File, it seems to play a bunch of other files that reside in the assets directory. Any ideas?
Use this way it is very useful function :)
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
}
}
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
descriptor = getAssets().openFd( "filename.mp3" );
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();
put your mp3 in the assets folder.
you can refer this link also play-audio-file-from-the-assets-directory and this also android-problem-playing-sounds-from-assets-folder

Android media player bug

I have the media player playing an mp3 when I load my application. But I had to move this application and now every time I load the application this gives a force close error.
The media player is opened like this:
final MediaPlayer mp = MediaPlayer.create(Splash.this, R.raw.indra);
mp.start();
I know its the media player which causes the error as when I comment the lines above out the application works.
Is there any other ways I can try to load the mp3?
Thanks
Edit:
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor = contex.getAssets().openFd("indra.mp3");
mp.setDataSource( descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();
Edit:
try {
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
descriptor = contex.getAssets().openFd("indra.mp3");
mp.setDataSource( descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Just put your file in asset folder n apply this code..
Media Player mp = new MediaPlayer();
AssetFileDescriptor descriptor = contex.getAssets().openFd(fileName);
mp.setDataSource( descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();

Categories

Resources