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();
}
};
Related
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();
}
});
}
}
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()) {
I'm creating an app that connect with server(localhost).
In my Activity, I have a countdown timer which will tick for 30 seconds and at the finish() I have some code to check if there's any changes in the database on server, and then start the timer again so it'll be a loop in the activity. But the timer seem like running just once. When I'm running in debug mode, the timer get some error at the finish(), it said that I'm missing some folder or file that related to the API or the SDK version which is being run in android:targetSdkVersion="9"
Please help me with this issue. Here's the activity:
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
start();
}
};
counter.start();
}
}
Do I have to make a thread or not?
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
new CountDownTimer(30000, 1000).start(); <------Start Again.
}
};
counter.start();
}
}
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.