Android MediaPlayer can't stop - android

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;
}
}

Related

Unable to stop mediaplayer

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(); }
}
}
}

Android: How can I prevent MediaPlayer from playing overlapping audio?

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.

How to define the button to play Z if Y is already playing?

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.

Android abandonAudioFocus doesn't trigger AudioManager.AUDIOFOCUS_LOSS

I abandon the audio focus in my onStop() method, but this doesn't lead to a loss of the focus in my OnAudioFocusChangeListener.
Abandoning doesn't even do any focus change.
What is my mistake?
How I get the focus:
private void startPlaying() {
int result = am.requestAudioFocus(afChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
mediaplayer.start();
if (am.isBluetoothA2dpOn()) {
// Adjust output for Bluetooth.
Log.i(AudioDemo.class.getName(), "Audio traget: BTHeadset");
} else if (am.isSpeakerphoneOn()) {
// Adjust output for Speakerphone.
Log.i(AudioDemo.class.getName(), "Audio traget: Speakerphone");
} else if (am.isWiredHeadsetOn()) {
// Adjust output for headsets
Log.i(AudioDemo.class.getName(), "Audio traget: WiredHeadset");
} else {
// If audio plays and noone can hear it, is it still playing?
Log.i(AudioDemo.class.getName(), "Audio traget: None");
}
isAudioPlaying = true;
tb = (ToggleButton) findViewById(R.id.toggle_audio);
tb.setChecked(true);
}
}
onStop():
#Override
protected void onStop() {
Log.i(AudioDemo.class.getName(), "onStop()");
// Abandon audio focus when playback complete, does nothing
int result = am.abandonAudioFocus(afChangeListener);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.i(AudioDemo.class.getName(), "Abandonning focus worked.");
} else {
Log.i(AudioDemo.class.getName(), "Abandonning focus failed.");
}
super.onStop();
}
My listener:
afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
Log.i(AudioDemo.class.getName(), "focusChange: " + focusChange);
if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|| (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
if ((mediaplayer != null) && (mediaplayer.isPlaying())) {
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because
// playback is likely to resume
mediaplayer.pause();
}
isAudioPlaying = false;
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
if (mediaplayer == null) {
mediaplayer = MediaPlayer.create(AudioDemo.this, R.raw.song);
}
if (!mediaplayer.isPlaying()) {
mediaplayer.start();
}
isAudioPlaying = true;
// Lost focus for an unbounded amount of time: stop playback and
// release media player
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(audioDemoReceiver);
am.abandonAudioFocus(afChangeListener);
if (mediaplayer != null) {
if (mediaplayer.isPlaying()) {
// Stop playback
mediaplayer.stop();
}
isAudioPlaying = false;
mediaplayer.release();
mediaplayer = null;
}
}
tb.setChecked(isAudioPlaying);
}
};
Greetings

How to play sound onclick of button in layout?

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();

Categories

Resources