I'm building a thug life sound effect player and I wanted to know how can I stop the MediaPlayer from playing 2 or more tracks the same time.
This is the method that's called when a button is clicked:
int sequence = 1;
public void thugPlay (View view) {
// assign a media player to an audio file
MediaPlayer nothingButThatGthang = MediaPlayer.create(MainActivity.this, R.raw.g_thang);
MediaPlayer nextEpisode = MediaPlayer.create(MainActivity.this, R.raw.next_episode);
MediaPlayer fuckThePolice = MediaPlayer.create(MainActivity.this, R.raw.ftp);
MediaPlayer hipnotize = MediaPlayer.create(MainActivity.this, R.raw.hipnotize);
MediaPlayer moveBitch = MediaPlayer.create(MainActivity.this, R.raw.move_bitch);
MediaPlayer ridin = MediaPlayer.create(MainActivity.this, R.raw.ridin);
MediaPlayer still = MediaPlayer.create(MainActivity.this, R.raw.still);
// Plays according to sequence's value
if (sequence == 1) {
still.stop();
nothingButThatGthang.start();
sequence++ ;
}
else if (sequence == 2) {
nothingButThatGthang.stop();
nextEpisode.start();
sequence++ ;
}
else if (sequence == 3) {
nextEpisode.stop();
fuckThePolice.start();
sequence++ ;
}
else if (sequence == 4) {
fuckThePolice.stop();
hipnotize.start();
sequence++ ;
}
else if (sequence == 5) {
hipnotize.stop();
moveBitch.start();
sequence++ ;
}
else if (sequence == 6) {
moveBitch.stop();
ridin.start();
sequence++ ;
}
else {
ridin.stop();
still.start();
sequence = 1 ;
}
}
As you can see, before playing the current audio file it should stop the previous one, however, it doesn't happen.
Thank you!
Create one MediaPlayer variable as class member:
MediaPlayer mp;
Then in your click event:
if (mp != null && mp.isPlaying()){
mp.stop();
}
if (sequence == 1) {
mp = MediaPlayer.create(MainActivity.this, R.raw.still);
mp.start();
sequence++;
}
You should off-course complete the If statment.
Related
I've two radio buttons in a radiogroup and on selection of one radio button it will start music and stop the other sound and on selection of other it will start the next song and stop the previous song. The problem is I'm unable to stop the music. The song keeps on playing in the background. I'm using dialog box to show radio buttons and getting the input from user on button'c click.
The length of each song is greater than 3 minutes.
I've searched the internet and tried multiples solution but no solution is working. Below is the code I've used:
btnDone.setOnClickListener(view.onClickListener)
{
#Override
public void onClick(View view)
{
boolean checked = radioButton1.isChecked();
boolean checked1 = radioButton2.isChecked();
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song1);
MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.song2);
if(checked && !checked1)
{
mediaPlayer.start();
}else if(checked1 && !checked)
{
try{
if(mediaPlayer !=null)
{
if(mediaPlayer.isPlaying())
{mediaPlayer.stop;
mediaPlayer.release;
mediaPlayer = null;
}
}
}
catch(Exception e)
{e.printStackTrace(); }
}
}
}
IMO your else if(checked1 && !checked1) condition not working properly so try else if(checked1 && !checked) try below code
btnDone.setOnClickListener(view.onClickListener)
{
#Override
public void onClick(View view)
{
boolean checked = radioButton1.isChecked();
boolean checked1 = radioButton2.isChecked();
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song1);
MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.song2);
if(checked && !checked1)
{
mediaPlayer.start();
}else if(checked1 && !checked) //change here
{
try{
if(mediaPlayer !=null)
{
if(mediaPlayer.isPlaying())
{mediaPlayer.stop(); // also change here
mediaPlayer.release(); //change here
mediaPlayer = null;
}
}
}
catch(Exception e)
{e.printStackTrace(); }
}
}
}
I'd just like to stop my MediaPlayer file and I really don't understand why is that so complicated. I have this method to start to music:
public void startMusic() {
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
mediaPlayer1.start();
mediaPlayer1.setVolume(0.2f, 0.2f);
mediaPlayer1.setLooping(true);
play = true;
}
And I'd like to stop music with this method:
public void stopMusic(){
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
if(play){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
When I call the startMusic method the music starts but when I call the stopMusic method then nothing happens.If anyone knows how to do that please response.
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
On this line you are creating a new MediaPlayer object and you lose the old object reference (it will continue playing until gc). Use same object, as listed below
public void stopMusic(){
if (mediaPlayer1 != null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
}
}
You are again creating MediaPlayer instance and stopping the new instance. But what you need to do is to stop the existing MediaPlayer instance which is running and playing audio. Change your stopMusic() -
public void stopMusic(){
if(play && mediaPlayer1 != null){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
the idea is that i have 1 button and 2 states.
1. On press is playing sound Z (from a random sound list)
2. On second press: if Z.isPlaying play Y
All seems fine, till i press it 3 times in a row, and get a nullpointerexception
Here is the onClick code:
#Override
public void onClick(View view) {
if (view.getId() == R.id.goat) {
if(Piciu != null) {
if(mp2.isPlaying()){mp2.stop();mp2.reset();mp2.release();mp2 = null;}
butGoat.setBackgroundResource(R.drawable.goat96);
mp3 = MediaPlayer.create(this, R.raw.cow);
mp3.start();
butGoat.setBackgroundResource(R.drawable.goat96);
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp3) {
butGoat.destroyDrawingCache();
butGoat.setBackgroundResource(R.anim.clipeala);
yourAnimation = (AnimationDrawable) butGoat.getBackground();
yourAnimation.start();
Piciu = null;
}
});
toast.setText("I can`t yell so Fast!");
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
} else {
Piciu = mp2;
butGoat.destroyDrawingCache();
butGoat.setBackgroundResource(R.drawable.goat96);
Random r = new Random();
int Low = 0;
int High = 16;
int rndm = r.nextInt(High-Low) + Low;
mp2 = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp2) {
butGoat.setBackgroundResource(R.anim.clipeala);
yourAnimation = (AnimationDrawable) butGoat.getBackground();
yourAnimation.start();
mp2.reset();
mp2.release();
mp2 = null;
Piciu = null;
}
});
}
}
}
i just can't understand Why do i get nullpointerexception if Sound Z is not playing.
I see you didn't check for mp2 != null before calling mp2.isPlaying():
if (view.getId() == R.id.goat) {
if(Piciu != null) {
if(mp2.isPlaying()){mp2.stop();mp2.reset();mp2.release();mp2 = null;}
change it to if (Piciu != null && mp2 != null) Also not sure why you need the Piciu variable at all. I'd clear up the logic first, then cleanup the code if it's still crashing.
I have a code fragment in my BrocardcastReceiver like this:
if (cmd != null && cmd.equals(PLAY_SOUND)) {
Log.i("mp", "mp has already been prepared ");
mp.setLooping(true);
mp.start();
return;
}
if (cmd != null && cmd.equals(STOP_SOUND) && mp != null) {
if (mp.isLooping()) {
mp.setLooping(false);
}
mp.stop();
mp.release();
return;
}
It aims to control alarming or stop alarming. I can make mp run successfully, but when I want to stop it, mp is null. How can I stop mp from being changed?
try this
in your code
stopPlaying();
and declare this methode .
private void stopPlaying() {
if (player1 != null) {
player1.stop();
player1.release();
player1 = null;
}
}
I have a layout in which multiple button is showing.onclick of button one sound play respectively.My code is like as but its not work:-
#Override
public void onClick(View view) {
resetPlayer();
int index = 0;
if (view.equals(objbutton1))
index = R.raw.sound1;
while (true) {
this.objplayer = MediaPlayer.create(this, index);
this.objplayer.setLooping(false);
this.objplayer.start();
this.objplayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
MainActivity.this.objplayer.release();
MainActivity.this.objplayer = null;
}
}
);
if(view.equals(objbutton2))
{
index=R.raw.sound2;
continue;
}
if(view.equals(objbutton3))
{
index=R.raw.sound3;
continue;
}
if(view.equals(objbutton4))
{
index=R.raw.sound4;
continue;
}
if(view.equals(objbutton5))
{
index=R.raw.sound5;
continue;
}
break;
}
}
private void resetPlayer() {
if (this.objplayer != null) {
if (this.objplayer.isPlaying())
this.objplayer.stop();
this.objplayer.release();
this.objplayer = null;
}
App is playing only one sound and when click other button no sound be changed
Use this
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
Like...
private MediaPlayer mp; // declare it in public area.
public void onClick(View view) {
int index = 0;
if (view.equals(objbutton1)){
index = R.raw.sound1;
if(mp !=null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
}
if(view.equals(objbutton2))
{
index=R.raw.sound2;
if(mp !=null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
}
if(view.equals(objbutton3))
{
index=R.raw.sound3;
if(mp !=null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
}
if(view.equals(objbutton4))
{
index=R.raw.sound4;
if(mp !=null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
}
if(view.equals(objbutton5))
{
index=R.raw.sound5
if(mp !=null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(getApplicationContext(), index);
mp.start();
}
}
And in onDestroy()put this mp.release();