I am trying to create a delay for an Audio file that I am repeatedly playing.
However, the code doesn't seems to have any effect whatsoever. Here is my code:
try {
mp.setDataSource(text);
mp.prepare();
mp.setLooping(true);
mp.setVolume(0.5f,0.5f);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
#Override
public void run()
{
time += 1000;
Log.d("TimerExample", "Going for... " + time);
h.postDelayed(this, 5000);
}
}, 5000);
}
});
Any fix/solution would be highly appreciated.
Please refer this https://stackoverflow.com/questions/31247607/adding-delay-while-mediaplayer-loops
Mainly you have to use listener like this
private final Runnable loopingRunnable = new Runnable() {
#Override
public void run() {
if (mp != null) {
if (mp.isPlaying() {
mp.stop();
}
mp.start();
}
}
}
mp.setDataSource(filePath);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
button.postDelayed(loopingRunnable, 5000);
}
});
Related
when i am play audio player and between playing i am play second audio first audio progress bar and timer sink with second audio player and so on . please help on this .**All audio are loaded in recyclerview.**I want audio Player like whatsapp play audio one by one and there progress bar no sink with each other
if (message.getFileType() == MessageFileType.AUDIO)
{
mediaUrl=message.getFile();
Log.d("tag","mediaURL"+mediaUrl);
Uri video = Uri.parse(mediaUrl);
holder.BtnPlayMusic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.BtnPlayMusic.setImageResource(R.drawable.pause_button_red);
if(flag){
mPlayer = MediaPlayer.create(mContext, video);
flag = false;
}
if ( mPlayer.isPlaying())
{
holder.MusicSeekBar.setProgress(0);
mPlayer.stop();
flag=true;
// mPlayer.reset();
holder.BtnPlayMusic.setImageResource(R.drawable.play_button_red);
mPlayer = MediaPlayer.create(mContext, video);
flag = false;
mPlayer.start();
holder.BtnPlayMusic.setImageResource(R.drawable.pause_button_red);
getDurationTimer(holder);
holder.MusicSeekBar.setMax(mPlayer.getDuration()/1000);
getSeekBarStatus(holder);
}
else
{
mPlayer.start();
getDurationTimer(holder);
holder.BtnPlayMusic.setImageResource(R.drawable.pause_button_red);
holder.MusicSeekBar.setMax(mPlayer.getDuration()/1000);
getSeekBarStatus(holder);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
holder.BtnPlayMusic.setImageResource(R.drawable.play_button_red);
flag=true;
holder.MusicSeekBar.setProgress(0);
}}); }}
});
holder.BtnStopMusic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.stop();
mPlayer.reset();
flag=true;
// mPlayer.reset();
holder.BtnPlayMusic.setImageResource(R.drawable.play_button_red);
}
});
}
//Creating duration time method
public void getDurationTimer(MessageViewHolder holder){
final long minutes=(mPlayer.getDuration()/1000)/60;
final int seconds= (int) ((mPlayer.getDuration()/1000)%60);
holder.MusicTime.setText(minutes+ "0:0"+seconds);
}
//creating a method for seekBar progress
public void getSeekBarStatus(MessageViewHolder holder){
thread=new Thread(new Runnable() {
#Override
public void run() {
int currentPosition = 0;
int total = mPlayer.getDuration();
holder.MusicSeekBar.setMax(total);
while (mPlayer != null && currentPosition < total && flag==false) {
try {
Thread.sleep(1000);
currentPosition = mPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
}
holder.MusicSeekBar.setProgress(currentPosition);
}
}
});
thread.start();
holder.MusicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progress=0;
#Override
public void onProgressChanged(final SeekBar seekBar, int ProgressValue, boolean fromUser) {
if (fromUser) {
mPlayer.seekTo(ProgressValue);//if user drags the seekbar, it gets the position and updates in textView.
progress=ProgressValue;
}
final long mMinutes=(ProgressValue/1000)/60;//converting into minutes
final int mSeconds=((ProgressValue/1000)%60);//converting into seconds
holder.MusicTime.setText(mMinutes+"0:0"+mSeconds);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mPlayer.seekTo(progress);
}
});
}
Being a novice coder, trying to code an app that plays a sound on buttonclick in a fragment; successfully did it but I want to add more small pieces of sounds to play one after another. How can I add those sounds(sound2, sound3, sound4, etc) after that 'R.raw.sound1'? Please guide..
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});
}
});
//you can add all the songs in a array
ArrayList<String> songsArray = new ArrayList<>();
int position = 0;
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer(songsArray, position);
}
});
private void mediaPlayer(ArrayList<String> songsArray) {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
}
if (position > songsArray.size() - 1) {
return;
}
try {
mp.setDataSource(songsArray.get(position));
position++;
mp.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
mediaPlayer(songsArray, position);
}
});
}
I am using seekbar and mediaplayer for a chat app to send and listen voice recorders , I have tried all solutions but seekbar doesn't run with me
my code in adapter
private void updateseek()
{
if(player!=null) {
seekBar.setProgress(player.getCurrentPosition());
if(player.isPlaying())
{
runnable = new Runnable() {
#Override
public void run() {
updateseek();
}
};
handler.postDelayed(runnable,100);
}
else {
seekBar.setProgress(0);
}
}
}
//and i call it in getview in adapter :
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
seekBar.setMax(player.getDuration());
player.start();
updateseek();
}
});
and I also implemented setOnSeekBarChangeListener to seekbar , but not working
for repeat audio in mediaplayer write this code but not work correct :
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
G.mediaPlayer.seekTo(i);
}
if (i >= G.mediaPlayer.getDuration()) {
if (isRepeat) {
G.mediaPlayer.seekTo(0);
G.mediaPlayer.start();
}else {
G.mediaPlayer.seekTo(0);
G.mediaPlayer.pause();
}
}
}
and for play audio do this :
public void playSongWithIndex(final int songIndex) {
progressBar.setVisibility(View.VISIBLE);
if (music_exist.exists()) {
playMusicOfflineMode();
} else {
try {
G.mediaPlayer.reset();
G.mediaPlayer.setDataSource(songList.get(songIndex).getFile_128());
G.mediaPlayer.prepareAsync();
G.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
G.mediaPlayer.start();
seekBar.setMax(G.mediaPlayer.getDuration());
progressBar.setVisibility(View.INVISIBLE);
timer = new Timer();
timer.schedule(new MainTimer(), 0, 1000);
}
});
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
e.printStackTrace();
}
}
}
but repeat audio some times work done and sometimes when seekbar well be over stop and do not work .
please help me how to repeat audio ?
You can set Media player looping to true,use this code:
G.mediaPlayer.setLooping(true);
And if you want to know when the player has finished you should use OnCompletionListener like this:
G.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
});
ok for help other developer write my correct answer :
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (isRepeat) {
G.mediaPlayer.start();
} else {
G.mediaPlayer.seekTo(0);
seekBar.setProgress(0);
current_duration_text.setText("" + utils.milliSecondsToTimer(0));
play_music_img.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
}
I hope to be useful
i am not sure but i think you can use loop ...for example
mMediaPlayer.setLooping(true);
Am using mediaplayer to play tick sound in timer.But it continues to play after timer is over,Even after i called stop for media player. I played sound as seen below in ontick() in timer. In onfinish() called mp.Stop(),But it not working.
mp = MediaPlayer.create(getBaseContext(), sound);
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
});
mp.setLooping(false);
// mp.setVolume(0.1, 0.1);
mp.start();
}
coding for my timer .
final class MyCounter extends CountDownTimer {
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timer.cancel();
MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.release();
mp.stop();
mp.reset();
}
}
};
mp.setOnCompletionListener(onCompletion);
i++;
submitAnswer();
final GameEngine ge = GameEngine.getInstance();
timeer.setText("Timer Completed.");
if (ge.getCurrentUnAllocatedAmount() > 0 && i <= 8) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
ge.setCurrentLevel(i);
startActivity(intent);
}
}, 2200);
} else if (ge.getCurrentUnAllocatedAmount() <= 0 || i > 8) {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
startActivity(intent);
}
}
public void onTick(long millisUntilFinished) {
remTime = (millisUntilFinished / 1000);
timeer.setText("Time:" + (millisUntilFinished / 1000) + "");
mp = MediaPlayer.create(getBaseContext(), R.raw.beep7);
if (remTime > 0) {
mp.start();
mp.release();
}
if (remTime < 10) {
final Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
if (timeer.getVisibility() == View.VISIBLE) {
timeer.setVisibility(View.INVISIBLE);
} else {
timeer.setVisibility(View.VISIBLE);
}
timeer.setTextColor(Color.RED);
}
});
}
}
}
Use this code:
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
});
try this:
mp.setOnCompletionListener(onCompletion);
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
}
};
I think you are not calling the release() method on the onCompletionListener() itself.
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
});
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
});
Check condition when timer is stop.
if(mPlayer.isPlaying())
{
mPlayer.stop();
}