ImageButton background not changing - android

on clicking the play button if music is playing the music should pause and image of button should change to pause button and vice versa
I dont know whats wrong with the logic please check.
public void onClick(View v) {
switch (v.getId())
{
case R.id.mpPlay: // bPlay
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
bPlay.setBackgroundResource(R.drawable.play);
}
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
bPlay.setBackgroundResource(R.drawable.pause);
}
break;
}

Try to use
setImageResource(id);
or
setImageDrawable(ContextCompat.getDrawable(context, id));

You should replace your second "if" statement with an "else". When your code is executed, the media player is not playing so your first "if" statement gets executed. This turns on your media playback. However, when your code gets to your second "if" statement, your media will be playing, so it immediately turns back off. Replacing your second "if" statement with an "else" will ensure that only one of the options (turn media playback on or turn media playback off) will be executed on any given onClick().
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
bPlay.setBackgroundResource(R.drawable.play);
}
else
{
mediaPlayer.pause();
bPlay.setBackgroundResource(R.drawable.pause);
}

As I understood
ImageButton bPlay = (ImageButton)findViewById(R.id.mpPlay);
//mpPlay is id of your ImageButton for play button in xml file
is Ok.
So, try this:
public void onClick(View v){
switch (v.getId()){
case R.id.mpPlay:
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
bPlay.setImageResource(R.drawable.pause);
//pause icon during player plaing
} else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
bPlay.setImageResource(R.drawable.play);
//play icon during player on pause
}
break;
}
}
In some way, it's logically

Related

Overlapping two sounds in spinner

I'm having trouble with the following:
I have a spinner with different songs in it. In the spinner, users can preview the selected sound. I already developed that part. But my problem is: when I select one song from the list it will play. Then when we select another song from the list, Mediaplayer doesn't stop and plays the previous song also. But what I need is to stop the previous song when a user selects another song.
Here is my code...
//set onClickListner to the onItem SelectedListner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
choose_ringtone = (int) id;
// Toast.makeText(setAlarm.this,"The selected choice is "+ id,Toast.LENGTH_SHORT).show();
String ringtSound = String.valueOf(parent.getItemAtPosition(position));
//set ringtone options
switch (ringtSound) {
case "alarm Sound 1":
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up);
break;
case "alarm Sound 2":
mp = MediaPlayer.create(setAlarm.this, R.raw.alarm);
break;
case "alarm Sound 3":
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up_tone);
break;
case "alarm Sound 4":
mp = MediaPlayer.create(setAlarm.this, R.raw.sweet_alarm);
break;
case "alarm Sound 5":
mp = MediaPlayer.create(setAlarm.this, R.raw.morning_alarm);
break;
default:
break;
}
if(mp!=null) {
mp.start();
}
Before the switch statement, you could do something like
if (mp != null && mp.isPlaying()) {
mp.stop();
}
I am not sure what the rest of your code looks like, but please make sure that it also follows the tips from Android Developers, specifically:
It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether. Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
Try this,,Before start Play call below function.
switch (ringtSound) {
case "alarm Sound 1":
stopPlaying()
mp = MediaPlayer.create(setAlarm.this, R.raw.wake_up);
mp.start();
break;
}
Call the function to stop sound.
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
Try mp.stop(); before start any sound...

How to play/stop/play again using MediaPlayer [duplicate]

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?

Stopping or Pausing MediaPlayer on click using single media player

How can I stop or pause MediaPlayer using only 1 media player?
For example I have Grid View. What I want when I click on button it's playing sound.
But here is tricky part. When I click again on button, its not stopping it but instead it's resting sound and playing it from start.
I tried to add
else{
mp.pause
mp.seekTo(0);
}
But it didn't work. It gave me errors
Here is my Code:
case 24:
if(mp!=null)
{
mp.release();
mp=null;
}
mp = MediaPlayer.create(Glavni.this, R.raw.s25snd);
mp.start();
break;
You should be able to pause the track
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
save length and than resume it with that length
Mediaplayer.seekTo(length);
Mediaplayer.start();
You need something like this:
if (mp == null) {
initMediaPlayer();
}
...
if (mPrepared && mHasStarted && mp.isPlaying()) {
mp.pause();
} else if (mPrepared) {
mHasStarted = true;
mp.start();
}
Where mPrepared is true once the media player has been prepared. If you look really closely at the state diagram and table, you'll notice it's an error to call pause() before the playback has been started. If this is indeed intentional, then you need the mHasStarted flag as well.

Unable to stop the execution of media player in the middle

I have the following code snippet. Not sure why, when clicked on the exit button the media player doesn't stop, even though exiting the game should stop all activities in it.
Any help would be appreciated.I have tried stop(), release(), reset() and setting to null. Please let me know where am I going wrong.
public void onClick(View v){
// The background music of the game
MediaPlayer back_music = MediaPlayer.create(getBaseContext(), R.raw.sher_khan);
switch (v.getId()){
case R.id.new_game:
openNewGameDialog();
break;
case R.id.about_game :
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_game :
if(back_music.isPlaying()){
back_music.release();
back_music.reset();
back_music = null ;
//onDestroy();
}
finish();
break;
case R.id.sound :
// Looping the music
//back_music.setLooping(true);
// Identifying and kind of looping through the sound_selector items
if(v.isSelected()){
v.setSelected(false);
play = false;
//Music start for the media player
back_music.start();
}
else if (!v.isSelected()){
//speaker.setSelected(false);
back_music.stop();
back_music.release();
v.setSelected(true);
play = true;
back_music.release();
}
}
Please try the following :
create your media player on the on create and declare it as a member of your ACTIVITY :
MediaPlayer back_music;
public class MainACtivity(){
....
....
onCreate(){
back_music= MediaPlayer.create(getBaseContext(), R.raw.sher_khan);
.....
}
then delete the the row "MediaPlayer back_music = MediaPlayer.create(getBaseContext(), R.raw.sher_khan);"
and give it a another shot :)
hope it helps you .

Android: Mediaplayer stop / start playing raw resource

It now plays on first press, stops on second press or press of another sound button. If I let a sound play through I can restart it or play another sound on a single press.
My goal is for the second press of the same button to stop it, but the press of a new button to just start the new sound instead of first press of new button stopping the old sound, second press starting the new sound.
I can see why it does what it does now but am not sure how to make it work the way I want.
public void playSound(int input){
if (mp!=null && mp.isPlaying()) {
mp.stop();
mp.reset();
} else{
if (mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Soundboard.this, input);
mp.start();
}
}
This block of code:
if (mp!=null){
mp.reset();
mp.release();
}
will never be executed. mp can only be null at this point, as it is in the else block of an if (mp != null) test. This suggests that there is a flaw in your thinking regarding the use of this method.
If a sound has played through and you press the button, then this code will execute:
mp.release();
mp = null;
Since mp was not null, the else block doesn't execute and no new sound is played. When the button is pressed a second time, mp is now null and the else block gets executed, creating a new MediaPlayer and playing the sound.

Categories

Resources