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();
How can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance
I want to play a sound automatically when I call to somebody
but I don't know the way for this.
It is possible because we have some application that play some noise (traffic and ...) to background of call but I could not find a code or something for this
example of sound play in background
this is a code to play music
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd(“home.mp3″);
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(true);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
but can we use this during call?
I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?
Does anybody know what does the second argument mean from this error (1, -2147483648) in MediaPlayer? I constantly receive it when attempting to play an audio from a url stream. I try to play it from a class that extends BaseExpandableListAdapter if that matter. I have already reviewed this post Android MediaPlayer error: MediaPlayer error(1, -2147483648) on Stream from internet however all the answers refers to the stream support issues. It's not a stream support issue in my case since I am able to play an audio from the same stream but just using a different class. This is a method that I'm using for playing:
private void startPlaying(String fileName) {
mediaPlayer = new MediaPlayer();
try {
if (fileInputStream != null) { // Read a file from a fileInputStream from a filesystem (EXTERNAL OR INTERNAL storage)
mediaPlayer.setDataSource(fileInputStream.getFD());
Log.d("MediaPlayer is playing", "from device");
} else {
// mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getFilePath()); // Read a file from a url
Log.d("MediaPlayer is playing", "from stream");
}
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new CompletionListener());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
The error is usually one of these in this case:
File path is in error. Incorrect directory or Url or Uri found.
Media file is in error, incompatible format.
Missing permissions
Here's a good blog that outlines these situations and how to fix them:
http://www.weston-fl.com/blog/?p=2988
Also see this thread:
Android mediaplayer MediaPlayer(658): error (1, -2147483648)