Hi there I'm an android and java newbie and is wondering if there is a better way of doing the following:
I currently have 250 short audio files (.3gp) that i put inside my res/raw folder and based on a certain condition, I want to play certain audio file. For example if the text of the button that is being clicked is the word "and", I want to play the and.3gp sound file.
The following is the code that I use to accomplish what I want to do:
if(word.equals("No"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.no);
mp.start();
mp.setOnCompletionListener(listener );
}
else if(word.equals("And"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.and);
mp.start();
mp.setOnCompletionListener(listener );
}
.....
The above code is working fine so far but I'm wondering if I can do this without having a 250 else if condition. Is there a way to pass in the audio file to MedialPlayer.Create by file name via Uri? If yes, how do I do it? Thank you.
You can make a hash function that maps a string key to the uri
Loop through all your files in your raw folder and remove the file extension, then test against that.
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
I know there are lots of similar problems to mine, but for some reason the answers to those questions don't seem to work. I am using android and have a sound file named 'backgroundbeat.wav' and I'm trying to import it but instead I get the error:
"failed to open file 'res\raw\backgroundbeat.wav'. (No such file in directory)"
Here is the code where I use the media player:
mp = new MediaPlayer();
try{
mp.setDataSource("res/raw/backgroundbeat.wav");
mp.prepare();
mp.start();
}catch(Exception e){
e.printStackTrace();
}
Use your app package name to define the correct path :
mp.setDataSource("android.resource://<YOURPACKAGE_NAME>/raw/backgroundbeat");
example:
mp.setDataSource("android.resource://com.jorgesys.myapp/raw/backgroundbeat");
or other way to load a media file from /raw folder, if you are inside an activity you could use:
mp = MediaPlayer.create(this, R.raw.backgroundbeat);
or You can use a VideoView to play your file
Videoview on splash screen
Not much experienced in this but you can try to use mediaPlayer.setDataSource(FileDescriptor fd) instead of mediaPlayer.setDataSource(String path). See MediaPlayer setDataSource, better to use path or FileDescriptor? . Apparently if you call prepare() while using string as a parameter to setDataSource, it is causing some issues as per the answers.
You may try something like:
// Use the proper context instead of *context* variable
InputStream in = context.getResources().openRawResource(R.raw.backgroundbeat);
mediaPlayer.setDataSource(in.getFD())
Let me know if it helps.
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'm new in Java/Android programming, so please have patience with me.
I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.
This code works to play a mp3-file:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.
I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.
Have someone an idea about this problem?
I don't believe using FileDescriptor is the right way to do this. Try using .create() and a Uri instead:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), songUri);
mp.start();
Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:
Play audio file from the assets directory