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
Related
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.
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()));
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.
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
I added some audio files to the res file.
first I created a raw dir and then I added the folder with the audio files.
How can I get each file path? (I want to use the path to play this audio files)
I found that I need to use
classLoader.getResource
but I don't understand how. what is the argument for this function?
A reference should be added to your R class that you can get as something like R.raw.sound_file_1. Check out the Audio and Video section of the Android Developer's Guide for more details.
Once you have the reference to your audio clip you can play it by:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
So you don't really need the path to the resource at all, just use the reference.