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();
}
Related
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.
The audio file (.mp3) gets played when I don't use the onPreparedListener, but I get an error after the file tries to replay after some time (because it's not listening for the state obviously). So basically the code is checking the battery state and if it's 10% or below it plays the alarm sound, I also have a button which I can click to stop the sound. Now with the onPreparedListener the alarm sound doesn't get played anymore. What am I doing wrong?
tv_battery = (TextView) findViewById(R.id.tv_battery);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm);
Button b = (Button) findViewById(R.id.stop_alarm);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
runnable = new Runnable() {
#Override
public void run() {
int level = (int) batteryLevel();
tv_battery.setText("Battery: " + level + "%");
handler.postDelayed(runnable, 5000);
if(level <= 10) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
mp.start();
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
count.start();
}
I think you have place mp.start(); on wrong place please move it before count.start(); as below :
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
mp.start();
count.start();
Also correct below condition if media player already started when battery is under 10 otherwise media player again prepared even after started :
if(level <= 10 && !mp.isPlaying()) {
Hi i have again a Problem,
my app plays sound 1 sec. this work fine but if i press less 20 time on any button then plays nothing more what is the error? How i can fix it?
tsc = (Button) findViewById(R.id.button12);
tsc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(R.raw.en_bye);
}
private void playSound(int sFile) {
// set up MediaPlayer
final int medFile = sFile;
Thread thread = new Thread(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), medFile);
mp.start();
}
});
thread.start();
}
Thank you
Here's what I am trying to achieve: When user presses "Button A", a 30 seconds "sound.mp3" starts playing over and over for 200 seconds and then stops. "Button B" plays the same sound for 400 seconds and stops.
Here's my last attempt, using CountDownTimer (not sure if that's the best approach or even possible):
public class MyClass extends Activity {
MediaPlayer player_test;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_rain);
my_player();
}
public void my_player(){
player_test = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
player_test.setLooping(true);
player_test.start();
}
public void onCompletion(MediaPlayer player_test) {
player_test.stop();
player_test.release();
}
});
public void player_OnClick(View v) {
switch (v.getId()) {
case R.id.button_a:
new CountDownTimer(200000, 1000) {
public void onTick(long millisUntilFinished) {
player_test.start();
}
public void onFinish() {
player_test.pause();
}
}.start();
break;
case R.id.button_b:
new CountDownTimer(400000, 1000) {
public void onTick(long millisUntilFinished) {
player_test.start();
}
public void onFinish() {
player_test.pause(); }
}.start();
break;
}
}
Thank you for your answers.
It's a good approach. However, you should fix some points in your code:
Don't call player_test.start() in your my_player() method, it causes the player to start playing when your activity is created. You should only instantiate the player_test object in that method.
Don't call player_test.start() in every tick of the CountDownTimer. You only need to call it one time to start playing.
Call player_test.start() whenever any of the buttons A or B is clicked.
I am trying to play an mp3 file (with an onClickListener) and stop after 2 seconds. I tried the code below but it is not working. Could anyone please help?
final MediaPlayer mpsound = MediaPlayer.create(this, R.raw.media_player_sound);
ImageView sound = (ImageView) findViewById(R.id.button);
sound.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});
Why are you calling stop() on mpfrog if you are playing audio with mpsound? You need to call the stop() function on the mpsound MediaPlayer. Also, you might want to add the #Override annotation to your onClick() method.
for the override...
sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});
for a timer.....
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
mpsound.stop();
}
};
//Task for timer to execute when time expires
class SleepTask extends TimerTask {
#Override
public void run(){
handler.sendEmptyMessage(0);
}
}
//then in some other function...
Timer timer = new Timer("timer",true);
timer.schedule(new SleepTask(),2000);
You have to wait 2 seconds in different thread, for this case use your own created thread and wait there and stop player, or you can use CountDownTimer - very simple solution.
You can find same question aggregated here and here
In eclipse you can use autocomplete (ctrl + space by default) to fill automatically all inmplemented methods and #Overrides will already be there.