playing a MP3 from raw folder? - android

im trying to play a MP3 from raw folder .i have different buttons which playing different MP3's so i must change the mediaplayer input every time here is my code on button click:
public void onClick(View v) {
t=R.raw.virtualbarber
playsound(t);
}
and here is my mediaplayer code which im taking error in .create ! :
private void playSound(string t ){
mp = MediaPlayer.create(getActivity(), t);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.setLooping(true);
mp.start();
}}
im also declared my string and mediaplayer on top of my project like this :
MediaPlayer mp;
int t;
nothing works ! my other program worked correctly but this doesn't !
logcat
01-17 22:01:35.016: E/AndroidRuntime(30521): FATAL EXCEPTION: main
01-17 22:01:35.016: E/AndroidRuntime(30521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.safshari.board3d/com.safshari.board3d.MainActivity}: java.lang.NullPointerException
01-17 22:01:35.016: E/AndroidRuntime(30521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
01-17 22:01:35.016: E/AndroidRuntime(30521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)

Use
mp = MediaPlayer.create(getActivity(), R.raw.virtualbarber);
If it is fragment instead of this use getActivity().
mediaplayer fixed tnx to you but now im getting nullpointer exception on start of program ?
If its still crashing post the updated relevant code along with stacktrace for further help

Use the uri something like:
Uri myUri = Uri.parse("android.resource://com.package.sample/raw/filename");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), myUri);
mp.prepare();

Related

Android MediaPlayer.start does not start

I have yet to find an answer to this.
I have a local file (R.raw.Bob); and I am trying to use MediaPlayer to play the file.
Sometimes it plays, sometimes it does not. I have another file which plays seemingly fine every time.
My activity flow is like this: In onCreate I do the following:
MediaPlayer mBackground = MediaPlayer.create(MainAct.this, R.raw.background);
mBackground.start(); // Works as expected.
Now in a different part of the activity I have the following:
MediaPlayer mBob= MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.start();
And nothing occurs. I have used Log.i() and the execution goes through the relevant code but the file does not start.
Why does MediaPlayer sometimes work and sometimes does not, and is there a more reliable way of playing sound files?
Try this to start:
MediaPlayer mBob = MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
mp.start();
}
});
and this to stop:
mBob.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});

IllegalStateException while trying to set 3rd player via setNextMediaPlayer?

There are 3 MediaPlayers and I want to set that these 3 sounds play seamlessly using setNextMediaPlayer(). The code is simple (repeated code is omitted)
mediaPlayer1 = new MediaPlayer();
try {
AssetFileDescriptor afd = getAssets().openFd("sound1.ogg");
mediaPlayer1.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer1.prepare();
afd.close();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer1.start();
mediaPlayer2 = new MediaPlayer();
//Prepare 2nd player with sound2 without calling start()
mediaPlayer3 = new MediaPlayer();
//Prepare 3rd player with sound3 without calling start()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mediaPlayer1.setNextMediaPlayer(mediaPlayer2);
mediaPlayer2.setNextMediaPlayer(mediaPlayer3); //ERROR RUNTIME!!!
And here I get an error
E/MediaPlayer﹕ next player is not prepared
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4175fda0)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testgaplesssound.app, PID: 28960
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.IllegalStateException at...
The error say MediaPlayer﹕ next player is not prepared so I tried setting onPrepared listener
mediaPlayer2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mediaPlayer2.setNextMediaPlayer(mediaPlayer3);
}
}
});
but onPrepared is never initiated!
So, how can I sent the 3rd player to play seamlessly as the first two?
I just tested the following setup with no errors, thinking your problem was with initialization. But then I noticed that you're starting your first player before initializing and chaining the other two. Reordering your code might solve your problem, but I'll post this as well, as it seems a simpler solution, to me, at least.
Move your audio files to the /res/raw folder, and initialize and start your players like so:
MediaPlayer mediaPlayer1, mediaPlayer2, mediaPlayer3;
mediaPlayer1 = MediaPlayer.create(this, R.raw.sound1);
mediaPlayer2 = MediaPlayer.create(this, R.raw.sound2);
mediaPlayer3 = MediaPlayer.create(this, R.raw.sound3);
mediaPlayer1.setNextMediaPlayer(mediaPlayer2);
mediaPlayer2.setNextMediaPlayer(mediaPlayer3);
mediaPlayer1.start();
NB: I tested under API 19, so you'll still need your version checks, if you're back-supporting.

Error (-38.0) on Media Player

I am making a simple media player application. It involes a textfield (et) where user just enters the exact name of the song to be played and presses the play button (ib1) to be played. Songs are saved inside sdcard. My code is:
ib1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String value = et.getText().toString();
String full_path = "/mnt/media_rw/sdcard/mymusic/" + value + ".mp3";
et.setText("");
mp = new MediaPlayer();
mp.setDataSource(full_path);
mp.prepare();
mp.start();
} });
But this produces Media Player error (-38.0). So, following Media Player called in state 0, error (-38,0) I tried to replace the mp lines with:
mp.setDataSource(full_path);
mp.setOnPreparedListener(null);
mp.prepareAsync();
mp.start();
But it won't work either, producing error (1, -2147483648). Can someone help me or make some suggestions for the code? Thanks a lot
You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mp.start() before it has reached the prepared state.
Here is how you can do it :
mp.setDataSource(full_path);
mp.setOnPreparedListener(this);
mp.prepareAsync();
public void onPrepared(MediaPlayer player) {
player.start();
}

Android MediaPlayer stops playing after some time

Having small game to play short sound after onclick ImageButton. But after touching 10times MediaPlayer stop playing sounds on short time. After sometime it play sound again. When I look to LogCat console it show error: E/MediaPlayer(19584): error (1, -2147483648).
Please can you show me the way to find sollution to solve this problem ? Why MediaPlayer gives me error ?
I use this part of code to play Sound:
public void playAudio () {
try {
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.trefa);
mediaPlayer.setLooping(false);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
}
});
} catch (Exception e) {
Log.e("beep", "error: " + e.getMessage(), e);
}
}
The sollution for me is really to use SoundPool (not MediaPlayer). I have replaced my MediaPlayer with SoundPool from this tutorial: http://www.edumobile.org/android/android-programming-tutorials/sound-pool-example-in-android-development and everything is ok.

Mediaplayer error (-19,0) after repeated plays

I have a game in which a sound plays when a level is completed. Everything works fine to start with but after repeating a level 10 or 20 times the logcat suddenly reports:
"MediaPlayer error (-19,0)" and/or "MediaPlayer start called in state 0" and the sounds are no longer made.
I originally had the all sounds in mp3 format but, after reading that ogg may be more reliable, I converted them all to ogg, but the errors appeared just the same.
Any idea how I can fix this problem?
I was getting the same problem, I solved it by adding the following code to release the player:
mp1 = MediaPlayer.create(sound.this, R.raw.pan1);
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
I think you are not releasing the mediaplayers you are using to play the sound..
You need to release() the media players otherwise the resources are not released , and you soon get out of memory (since you allocate them again next time). so,I think you can play twice or even thrice... but not many times without releasing the resources
MediaPlayer is not a good option when you are playing small sound effects as the user can click on multiple buttons very soon and you will have to create a MP object for all of them which doesnt happen synchronously. That is why you are not hearing sounds for every click. Go for the SoundPool Class which allows you to keep smaller sounds loaded in memory and you can play them any time you want without any lag which you would feel in a mediaplayer. http://developer.android.com/reference/android/media/SoundPool.html Here is a nice tutorial : http://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html
I solved both the errors (-19,0) and (-38,0) , by creating a new object of MediaPlayer every time before playing and releasing it after that.
Before :
void play(int resourceID) {
if (getActivity() != null) {
//Using the same object - Problem persists
player = MediaPlayer.create(getActivity(), resourceID);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
}
After:
void play(int resourceID) {
if (getActivity() != null) {
//Problem Solved
//Creating new MediaPlayer object every time and releasing it after completion
final MediaPlayer player = MediaPlayer.create(getActivity(), resourceID);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
}
This is a very old question, But this came up first in my search results So other people with the same issue will probably come upon this page eventually.
Unlike what some others have said, you can in fact use MediaPlayer for small sounds without using a lot of memory. I'll put in a little modified snippit from my soundboard app to show you what I'm getting at.
private MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mp = new MediaPlayer();
}
private void playSound(int soundID){
mp.reset();
AssetFileDescriptor sound = getResources().openRawResourceFd(soundID);
try {
mp.setDataSource(sound.getFileDescriptor(),sound.getStartOffset(),sound.getLength());
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
with the way I set it up, you create on MediaPlayer object that you reuse everytime you play a sound so that you don't use up too much space.
You call .reset() instead of .release() because .release() is only used if you are disposing of an object, however you want to keep your MediaPlayer Object.
You use an assetfiledescriptor to set a new soundfile for your mediaplayer to play instead of setting a new object to your mediaplayer address because that way you are creating new objects within the method that aren't being handled properly and you will eventually run into the same error as you described.
This is only one of many ways to use MediaPlayer but I personally think it is the most efficient if you are only using it for small sound applications. The only issue with it is that it is relatively restrictive in what you can accomplish, but that shouldn't be much of an issue if you are indeed using it for small sound applications.
i try delete emulator and new create emulator for remove error of (-19,0) media player.

Categories

Resources