I have a button - on press of that , i am playing a sound and moving to the next activity. The sound clip is of 4 secs. I want the sound to be fully played and THEN move to the next activity.
What is happening now is on click of the button the activity moves to the next and the sound is also played and it doesnt work as per my need.
My code
final ImageButton circleButton=(ImageButton)findViewById(R.id.circleCircle);
circleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
/* Code for playing the "Very Good" sound */
MediaPlayer mediaPlayer = MediaPlayer.create(shapeActivity.this, R.raw.wow);
mediaPlayer.start();
Intent myintent1 = new Intent(shapeActivity.this,shapeSquareActivity.class);
myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myintent1);
}});
Can someone help?
Use mediaplayer oncomplete listener method, once the sound is complete than move to next intent
mediaPlayer.setOnCompletionListener( new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Intent myintent1 = new Intent(shapeActivity.this,shapeSquareActivity.class);
myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myintent1);
}} );
Related
// This is my app exit button it destroys the activity and exits the user from an app. I want to //play a sound when I click this button but unfortunately, the sound is not playing and the activity //destroys. Please help me in solving this issue. Except for this button, all my button sound is working.
// This is my second activity, in this activity, I have used a button for quitting the app but I also //want the button to play a sound when the button is clicked by user. But it is not playing the sound and //quits the app which destroys activity. please tell me how to play the sound on my exit button during //the click
mExitApp = (Button)findViewById(R.id.exit_app);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample2);
mExitApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
ScoreActivity.this.finish();
System.exit(0);
}
});
A quick and dirty way would be to wait for the sound to end playing before finishing the activity/process.
public void onClick(View v) {
mp.start();
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
ScoreActivity.this.finish();
System.exit(0);
}
}, 300);
}
A more cleaner solution would probably involve a MediaPlayer.OnCompletionListener.
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
finish(); // finish current activity
}
});
Or you could play the sound in a foreground/background service. But then you shouldn't kill the entire process (System.exit(0)) like you do now ofc.
This question already has answers here:
Media Player start stop start
(8 answers)
Closed 6 years ago.
I have a button when clicked it plays an audio, and while it is playing I can pause it and replay it again and so forth. I have a shake event, where I want to play the audio on shake and replay it when device is shaken again (by first stopping the audio, calling stopAudio?)
I have the following code:
llBtn = (LinearLayout) findViewById(R.id.button);
llBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this, "LL WAS CLICKED", Toast.LENGTH_SHORT).show();
//IF AUDIO IS NOT PLAYING... PLAY AUDIO
if (tvPS.getText() == "Stop Phrase!") {
StopAudio();
}
else {
PlayAudio();
}
}
});
//the same button is clicked to stop, it is currently setting to Pause.
public void StopAudio() {
mediaPlayer.pause();
Toast.makeText(MainActivity.this, "STOPPED", Toast.LENGTH_SHORT).show();
tvPS.setText("Play Phrase!");
}
public void PlayAudio() {
//stopPlaying(mediaPlayer);
tvPS.setText("Stop Phrase!");
inResId = getResources().getIdentifier("play" , "raw", getPackageName());
mediaPlayer = MediaPlayer.create(getBaseContext(), inResId);
mediaPlayer.seekTo(0);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
tvPS.setText("Play Phrase!");
mediaPlayer.pause();
}
});
}
...
public void onShake() {
// Do stuff!
Toast.makeText(getBaseContext(), "Motion detected",
Toast.LENGTH_SHORT).show();
if (mediaPlayer == null) {
PlayAudio();
}
}
How can I modify the code above to handle Play, and then stop if stopped in the middle and then able to replay it again.
The button click works but I am creating a new instance each time and not recycling. When the device is shaken, it plays the audio twice instead of just once.
First of all create the MediaPlayer in onCreate of the context and not in some listener, and don't forget to release it when done, since it consumes a lot of resources,
mediaPlayer.release();
mediaPlayer = null;
Next thing, stop the audio using the stop() function instead of the pause() on completion of the song.
Steps
If you are using a service/activity to play a video create a MediaPlayer instance before it's usage begins, likely in onCreate.
Later use that instance to play/pause/stop the media.
In the onStop of the Service/Activity release the MediaPlayer instance.
Update your playMusic() function to use the MediaPlayer instance you just created, also in the listener use stop() instead of pause()
To change track of an existing MediaPlayer instance
You can use this:
String path = getExternalFilesDir(null).toString()+"/";
mMediaPlayer.setDataSource(path + mediafile);
Link: Changing data source for audio playback using existing MediaPlayer?
When I touch sound play sound but when I touch another it stop play this sound. I need to sound don't stop playing.
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button SoundButton2 = (Button)findViewById(R.id.sound2);
SoundButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
Button SoundButton3 = (Button)findViewById(R.id.sound3);
SoundButton3.setOnClickListener(new OnClickListener() {`enter code here`
public void onClick(View v) {
mSoundManager.playSound(3);
Your code in my comment shows that you have initialized your sound pool with 1 as the value for maxStreams. If you set this value to the maximum amount of sounds you want to be able to play at the same time, it should work.
You have mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
If you change that to mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
You will be able to play 5 sounds at once.
I'd need to take a look at your sound manager class to have a better idea of what is wrong with it. However, it is perfectly possible.
Take a look at this:
Playing sounds simultaneously Android
In Adnroid, at first i declare the mediaplayer by
MediaPlayer mpl;
next I have this in the onCreate method
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.hit );
mp.setVolume(1, 1);
and a function that's supposed to play a sound when called
public void click()
{
mp.start();
}
yet the problem is that if the user calls this function multiple times, before it has stopped playing the last sound, it will die and stop playing any sounds, before the app is reset.
Any ideas how to fix that?
Thanks!
edit - found a solution:
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpl.release();
}
});
public void click()
{
if( ! mp.isPlaying() ) {
mp.start();
}//if
}//met
you can disable the button.
or you can stop current playing and star
new in onClick()
I am currently working on an application for Android and I'm using Eclipse and of course Android SDK, but I have bumped in to a problem that is almost the only thing I need to fix before I can relaese a beta-version to Android Market.
So, my problem is that I have an xml with 4 different buttons, and if the user press on a certain button, one sound will be played, and if the user press any of the other buttons is pressed another sound will play but the sound only play sometimes, I want it to play every time the user press a button.
Here's my code(concerning the mediaplayer):
public MediaPlayer right=null;
public MediaPlayer wrong=null;
if(right!=null) {
right.reset();
right.release();
}
if(wrong!=null) {
wrong.reset();
wrong.release();
}
right = MediaPlayer.create(getBaseContext(), R.raw.rightsound);
wrong = MediaPlayer.create(getBaseContext(), R.raw.wrongsound);
if(****()){
right.start();
}
else {
wrong.start();
}
That's my code and I would be very grateful if somebody could help me solve my problem.
new Thread() {
public void run() {
int sound = R.raw.wrongsound;
if(****()) {
sound = R.raw.rightsound;
}
mp = MediaPlayer.create(Test.this, sound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
}.start();