How to Play MID file in Android? - android

I am writing a small piece of code
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://..../file.mid");
mediaPlayer.prepare();
mediaPlayer.start();
which is not working, where as
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://..../file.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
is working fine.

Please see this thread on the Pragmatic Forums:
Java SE5 javax.sound (MIDI) classes removed from Android libraries?
Your best option appears to be through the JetPlayer class

Related

Play a sound from sdcard0 in android

MediaPlayer mp;
mp = MediaPlayer.create(this, R.drawable.test);
mp.start();
That's my code. It just plays a sound from res/drawable/ folder. I need to play a sound from android's sdcard0/Test Folder/testsound.amr How to do this ?
Please Help. Thanks in advance.
Just use the setDataSource Method:
MediaPlayer mp = new MediaPlayer();
//set the source of the Sound...
mp.setDataSource("/mnt/sdcard/Test Folder/testsound.amr");
//load the file
mp.prepare();
//Do the thing (:
mp.start();
You can also do it dynamically by doing:
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Test Folder/testsound.amr");
NOTE: I'm not clearly sure if you need to do .getPath() or not...

Android MediaPlayer OnPreparedListener

I am working on a simple app and using a MediaPlayer to play some background noise in 1 activity. I am reading up on MediaPlayer and am not sure whether or not to implement an OnPreparedListener to trigger the start() method. What are the pros / cons to each approach?
Approach 1:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.start();
Approach 2:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
As per the docs, calling start() is effective when you are playing locally available resources for which the MediaPlayer does not require to fetch the data and process it for playing. For example playing audio resources from raw folder.
If you are trying to play a resource from remote source, its a better practice to go for OnPreparedListener() because it might involve fetching and decoding media data.
So, if you know for sure, that your resource is locally available and is of short length, go for Approach 1. Otherwise Approach 2 would be suitable.
Ideally, I prefer this.
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
The MediaPlayer has always been tricky for me to work with. So, I would recommend you to start with the developer docs. Go through it, understand the state diagram. I am sure it will help you in solving lot of questions which you are yet to come across.
Let's say you are playing a video from internet. If you start directly the player it would crash because it may be not ready to play because of some internet problems or something else. But if you use preparedlistener then it will not start the player until it is ready to play.
Therefore it is good to have onpreparedlistener so your activity does not crash or misbehaves.

Media Player set path of another folder

I am making a simple app that plays an mp3 file. I use the code
mp = MediaPlayer.create(MainActivity.this, R.raw.mysong);
But this works only if my song is saved in the res\raw folder of my PC. Sooner or later I will run the app of my iphone (as soon as I buy one!). What happens if I must set the path to the one that the mobile is saved? Let's say my Downloads folder from my mobile phone.
You can do it by giving it it's absolute path from the ExternalStorage as follwoing
mp = MediaPlayer.create(MainActivity.this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/mysong.mp3"));
OR Use setDataSource() method:
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
mediaPlayer.start()
String filePath = "somepath/somefile.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();

sounds not playing with mediaplayer after playing some sounds

I'm developing an android application which is collection of 100 sound effects.
After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop() method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer instance using the static create() method which means you create a new MediaPlayer object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.

One Android MediaPlayer Object plays several sounds at once

I have ONE object MediaPlayer mediaplayer. I use it to play different sounds, one after another.
mediaplayer = MediaPlayer.create(context, ResIdMusicONE);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
// some other user input
mediaplayer = MediaPlayer.create(context, ResIdMusicTWO);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
Sometimes is works fine. But sometimes the two sounds are played at the same time. And at positions, where mediaplayer should already have been released and be equal null.
Thanks for the help.
If I were you I would use the SoundPool class for this. With SoundPool you can set the number of streams to play at the same time, so by setting that to 1 you can just call play() over and over and the most recent call to play() will be the only sound that you hear.
Take a look at my post a while back. It has an example of the SoundPool class in the question.
Edit:
Have you tried creating a new instance and calling the prepare() every time you want to start a new sound?
mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
Though actually now that I think about it, I'm pretty sure you only need to do that if you are using a file from the sdcard not from your resources... Hmmm.

Categories

Resources