Mediaplayer should to play sound after onFinish countdownTimer, but it wouldn`t. After this code is only a Toast, which works fine. Before countdownTimer is dialog and onClick in my code snippet is from one of buttons of dialog.
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogfeed.dismiss();
CountDownTimerfeed = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millis) {
}
#Override
public void onFinish() {
final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.melodyfeed);
mp.start();
}
};
Create your mediaPlayer object out of your listener and only start it when user clicked on it.
Related
How can I reference more than one song on the same button click and not writing Mediaplayer.create() again and again for every song?
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(context,R.raw.jeenajeena);
mediaPlayer.start();
}
Add your files in array and play them one by one.
int[] medias = {
R.raw.song1, R.raw.song2, R.raw.song3
};
Now create a loop that plays through them one by one.
private void playSongs(final int next){
if(next>=medias.length)return;
mediaPlayer = MediaPlayer.create(context,medias[next]);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playSongs(next+1);
}
});
mediaPlayer.start();
}
Now you should call this on your onClick of the button
playSongs(0);
setOnCompletionListener is detecting the completion of a song the first time only. In the code below song1 and song2 are played one after the other but the remaining songs are not being played.
I want to play the songs one by one and add some silence between songs.
MediaPlayer song0=new MediaPlayer();
int track = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
song0=MediaPlayer.create(this,R.raw.song1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
function();
}
});
song0.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song0) {
track++;
loadsong();
function();
}
});
}
void loadsong()
{
if(track==1) song0=MediaPlayer.create(this,R.raw.song2);
if(track==2) song0=MediaPlayer.create(this,R.raw.song3);
if(track==3) song0=MediaPlayer.create(this,R.raw.song4);
}**strong text**
void function(){
if(track<4) song0.start();
else
song0.stop();
}
The problem is that you create MediaPlayer object every time you need to play songs. So you need to set OnCompletionListener every time after creating MediaPlayer object for another song.
So you can change a few lines in your code to fix the issue.
MediaPlayer song0=new MediaPlayer();
int track = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
song0=MediaPlayer.create(this,R.raw.song1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
function();
}
});
song0.setOnCompletionListener(m_CompletionListener);
}
void loadsong()
{
if(track==1) {
song0=MediaPlayer.create(this,R.raw.song2);
song0.setOnCompletionListener(m_CompletionListener);
}
if(track==2) {
song0=MediaPlayer.create(this,R.raw.song3);
song0.setOnCompletionListener(m_CompletionListener);
}
if(track==3) {
song0=MediaPlayer.create(this,R.raw.song4);
song0.setOnCompletionListener(m_CompletionListener);
}
}**strong text**
void function(){
if(track<4) song0.start();
else
song0.stop();
}
MediaPlayer.OnCompletionListener m_CompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song0) {
track++;
loadsong();
function();
}
};
Another way to implement is to create only one MediaPlayer object and instead of creating MediaPlayer object everytime, call setDataSource function for playing other songs.
If you need this way more detail, i can make sample code also.
I hope it will help you!
Your onclickListener event only starting for once. If you want to play song one by one you have to create a loop or have to do it recursively. Here's a snippet where I used song0.setOnCompletionListener inside loadsong() and in the event recursively called loadsong() every time. Changed your loadsong() method a little bit. Here is the code:
public class MainActivity extends AppCompatActivity {
private Button play;
MediaPlayer song0 = new MediaPlayer();
int track = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadsong();
}
});
}
void loadsong() {
track++;
if (track == 1) {
song0 = MediaPlayer.create(this, R.raw.track1);
song0.start();
}else if (track == 2) {
song0 = MediaPlayer.create(this, R.raw.track2);
song0.start();
}else if (track == 3) {
song0 = MediaPlayer.create(this, R.raw.track3);
song0.start();
}else if (track > 3) {
song0.stop();
}
song0.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song) {
song.stop();
loadsong();
}
});
}
}
i'm newbie in this world of programming, i want reproduce one sound at time, is a application with long sounds of animals.
when i push some button make sound about some animals (gato make sound of a cat)
i fail to play a sound at time, i try with soundpool too, same results, obviously the problem are my low lvl of programming xD, can somebody help me?
public class MainActivity extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void presionGato(View v) {
mp.stop();
mp.create(this, R.raw.gato);
mp.start();
}
public void presionLeon(View v) {
mp.stop();
mp.create(this, R.raw.leon);
mp.start();
}
public void presionPerro(View v) {
mp.stop();
mp = mp.create (this, R.raw.perro);
mp.start();
}
public void presionTigre(View v) {
mp = mp.create (this, R.raw.tigre);
mp.start();
}
public void presionRata(View v) {
mp = mp.create (this, R.raw.rata);
mp.start();
}
public void presionSapo(View v) {
mp = mp.create (this, R.raw.sapo);
mp.start();
}
public void presionRana(View v) {
mp = mp.create (this, R.raw.rana);
mp.start();
}
}
I suggest to you, learn programming and search about it. You didn't listen anything from UI, how can you know which button was clicked?
first, initialize UI element and media player on onCreate;
Button gatoButton = (Button) findViewById(R.id.gato);
Button leonButton = (Button) findViewById(R.id.leon);
mp = new MediaPlayer();
then set onClickListener for each button.
gatoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionGato();
}});
leonButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionLeon();
}});
You can use
if(mp.isPlaying()){
mp.stop();
mp.release();
}
it will stop the music if it is playing then you can start the your new music again. It prevent to play the music at the same time.
I've been trying to create a countdown that plays a buzzer wav (R.raw.buzzer1) when it runs out of time. Every code I've seen on this site causes my app to force close. How would I do this? For example,
public void onFinish()
{
// play the sound somehow?
}
Thanks!
try this:
MediaPlayer mpxmPlayer2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mpxmPlayer2 = new MediaPlayer();
mpxmPlayer2 = MediaPlayer.create(this, R.raw.digital_preview);
cntr_aCounter.start();
}
CountDownTimer cntr_aCounter = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
Log.e("CountDown",millisUntilFinished+"");
}
public void onFinish() {
mpxmPlayer2.start();
}
};
I really hope you can help me here. Below is a section of my code that successfully runs two count down timers side by side...all I want to do is play a short mp3 file when count down has finished....I have tried many different bits of code but I am struggling to make anything work...an quick wins would be good..
So to round up two timers each need to play sound when finished..
//Declare Start/Stop button
Button btnstart = (Button)findViewById(R.id.btnstart);
Button Button1 = (Button)findViewById(R.id.Button01);
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField=(TextView)findViewById(R.id.counter2);
//Counter 1
final CountDownTimer Counter1 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText(" " + formatTime(millisUntilFinished));
}
public void onFinish() {
start();
}
};
//Counter 2
final CountDownTimer counter2 = new CountDownTimer(9000000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter2TextField.setText(" " + formatTime(millisUntilFinished));
}
public void onFinish() {
start();
}
};
//Start Button1
btnstart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Counter1.start();
}
});
//Start Button2
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
counter2.start();
Thanks in advance
Dj
I assume start() is the function you call to play the sound, right ?
so inside the definition of start(), put the following code :
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound
mp.start();
Hope this helps!
Edit: trying to be super clear :)
Somewhere in your code, it is written :
public void onFinish() {
start();
}
This method/function is called when the counter finishes.
Inside this function it is written 'start()'
I don't know what this start() does.
In both cases, I suggest you keep it (if it doesn't create an error), and after start(), add playSound() inside the two onFinish() methods.
and then write OUTSIDE of this function, the following:
public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound
mp.start();
}