MediaPlayer plays only 2-3 seconds not full mp3 - android

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();

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();

Play short sound without interrupting playing music

I play music in my app using MediaPlayer. But music interrupts when I playing another sound (duration of sound is about 1 sec). The interruption is short, only a few ms, but it disturbs me.
What should I do to avoid this interruption?
UPDATE:
Here is code that I use to play short sounds:
private final Map<Integer,MediaPlayer> mediaPlayers = new HashMap<Integer,MediaPlayer>();
void playSound(int resId, boolean looping) {
MediaPlayer mediaPlayer = mediaPlayers.get(resId);
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayers.remove(resId);
}
try {
mediaPlayer = MediaPlayer.create(context, resId);
if (mediaPlayer == null){
throw new RuntimeException("Can't create MediaPlayer");
}
mediaPlayer.setLooping(looping);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
mediaPlayers.put(resId, mediaPlayer);
}
UPDATE2:
I solved the problem. The problem was that the short sound was in mp3 format, but all the other sounds in my app were in ogg format. So I converted this sound in ogg format and the music is no longer interrupted.

background sound during call in android

I want to play a sound automatically when I call to somebody
but I don't know the way for this.
It is possible because we have some application that play some noise (traffic and ...) to background of call but I could not find a code or something for this
example of sound play in background
this is a code to play music
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd(“home.mp3″);
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(true);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
but can we use this during call?

Streaming music and save cache file on local on Android

I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?

I have a query regrding to playing wave file from raw directory from res > raw

i have a query regrding to playing wave file from raw directory from res > raw,
i have written a code on button click event here is my code
public void buttonOnclick(View v) {
try {
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sc1malf);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
} catch(Exception e) {
}
}
but this code is not working on samsung android mobile ,any one correct this code
Try putting mMediaPlayer.prepare(); before start().

Categories

Resources