I'm developing an app where I have a Fragment for a Level Complete and I want to play a sound when this Fragment loads.
I think that I have to write the MediaPlayer in the onCreate method but I don't know exactly how to write the MediaPlayer code.
I've already loaded the mp3 file in the Raw Folder
MediaPlayer player = MediaPlayer.create(Activity.this, R.raw.some_file);
player.start();
Related
I am new to android. I am going to develop a simple mp3 application. There will be some mp3 tones in my app. I want to play the tones on click in the list item. Now my question is Which is the best way to store the mp3 files in my app.
1) SQLite
2) XML
3) folder
Please let me some good idea to me.
Thanks in advance.
You have to store them in a folder. There must be any resource the sqlite entrys refer to, because this is just a database. So storing them in a folder is not a question I think...
And XML is used to define UI Things like the interface.
This is because of the Model-View-Controller pattern if you want to know more about that look here
If you safed it in the raw folder(it´s located in the res folder, if you dont have one-> create one) of your project than this should be suitable for you
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
If you got it somewhere else this should fit your needs
Uri myUri = Uri.parse("/path/to/file.mp3"); // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
For more informations about MediaPlayer see here and here
How do I initialize MediaPlayer in Android without an R.raw or some kind of file?
If I don`t initialize MediaPlayer with some file I will get a null pointer exception at runtime. But when the program is started there is no file path to use because the user has not yet pressed a button to select the mp3 file from the SD card.
When initialzing the Media Player there is another probem. It only takes an R.raw type file, not the path of a file from the SD card. And if I don't have any file in the R.raw directory then I can`t initialize with a file. It appears that you need an audio file in the local R.raw folder to do this.
Mediaplayer player = MediaPlayer.create(this, R.raw.sample_music);
There is a method called "setDataSource" that allows me to set the path of the file, however I have to initialize the MediaPlayer first.
player.setDataSource(selectedAudioPath);
Any other way to initialize MediaPlayer?
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filepath);
This post helped me and the code below works as well:
Mediaplayer player = MediaPlayer.create(this, Uri.parse(File.getAbsolutePath()));
I'm from Taiwan and my English is pretty basic, thanks for your forgiveness.
I built a iPad app and published to App Store, which is a kind of communicating board (AAC for the person who has difficult on speaking). It has several buttons on main view and when user touch up one, the app will play a mp3 sound corresponding to that button. I've put all mp3 files on a folder and determining which one to play dynamically at run-time.
Recently I'm working on build the Android version of this app, but I meet a problem that is when I touch up one button on the view, the MediaPlayer play not only the corresponding one but keep playing all the rest mp3 files on the folder continuously. It doesn't happen on the development of iPad version. Any one can help me to resolve this problem and set the MediaPlayer up to working appropriately?
PS. I put all MP3 files on some sub-folders and put them on the "assets" folder. It because for some reasons, all mp3 files are using numeric file name (ex. 0123223.mp3), and which MP3 to play is according to which button user touched up, so I can't use R.raw.0123223 not only because the R class can't has a member named in numeric, but also the target mp3 file can only determining at run-time instead of compile-time, so I can't use the res/raw folder.
My code is as following:
private void PlayMP3(String mp3File) throws IOException {
AssetFileDescriptor afd = this.getAssets().openFd(mp3File);
MediaPlayer player = new MediaPlayer();
player.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
System.out.println(mp.getAudioSessionId() + " finished.");
}
});
player.setDataSource(afd.getFileDescriptor());
player.setLooping(false);
player.prepare();
player.start();
}
the parameter "mp3file" of PlayMP3 is a concatenated string of mp3 file path, according to the button user touched up.
Many thanks!
Your version would work if you had only that one file in the assets directory. The asset directory contents are put together one after another. So, if you do not specify where to start and how many bytes to read, the player will read up to the end as if they are one file.
You can accomplish what you want by using setDataSource with more arguments:
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
This will allow you to define the limits of the player and only play the song you specify in your string mp3File.
You did not post some code.
First step is to insert your sound file into the raw folder
Then, you need only a few lines of code:
MediaPlayer mediaPlayer = null;
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.mysoundfile);
mediaPlayer.start();
When the application is closing, dont forget to "release" your media player:
mediaPlayer.release();
mediaPlayer = null;
I'm trying to run a mp3 file using android media player. I'm using this code below
MediaPlayer mediaPlayer = MediaPlayer.create(this, "mmt/sdcard/mp3/file.mp3");
mediaPlayer.start();
But Ecliplse showing error & it says I can't use string as argument of create method. How can I run the file using this file location?
Plz help
you should use setDataSource to set the source of your media files. For files on the sdcard, the setDataSource(String ds) works best.
Also, note that your url should probably be mnt instead of mmt, and you need a forwardslash at the beginning, making your full url look like this: /mnt/sdcard/mp3/file.mp3
I am developing a game application in android. I have designed all the views and implemented all the functionality. Now in the last screen I have to play sounds in android. Can anybody tell me how to pursue with it?
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1);
mp.start();
And then you get all the start/stop/reset/pause/release methods from mp.
Just place the sound file in /res/raw (after creating the folder) and then use MediaPlayer to init, start and then stop playing the sound. MediaPlayer documentation can be found here.
HTH,
Sriram.
I would suggest SoundPool for seamless playback in android because
mediaPlayer first loads the whole sound data in memory than play, so it produces some lag when we switch among sounds frequently.
SoundPool is a better option with small size sound file, and produces best result with .ogg media file.
SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound, 1);
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
}
From Android Developer page: http://developer.android.com/guide/topics/media/index.html
Playing from a Raw Resource Perhaps
the most common thing to want to do is
play back media (notably sound) within
your own applications. Doing this is
easy:
Put the sound (or other media
resource) file into the res/raw folder
of your project, where the Eclipse
plugin (or aapt) will find it and make
it into a resource that can be
referenced from your R class Create an
instance of MediaPlayer, referencing
that resource using
MediaPlayer.create, and then call
start() on the instance:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
To stop playback, call stop().
If you wish to later replay
the media, then you must reset() and
prepare() the MediaPlayer object
before calling start() again.
(create() calls prepare() the first
time.)
To pause playback, call pause().
Resume playback from where you paused
with start().
Playing from a File or Stream You can
play back media files from the
filesystem or a web URL:
Create an instance of the MediaPlayer
using new Call setDataSource() with a
String containing the path (local
filesystem or URL) to the file you
want to play First prepare() then
start() on the instance:
Like this
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
mp.start();
By using Media Player you can achieve this.
here is the steps::
1.- Create a new folder called "raw" inside "debug" folder. To create it right clic on "debug" folder > new > directory > and in name it "raw" :
To add files just drag the .wav/.mp3 files to "raw" folder.
2.- Import media player:
import android.media.MediaPlayer;
3.- Declare MediaPlayer global variable(s):
public MediaPlayer mp1;
4.- Inside onCreate method, asign the corresponding sounds:
mp1 = MediaPlayer.create(MainActivity.this, raw.my_sound_name);
5.- Finally, you can use methods...
mp1.start() / mp1.stop() / mp1.pause()