Im "playing" with Android Studio, making a DrumPad app, but im struggling with it. I want that my buttons, if clicked, start playing the sample and if clicked again, just stop the sample. Every button have differents MediaPlayers, so here is an example of what im trying to do:
redbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mediaplayer.isPlaying()) {
mediaplayer = MediaPlayer.create(MainActivity.this, R.raw.chord1_ashp_maj7);
mediaplayer.start();
}
stopPlaying(mediaplayer);
}
});
just put else part ..
like,
if (!mediaplayer.isPlaying())
{
mediaplayer = MediaPlayer.create(MainActivity.this, R.raw.chord1_ashp_maj7);
mediaplayer.start();
}
else
{
stopPlaying(mediaplayer);
}
Because, you have to stop media player if its already running..
Note: Yeah I know, may be you have taken care of it in stopPlaying() function but without code of this function I assumed you have to put stop media player part in else statement.
If I did understand you very clearly, it is just a case of
if(---condition--){--statement--}else{--statement--} issue
redbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mediaplayer.isPlaying())
{
mediaplayer = MediaPlayer.create(MainActivity.this, R.raw.chord1_ashp_maj7);
mediaplayer.start();
}else{
stopPlaying(mediaplayer);
}
}
});
if (aredbtn2==false)
{
aredbtn2=true;
}
else
{
aredbtn2=false;
}
if(aredbtn2 == true)
{
mredbutton2 = MediaPlayer.create(MainActivity.this, R.raw.chord1_ashp_maj7); mredbutton2.start(); } else { stopPlaying(mredbutton2);
}
Related
I'm testing Android sound implementation. I have an "imagenButton" and when I push it, a method "musica()" is called. Is really simple:
public void musica(View v) {
img = (ImageView) findViewById(R.id.Migrunido);
if(m.isPlaying()) {
m.stop();
}
else {
m.start();
}
}
It works, but want when I press the button, the sound repeats. I was reading a lot about threads and I have to prepare it before "stop()" but can't fix it.
I have to prepare my method? Or what is the problem?
Thanks
try
public static void playAudio(int id) {
mediaPlayer = MediaPlayer.create(context,id);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
}else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
if this doesn't work try mediaPlayer.prepare(); before you start.
Here I have 2 buttons for the audio controller, play and pause. How can I combine both buttons into one? I also need the image icon to change as well.
In onCreate, I have initially disabled Pause button.
pauseButton.setEnabled(false);
Below is the implementation for Play button.
public void play(View view){
mediaPlayer.start();
...
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
Below is the implementation for Pause button.
public void pause(View view){
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
Make a single button and take a flag as
Boolean buttonflag=true;//initially true
Suppose true means play and false will mean pause.
public void button(View view)
{
if(buttonflag)//when true play
{
mediaPlayer.play();
buttonflag=false;//set to false so that on next click else will work
}
else//when false pause
{
mediaPlayer.pause();
buttonflag=true;//set to true so that on next click if will work
}
}
You can do this by 2 ways :
1. Take a frame layout and put two buttons overlapping each other (by default set visible to play button and hide the pause button) and set visibility according to it like
public void play(View view) {
if(pauseButton.isVisible()) {
pauseButton.setVisibility(View.GONE);
}
playButton.setVisibility(View.VISIBLE);
}
public void pause(View view) {
if(playButton.isVisible()) {
playButton.setVisibility(View.GONE);
}
pauseButton.setVisibility(View.VISIBLE);
}
2. You can take only one button and change it backgroundResource (by default set background resource play) like
public void play(View view) {
Button.setBackgroundResourec(R.drawable.pause);
}
public void pause(View view) {
Button.setBackgroundResourec(R.drawable.play);
}
MediaPlayer m1 = null;
playAndStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
m1=MediaPlayer.create(MainActivity.this, R.raw.sound1);
m1.start();
}
});
private void stopPlaying() {
if (mp1 != null) {
m1.stop();
m1.release();
m1 = null;
}
This should work for you! Good luck.
I know it is quite late to answer this question, but i have searched for this question and google shown me this at the top of result, but as i see there is not satisfactory result is available here, So here i am posting my code so that it could help other
fabBtnPlayAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!playPause) {
fabBtnPlayAudio.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
if (intialStage){
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource("URL for audio file.. ");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
fabBtnPlayAudio.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
playPause = false;
}
}
});
I am making a new android sound application. I made a clickable button to play sound when I click on it. But I also want it to stop playing sound when I click for the second time. That part works fine now here is the problem, when I click again on button to play sound again, it doesn't play it, Media player is completely stopped. I was looking on forums but I can't seem to find an answer that could help me.
Here is my Activity:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset();
}
else {
mpButtonClick1.start();
}
}
});
When I try to write mpButtonClick1.prepare(); I get error Unhandled Exception Type IOE exception
Try to use pause instead of stop.
Reason: if you pause the MediaPlayer, then you can resume it later. However, if you use stop, almost any other method won't work and you will have to prepare the MediaPlayer again (or create a new one).
More info: here and here
PS: don't forget to release the memory when you finish using the resources.
Try this:
You should use only one mediaplayer object
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
Change your class with below code:
remove reset();.
init well all components:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
}
else {
mpButtonClick1.start();
}
}
});
You're calling mpButtonClick1.reset() after mpButtonClick1.stop() - don't do that:
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset(); //<--------- calling reset(), remove this line
}
The docs for reset() say:
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
Remove mpButtonClick1.reset() and it should work.
Keep in mind that MediaPlayer works as a state machine, which means that if you call methods in the wrong order, you'll get problems. Please read about MediaPlayer here and here.
Hey please use following
for stop -> media player
mp.seekTo(0);
mp.pause();
again for start just call
mp.start();
In my experience when I need to play multiple times and I may need to stop one play to start another play, (like in the case of multiple buttons), I just create another player, making sure that I release the resources for the previous one. To stop just use
mediaPlayer.stop();
But for play use something like this (adapt the logging to your specific needs) to create/recreate your player:
private boolean createMediaPlayer()
{
if (mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setVolume(1f, 1f);
try
{
mediaPlayer.setAudioStreamType(Interop.PRIMARY_STREAM);
mediaPlayer.setDataSource(m_soundFile);
mediaPlayer.prepare();
return true;
// Interop.logDebug(TAG + "-loadAudio: SUCCESS" + m_soundFile);
} catch (Exception e)
{
Interop.logError(TAG + "-LoadAudio for Clic Sound: audioPlayer prepare failed for current file: " + m_soundFile);
Interop.logError(TAG + "-Exception: " , e);
return false;
}
}
and than use
if (createMediaPlayer())
mediaPlayer.start();
this will ensure proper release of the resources used by the media player.
A simple solution is to Use pause instead of stop and the seek to the beginning of the song.
I know that this question is quite old but recently while learning Android, I also got stuck at this point and found a very simple solution which I'd like to share with everyone.
Instead of trying to stop or reset the media, you can just seek back to the starting position.
mediaPlayer.seekTo(0);
For reference, I am also posting my code below:
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
public void play(View view) {
mp.start();
}
public void pause(View view) {
mp.pause();
}
public void stop(View view) {
// this seeks to the beginning of the file
mp.seekTo(0);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.sample_audio);
}
}
I have a button, when I click it plays music, how to do it, when I click second time, to stop the music?
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp2.start();
}
});
Ok, this one works:
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.n);
one.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.pause();
}
else {
mp2.start();
}
;
}});
The one with Pause above it works, but If I want to stop the music, it does not work.
Following not working:
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.stop();
}
else {
mp2.start();
}
;
}});
I get error: start called in state 0
error (-38, 0)
According to http://developer.android.com/reference/android/media/MediaPlayer.html, I suppose you could do something like this:
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp2.isPlaying() == true)
// Pause the music player
mp2.pause();
// If it's not playing
else
// Resume the music player
mp2.start();
}
});
You can actually write just
if(mp2.isPlaying())
instead of
if(mp2.isPlaying() == true)
It's just for the sake of understanding what is going on.
You could have a boolean check to see if it is started (and set the boolean to false) and if so stop the music, if not start it (and set the boolean to true). Something like:
public void onClick(View v)
{
if(musicPlaying == false)
{
mp2.start();
musicPlaying = true;
}
else
{
mp2.stop();
musicPlaying = false;
}
}
if(mp2.isPlaying()) {
mp2.pause();
} else {
mp2.start();
}
This is what I used to start and stop the music, you must prepare the MediaPlayer for playback. Note: this will start the music from where it stopped.Mediaplayer prepare() methodIf you are streaming music you should use prepareAsync ()
musicButton = (Button) findViewById(R.id.btn_dj_player);
musicButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {//check if a song is already playing
mp.stop();
try {
mp.prepare();//get the mediaplayer reeady for playback
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
mp.start();
}
}
});
If you have only one sound and you want it to start or stop with only one button than this may help .
Button button_name = (Button)this.findViewById(R.id.Your_button_id_Here);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.YOur_audio_File_name_here);
button_name.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
Have you tried using the seekTo() function?
Something like:
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
I am just guessing here, but might be worth looking into. :-)
I accomplished this as follows:
I declared a boolean variable:
boolean isButtonClicked = false;
Then I set up an if else in a method like so:
initPlaySound()
{
if(isButtonClicked)
{
player.start();
} else{
player.stop();
}
Lastly I set up an onClick of the button within the method:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isButtonClicked = !isButtonClicked;
initPlaySound();
}
});
Basically, when the button is first clicked the boolean is set to the opposite of its' current state (false to true) and the code within the if executes. Once the button is clicked again the boolean is set to the opposite again (true to false) and the sound stops.
I solved this problem in a manual way. If sound is playing, i wrapped sound into beginning(seekTo(0)), then paused the sound. If sound is already paused, only seekTo(0) is called.
public void onClick(View v) {
if(mp.isPlaying()){
mp.seekTo(0);
mp.pause();
}
else{
mp.seekTo(0);
}
} ;
Add a global variable boolean flag=false;
if(flag==false)
{
mp=MediaPlayer.create(this, R.raw.abc);
mp.start();
playbutton.setText("Pause");
flag=true;
}
else if(mp.isPlaying()&&flag==true)
{
mp.pause();
playbutton.setText("Play");
flag=false;
}
This code will work if you want to use the same button as PLAY/PAUSE in your app. Hope this helps. Add this code in the button onClick() function which you are using to play or pause.
I'm developing an application containing 100s of sound effects.
after playing some of the sounds the application force closes. I understood that the problem would be consuming all the available space of the memory. I tried using Release() method but after this method is called I'm not able to play the sound again.
I also tried using onDestroy() method to set the object of the mediaplayer to null but since these kind of objects must be final I can't do this.
What is your suggestion?
Here is my code:
final MediaPlayer mp1 = MediaPlayer.create(MainActivity.this, R.raw.a1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i[0] == false)
{
mp1.start();
Toast.makeText(getBaseContext(), "horn", Toast.LENGTH_SHORT).show();
b1.setBackgroundResource(R.drawable.stop_button);
i[0] = true;
}
else
{
mp1.pause();
mp1.seekTo(0);
b1.setBackgroundResource(R.drawable.horn);
i[0] = false;
}
}
});
mp1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
b1.setBackgroundResource(R.drawable.horn);
}
});
Have you tried something like this?
You can call release() in OnDestroy() method.
#Override
public void onDestroy(){
super.onDestroy();
mp.release();
}