Guys i able to play audio file using Mediaplayer. But i need to play a audio file for specific time.
Eg: play audio1.mp3 till 5 minutes.
The audio file is of 10 seconds only.But it should keep playing till 5 minutes.
try this
mediaplayerplayer.setLooping(true);<--- this lets the audio to loop...
and this is the countdown timer
MyCount counter;
Long s1;
counter= new MyCount(300000,1000);
counter.start();
public void asdf(View v){ <---- method for onclick of buttons pause and resuming timer
switch(v.getId()){
case R.id.button1:<-- for pause
counter.cancel();
break;
case R.id.button2:<--- for resume
counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
mediaplayer.stop();
mediaplayer.release();
}
#Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
}
}
this helps you to pause the mediaplayer also... along with timer.. and lets you play the song for complete 5 mins
//decleration
MediaPlayer mp;
//specify the path of media file
String filepath="?";
//onCreate
mp=new MediaPlayer();
Uri myUri = Uri.parse(filepath);
mp.setLooping(true);
mp = MediaPlayer.create(ActivityName.this, myUri);
mp.start();
new CountDownTimer(300000,1000) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
if(mp.isPlaying())
{
mp.stop();
}
}
}.start();
Related
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()) {
What I am trying to do basically is that I want my countDownTimer to run 60 times , i want to do this by setting the value of a variable to 0 and stopping the timer when the variiable reaches the value of 60.
i=0;
new CountDownTimer(3600000,60000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();
Below method help you to shecdule a countdown for 60 times
new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();
I want to play media player after 10 seconds time delay in android.
player = MediaPlayer.create(getApplicationContext(), R.raw.sleep);
player.start();
You can try something like below: The trick is to start the player after 10 seconds by using a CountDownTimer
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
CountDownTimer timer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.start();
}
};
timer.start();
Don't forget to stop & release the player when done with it.
Another approach is with Handler
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run(){
player.start();
}
}, 10 * 1000);
i want to play MediaPlayer for 1 second. How to set Duration in this code..
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer Timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
player.start();
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.stop();
}
};
Timer.start();
You don't need to do anything on the onTick method.
Try this code:
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Nothing to do
}
#Override
public void onFinish() {
if (player.isPlaying()) {
player.stop();
player.release();
}
}
};
timer.start();
If you look at the constructor:
public CountDownTimer (long millisInFuture, long countDownInterval)
millisInFuture - The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
So onFinish() will be called after 1 second (1000 millisecond = 1second).
Ref: http://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long, long)
You could use TimerTask to schedule a MediaPlayer.stop() to run after 1 secs.
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
MediaPlayer.stop()
}
};
timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 20000 ms
}
can u try this one
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
}
}, timeout);
I want to implement recording in my piano app. to do this, I save the time of playing of each sound and which one was played in a LinkedList. To play the recorded music(sequence of sounds) with this times and sounds, we should play each sound at its specified time. How do this?
here is a part of my code for one sound(note):
public class MainActivity extends Activity {
double StartTime;
double millis;
sound_pool snd;
LinkedList<list> rec_list = new LinkedList<list>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an instance of our sound manger
snd = new sound_pool(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button btn = (Button) findViewById(R.id.button1);
Date dt=new Date();
long mil=dt.getTime();
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN )
{
snd.play_s_r_1();
Date dt=new Date();
long mil=dt.getTime();
rec_list.add(new list(mil,"s_r_1"));
}
return true;
}
}); // end of ontouch listener
} //end of oncreate
} //end of activity
just try following code
public CountDownTimer (long millisInFuture, long countDownInterval)
millisInFuture - The number of millis in the future from the call to start() until the countdown is done and onFinish() is called. So onFinish() will be called after 1 second (1000 millisecond = 1second).
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Nothing to do
}
#Override
public void onFinish() {
if (player.isPlaying()) {
player.stop();
player.release();
}
}
};
timer.start();
Ref: http://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long, long)