While using fragment I'm trying to get audio file from raw folder like this:
mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(audioFile, "raw", getPackageName()));
I'm getting Cannot resolve method 'getPackageName()' error. How to solve this?
I was able to solve it by writing this code:
String path = "android.resource://"+"com.example.myproject"+"/raw/"+audioFile;
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getActivity(), Uri.parse(path));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
Related
I got a path of media file and want to play it.The problem is MediaPlayer.create() work successfully but when I use setDataSource,it's not working correctly, the media not play the music
player = new MediaPlayer();
String path =
"/storage/emulated/0/Music/AThousandYears_ChristinaPerri.mp3";
try {
player.setDataSource(getApplicationContext(),
Uri.parse(path));
player.start();
} catch (IOException e) {
e.printStackTrace();
}
I did not got any exception. Sorry about my bad english.I hope someone help.Thanks in advance
Try this
try{
mediaPlayer.reset();
mediaPlayer.setDataSource(songPath);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exception e){
//Handle Exception
}
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();
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'm done my Android app, just need to add some looping background music. Here is my method for playing the song
public void playAudio(){
path = "android.resource://" + getPackageName() + "/" + R.raw.music1;
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.setLooping(true);
mp.setVolume(100, 100);
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.d("NOTE WORKEING","NOT WORKING");
}
}
It's not working....It's going to catch everytime, and I don't know why. Please help me. Music1 is an mp3 file.
Thank you
Make sure your music1 file is in a Android playable Android format.
Then just use this code:
MediaPlayer mediaPlayer = MediaPlayer.create(YourActivity.this, R.raw.music1);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
mediaPlayer.start();
}
catch (Exception e) {
e.printStackTrace();
Log.d("NOT WORKEING","NOT WORKING");
}
You don't even need to call prepare() method.
I tested it with an mp3 file and it works perfectly.
If #Squonk's answer does not work, then the problem has to do with trying to start the music file before the MediaPlayer has prepared it. Try changing the MediaPlayer's start code to the following:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
}
});
Let me know if this helps, I have a good amount of experience with the MediaPlayer class and can help you troubleshoot.
Using mp.setDataSource(path) requires a valid filesystem path or URL.
In your case, path is a string representation of a Uri in which case you need to use a different approach.
Try setDataSource(Context context, Uri uri). You'll obviously need to provide a valid Context and parse your path variable into a Uri. Example...
mp.setDataSource(getApplicationContext(), Uri.parse(path));
Also change the path to...
path = "android.resource://" + getPackageName() + "/raw/music1";
I need your help. I tried to play an audio file stored in Assets folder but an error occurred.
Here are my code:
try{
if (player.isPlaying()) {
player.stop();
player.release();
}
}catch(Exception e){
Toast.makeText(this, "an exception occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try{
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets().openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
}catch(Exception e){
e.printStackTrace();
}
And here are my logcat:
06-16 22:39:53.330: W/MediaPlayer(13490): info/warning (1, 26)
06-16 22:39:53.330: E/MediaPlayer(13490): error (1, -4)
Could you please explain what's wrong with my code?
Thank you in advance
Regards,
Priska
This issue has been SOLVED.
The asset file descriptor must be closed before preparing the player. This is how I solved the problem:
player = new MediaPlayer();
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets()
.openFd("sounds/"+file);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
afd.close();**//just added this line**
player.prepare();
player.start();
Here you can see all Error codes Media player Error codes
-4 error code indicates you have given invalid arguments.
Put your code in try catch block.
Try Using
try {
AssetFileDescriptor afd = CustomListViewActivity.this.getAssets()
.openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Unfortunately there is very little information about MediaPlayer error codes available for some reason. However I suggest you try putting your sound file inside res/raw/ instead of your assets.
EDIT:
Start here with the Using the MediaPlayer section in the developer docs. This will show you how to set up and play the sound properly.
EDIT 2:
turns out that can do it from assets see this question: Play audio file from the assets directory
I don't think that wma files are supported.
http://developer.android.com/guide/appendix/media-formats.html
I noticed that you didn't specify the audioStreamType
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MISIC);
use this way it will solve your problem :)
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
}
}