How can I have an on and off state for an Image Button? My goal is to have sound play when the image button is clicked, and for sound to stop when the button is clicked again. Thank you!
you can use event's. like on click listener.
get your imageView and setOnClickListener for this.
ImageView mImageView = (ImageView) findViewById(R.id.sound_imageView);
Boolean flag = false;
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(flag) {
//play sound
flag = false;
} else {
//stop sound
flag = true;
}
}
});
There are a few Views that exist in Android that provide toggle functionality like you are looking for. You might want to research the android.widget.CompoundButton class for ideas, or reference this tutorial.
You can do it manually. First when you will click the button you will check whether the music is running or not. If it is running then stop it , if not running then play it.
Something like that -
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(musicPlayer!=null && musicPlayer.isPlaying()){
musicPlayer.stop();
}else{
musicPlayer=new MediaPlayer();
AssetFileDescriptor afd = getActivity().getAssets().openFd("AudioFile.mp3");
musicPlayer.setDataSource(afd.getFileDescriptor());
musicPlayer.prepare();
musicPlayer.start();
}
}
});
Related
I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it
please help, I always experience errors here, I create mediaplayer in the audio list using 1 button, I have created the condition and isPlaying function on the mediaplayer in the adapter class, when the audio button is clicked successfully run, but when I click a second time, audio can not be paused, just repeat from the beginning, help me, what is the right solution?
this is my code:
public void onBindViewHolder(#NonNull MurottalAdapter.MurotalHolder holder, int position) {
Item item = itemList.get(position);
holder.titles.setText(item.getTitle());
holder.btnplays.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//I've tried replacing this with holder.itemview.getContext() and I put it outside the onclick method, but the problem is the same
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());
if (mediaPlayer.isPlaying()){
if (mediaPlayer != null) {
mediaPlayer.pause();
holder.btnplays.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
}
} else {
if (mediaPlayer != null){
mediaPlayer.start();
holder.btnplays.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
}
});
}
Pls try to move following code outside of onClick() method?
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());
I'm trying to build an Android App where the user can play audiofiles labeled with an id from a database.
//get id from database and turn it into a string, add letter a, because its a res file
stringId = "a" + String.valueOf(standard.getId());
//find playbutton
final FloatingActionButton play = (FloatingActionButton) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//to be able to feed mediaplayer a variable I put it in another function called playAudio
playAudio(stringId, true); }
});
final FloatingActionButton pause = (FloatingActionButton) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
playAudio(stringId, false); }
});
The playAudio looks like this:
private void playAudio(String nameOfFile, Boolean booleanPlay){
MediaPlayer mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(nameOfFile, "raw", getPackageName()));
if (booleanPlay = true){
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
if (booleanPlay = false){
mediaPlayer.pause();
}
}
When I run my code every time I press play it creates a new mediaplayer which starts playing at the same time as the other mediaplayer, the pause button doesn't work for the same reason. I can't figure out how to make it work.
Do not create a new MediaPlayer instance each time you play a new file. Create an instance at the beginning and reuse it:
private static MediaPlayer mediaPlayer = new MediaPlayer ();
When changing the file to play, try this piece of code:
AssetFileDescriptor afd = getApplicationContext ().getResources ().openRawResourceFd (R.raw.sound);
if (afd != null)
{
mediaPlayer.reset()
mediaPlayer.setDataSource(afd.getFileDescriptor (), afd.getStartOffset (), afd.getLength ());
mediaPlayer.setLooping (true);
mediaPlayer.prepare ();
mediaPlayer.start ();
}
Replace R.raw.sound with the filename of the sound to play (this would be in the res/raw folder) or use a different way to reference the file as needed.
That works for me -- tested on Android 8.1 (targeted). Hope this helps.
EDITED TO ADD: I just noticed you have wrong operators in the if(booleanPlay = false) and if (booleanPlay = true) statements. They should be == (comparison, instead of assignment).
And one last thing: When exiting, you should call mediaPlayer.release() to free up the resources, but you probably knew that. Glad I could help.
I want to play music when a button is clicked, but I want to play different music with different button then the first music stopped and the second music would play.
code:
charge = MediaPlayer.create(this, R.raw.charge);
santai1 = MediaPlayer.create(this, R.raw.santai1);
}
public void charge(View view) {
charge.start();
}
public void santai1(View view) {
santai1.start();
}
Say, I clicked charge button and then played the charge.mp3. Then I want to play santai1.mp3 but when I clicked the santai1 button the charge.mp3 will stop playing and the santai1.mp3 will play.
I want this to be able the opposite and it can be done continuously.
When I clicked santai1 button then santai1.mp3 will be played, while the santai.mp3 still playing I clicked the charge button. I want the santai1.mp3 stopped and then play the chage.mp3.
When I clicked on charge button, then I clicked on santai1 button, both music started together.
if you need it for many
create a mediaplayer object and a method.
private MediaPlayer media;
private void stopMusic() {
if (media != null) {
media.stop();
media.release();
media= null;
}
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMusic();
media= MediaPlayer.create("you acivityname".this, "mp3file directory");
media.start();
}
});
try this:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
and do the same for other button
well it's simple you need 2 if statments for the buttons to check if one of them is playing.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(santail.isPlaying()){
santail.stop();
}
charge.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.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();
}