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.
Related
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 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 can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance
I have a sound that is supposed to start when the activity starts onCreate because this is an alarm. THe problem is, the sounds doesn't start. I've checked my volume, it's on.
This is the code for the media player:
String tone = intent.getStringExtra("reminderTone");
MediaPlayer mPlayer = new MediaPlayer();
try {
Log.d("log", "try");
if (tone != null && !tone.equals("")) {
Log.d("log", "tone is not null");
Uri toneUri = Uri.parse(tone);
if (toneUri != null) {
Log.d("log", "mediaplayer starts");
mPlayer.setDataSource(this, toneUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.setLooping(true);
mPlayer.prepare();
mPlayer.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
and this is the log output
08-09 10:55:33.316 20706-20706/? D/log﹕ try
08-09 10:55:33.317 20706-20706/? D/log﹕ tone is not null
08-09 10:55:33.317 20706-20706/? D/log﹕ mediaplayer starts
I've already checked "reminderTone" there is a String URI in it.
This is how the String URI looks like. "android.media.Ringtone#ab91882"
I used another source of sound to test, and this one works:
//play ringtone
final MediaPlayer sounds = MediaPlayer.create(this, R.raw.sample);
sounds.start();
Do you guys know what's wrong with this?
Context appContext = getApplicationContext();
MediaPlayer resourcePlayer = MediaPlayer.create(appContext, R.raw.my_audio);
MediaPlayer filePlayer = MediaPlayer.create(appContext, Uri.parse("file:///sdcard/localfile.mp3"));
MediaPlayer urlPlayer = MediaPlayer.create(appContext, Uri.parse("URL"));
MediaPlayer contentPlayer = MediaPlayer.create(appContext, Settings.System.DEFAULT_RINGTONE_URI);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("/sdcard/test.3gp");
mediaPlayer.prepare();
You need to check your URI, make sure you use the proper URI.
Note: URI string is different for every storage media that you are
calling, in my example I am assuming that you are looking for the
Music inside your raw folder.
int i = R.raw.sample; // we assume that we want to get the URI of our sound from our RAW folder.
String uri = android.resource://com.example.myproject/" + i;
For playing the music you can use this simple method
MediaPlayer player = null;
public void playMusic(int i) throws RemoteException {
try {
if (player == null)
player = new MediaPlayer();
player.reset(); // optional if you want to repeat calling this method
player.setDataSource(getApplicationContext(), uri);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The full working example can be downloaded here : Music Player Tutorial
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?