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();
Related
I got a path of media file and want to play it.The problem is MediaPlayer.create() work successfully but when I use setDataSource,it's not working correctly, the media not play the music
player = new MediaPlayer();
String path =
"/storage/emulated/0/Music/AThousandYears_ChristinaPerri.mp3";
try {
player.setDataSource(getApplicationContext(),
Uri.parse(path));
player.start();
} catch (IOException e) {
e.printStackTrace();
}
I did not got any exception. Sorry about my bad english.I hope someone help.Thanks in advance
Try this
try{
mediaPlayer.reset();
mediaPlayer.setDataSource(songPath);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exception e){
//Handle Exception
}
There is mp3 file which is 20 seconds long.
But MediaPlayer only plays 2-3 seconds.
I am using below code to play mp3 from raw file.
try{
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(R.raw.buzzer6);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
mp.start();
}
});
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
} catch (IOException ioe) {
ioe.printStackTrace();
}
So what is the issue?
I want to Play full mp3 file which is 20 seconds.
I suggest you to have a look at this post
Basically if you try to enlarge the scope of MediaPlayer as instance variable it should work
(In the example is declared static, you probably do not need to declare it static)
just change this line:
MediaPlayer mediaPlayer = new MediaPlayer();
in:
private MediaPlayer mediaPlayer //is an instance variable not a local one
while into the method you maintain:
mediaPlayer = new MediaPlayer();
I'm done my Android app, just need to add some looping background music. Here is my method for playing the song
public void playAudio(){
path = "android.resource://" + getPackageName() + "/" + R.raw.music1;
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.setLooping(true);
mp.setVolume(100, 100);
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.d("NOTE WORKEING","NOT WORKING");
}
}
It's not working....It's going to catch everytime, and I don't know why. Please help me. Music1 is an mp3 file.
Thank you
Make sure your music1 file is in a Android playable Android format.
Then just use this code:
MediaPlayer mediaPlayer = MediaPlayer.create(YourActivity.this, R.raw.music1);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
mediaPlayer.start();
}
catch (Exception e) {
e.printStackTrace();
Log.d("NOT WORKEING","NOT WORKING");
}
You don't even need to call prepare() method.
I tested it with an mp3 file and it works perfectly.
If #Squonk's answer does not work, then the problem has to do with trying to start the music file before the MediaPlayer has prepared it. Try changing the MediaPlayer's start code to the following:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
}
});
Let me know if this helps, I have a good amount of experience with the MediaPlayer class and can help you troubleshoot.
Using mp.setDataSource(path) requires a valid filesystem path or URL.
In your case, path is a string representation of a Uri in which case you need to use a different approach.
Try setDataSource(Context context, Uri uri). You'll obviously need to provide a valid Context and parse your path variable into a Uri. Example...
mp.setDataSource(getApplicationContext(), Uri.parse(path));
Also change the path to...
path = "android.resource://" + getPackageName() + "/raw/music1";
This is my code: mp3 format file plays without any error, but wav format brings MediaPlayer error(1,-1):
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("h*.wav");
player.prepare();
player.start();} catch (Exception e) {
// TODO: handle exception
}
Kindly make a reference to Android supported media-formats
I am trying to play audio files stored in the assets directory, but before I even read a file I get the error "error occured java.io.FileNotFoundException: track.mp3". Here's how I read it:
AssetFileDescriptor afd = getAssets().openFd("track.mp3");
I read a lot of descriptions on the internet, but no success.
I think this code should work for you:
AssetFileDescriptor afd = getAssets().openFd("track.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
Otherwise, check if the file is in the right folder and the name is "track.mp3".
Right-click your "track.mp3" file, with the "Refactor" > "Move" command, move your file to another directory (e.g. "res"). With this same command, move it back to your "assets" directory. For me, it works.
Android guidelines suggest for Audio use res/raw folder. So make sure that all audio are there and when you read you can use this method:
void playAudio(Context mContext, String sound){
new AudioPlayer().play(mContext, mContext.getResources().getIdentifier(
sound,"raw", mContext.getPackageName()));
}
And if you need something line audio player then take a look at MediaPlayer
Use this code
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("1.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Toast.makeText(LearnActivity.this, "Error Playing audio from Asset directory",Toast.LENGTH_SHORT).show();
}
Otherwise, check if the file is in the right folder and the name is "track.mp3".