I have problem with using the same MediaPlayer more than once. I've tried mp.stop() but when i want again to start the same I'm using mp.start() but it wont want to play. Also I've tried mp.reset() and mp.prepare() ,than mp.start() but it wont want to play. So I I've done this:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.release();
// then again I'm creating
mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // second using, the same for more uses
I'm not sure if this is OK, but it works. Is this code good or I should use another solution?
Use the mp.pause() method instead of mp.release(). You can see the lifecycle of the mediaplayer here: http://developer.android.com/reference/android/media/MediaPlayer.html
Your code should look like this if you want to continue from where you paused:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.pause();
mp.start(); // second using, the same for more uses
If you want to play the same song, but start the song again:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.stop();
mp.prepare(); // second using, the same for more uses
mp.start();
If you wan to use another data source, the correct order is the following one:
mp.stop()
mp.reset();
mp.setDataSource(PATH);
mp.prepare();
mp.start();
Check the doc for more info about these calls.
Related
I have come across another error along my first app journey :) I want to play a sound when the app loads. Which is a .wav file. It lasts 2 seconds long yet it does not play when I run the app on my old Samsung S4. There is no errors within the IDE or anything I can see, I have checked if 'mp' has a value and it does. Looking around on posts most people have the problem that 'mp' is = null. Whereas mine has a value just no sound comes out of the phone... Again, any help is appreciated!
public class OpeningScreen extends Activity {
#Override
// create the screen state
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// connect the xml layout file
setContentView(R.layout.activity_opening_screen);
final MediaPlayer mp = new MediaPlayer();
mp.create(this, R.raw.welcome_message);
mp.start();
// create the on touch listener
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.opening_layout);
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// change the screen to a new state
Intent intent = new Intent(OpeningScreen.this, GameScreen.class);
// start the new activity
startActivity(intent);
// stop welcome sound (if still playing)
mp.stop();
return true;
}
});
}
}
public static MediaPlayer create(Context context, int resid) is a static method to create a MediaPlayer for a given resource id.
It means that by calling create you are creating a new instance of media player with no reference usage.
Try to change
final MediaPlayer mp = new MediaPlayer();
mp.create(this, R.raw.welcome_message);
to
final MediaPlayer mp = MediaPlayer.create(this, R.raw. welcome_message);
And the player should work.
It's better to register for OnPreapredListener via MediaPlayer.setOnPreaparedListener and after preparation you start your media playback.
http://developer.android.com/guide/topics/media/mediaplayer.html
Why do you use final?
You can play a mp3 with
MediaPlayer mp = MediaPlayer.create(OpeningScreen.this, R.raw.welcome_message);
mp.start();
Also stopping mediaplayer is better if you stop in onDestroy.
public void onDestroy() {
mp.stop();
super.onDestroy();
}
I am doing a little trivia game that plays a correct or wrong song when the user chooses an answer. This is my function:
public void playSound(boolean isRight) {
if(isRight){
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.correct);
mp.setVolume(VOLUME,VOLUME);
mp.start();
}else{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.wrong);
mp.setVolume(VOLUME,VOLUME);
mp.start();
}
}
My problem is that even though my audio is properly played, it has an extremly low volume on my device(which has maximun volume).
I already tried many solutions found here on Stackoverflow but the only one that seems to work is this: Android MediaPlayer not playing sound on some devices but it doesnt respect the user's volume.
Any ideas?
I made an android application where you can click buttons to play sounds. Every time, you click the button, a new MediaPlayer is getting created and the problem is, when you push a button during another sound is still playing, they are playing at the same time, but I want, that the previous sounds stops when you push a button. I already tried this code but my application crashes every time I press the button:
MediaPlayer mp;
Button button;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()){
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});
Is there a way to solve my problem?
It might be crashing because 'mp' is not initialized. Try this:
#Override
public void onClick(View v) {
//if mp exists and is playing...
if(mp != null && mp.isPlaying()){
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});
EDIT You could also avoid checking for null by initializing 'mp' when you declare it on top.
For instance, instead of checking if mp != null. Change your declaration of 'mp' from:
MediaPlayer mp;
to
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
and your onclick method could look like:
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
}
//start mp from beginning
}
It seems like starting too many MediaPlayer isn't good, because when i click the sounds a couple times, the app crashes. I already changed mp.start to mp.reset and release.
Notes
You have to release the player before creating a new one
I would not bet on isPlaying() check to stop the player; since you don't need it release it!
Code
#Override
public void onClick(View v) {
if(mp != null) {
mp.release();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.lastmohican);
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mp.isPlaying()) {
mp.stop();
} else {
mp.start();
}
}
}
I can play this music by one click and with another click music is stopped, but when I click together music does not start. Any idea why?
I'm not sure I understand your problem, but if you are wondering why the audio will not start back up after you press the button a third time, it is because you are calling stop() on the media player when you should be calling pause(). Stop requires you to prepare the media again before you can begin playback. Have a look at the MediaPlayer Documentation. If that is not your problem you need to provide a better description.
Its solved you should change code for this type
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
I am using an Android MediaPlayer to play from a local source. It is working well other than one bug when restarting the sound.
public void create() {
FileInputStream in = mApp.openFileInput(mMusicFile);
mp = new MediaPlayer();
mp.setDataSource(in.getFD());
mp.prepare();
mp.setLooping(true);
}
public void play() {
mp.start();
}
public void stop() {
mp.stop();
mp.prepare();
mp.seekTo(0);
}
If I call stop(), then a second or so later call play() I hear a brief clip of the sound where it had been stopped then it sound restarts.
I believe this is a known bug in Android 2.2 Froyo. The prescribed work around is to delete the MediaPlayer and make a new one or fade in to the playback.
The internal buffers are not being flushed after the seek.