repeat audio in mediaplayer - android

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);

Related

seekbar and mediaplayer android

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

Media player seekTo(position) not working properly

I am trying to play the song from local storage using MediaPlayer. playing song is working properly, but when i am trying to change mediaPlayer.seekTo(p) with seekBar onProgressChanged listener it always restart the song instead of starting from seeked position. This is not duplicate question i have searched for the solution but nothing works for me.
Code To Play Song:
public void playSong(String path) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getContext(), Uri.parse(path));
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared: ");
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
Change SeekBar Position Listener:
public void circularSeekBarChangeListener(CircularSeekBar circularSeekBar) {
circularSeekBar.setOnSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {
#Override
public void onProgressChanged(CircularSeekBar circularSeekBar, float progress, boolean fromUser) {
Log.d(TAG, "onProgressChanged: P2 " + progress);
if (fromUser) {
int p = (int) progress;
Log.d(TAG, "onProgressChanged: "+mediaPlayer.getCurrentPosition());
mediaPlayer.seekTo(p);
Log.d(TAG, "onProgress1111 "+mediaPlayer.getCurrentPosition());
}
}
#Override
public void onStopTrackingTouch(CircularSeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(CircularSeekBar seekBar) {
}
});
}

How to set Looping conditions for VideoView in Android

I want to set looping conditions and for the VideoView. This is what I am trying to achieve.
Video Starts and finishes if Edit Text is not selected.
If Video Start and EditText is selected then Video is set to looping.
If the user types into the EditText and presses button submit then looping set to false and the activity closes after video completes.
If EditText loses selection the video activity finishes on complete.
Here is the code but it's not working for me
mVideoView.setVideoPath(phone);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(final MediaPlayer mp) {
mVideoView.start();
if (mVideoView.isPlaying()) {
mp.setLooping(false);
}
}
}
);
CommentBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp1) {
finish();
}
}
);
Problem is you have not called mediaplayer.start() after you set loop in onCompletionListener().
I executed the below code and it works fine here
Solution:
Create public bundle type of variable
b=new Bundle();
b.putBoolean("repeat", false);
v.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.ak));
v.requestFocus();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (txt.getText().toString().length() > 0) {
b.putBoolean("repeat", false);
}
}
});
txt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean has) {
if (has) {
b.putBoolean("repeat", true);
}
}
});
v.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (b.getBoolean("repeat") == true) {
mediaPlayer.setLooping(true);
mediaPlayer.start();
Toast.makeText(getApplicationContext(),String.valueOf(b.getBoolean("repeat")),Toast.LENGTH_LONG).show();
} else {
mediaPlayer.setLooping(false);
mediaPlayer.stop();
Toast.makeText(getApplicationContext(),String.valueOf(b.getBoolean("repeat")),Toast.LENGTH_LONG).show();
}
}
});
v.start();

Using Timer in MediaPlayer does not work

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);
}
});

OnCompletionListener not called once media player has been reset and initialised

In my code, I have a start/stop button.I am trying to play an audio clip from the raw folder. On completion of an audio clip, the start/stop button has to change from stop mode to start mode. I don't want to loop the audio. For this,I have called OnCompletionListener. However, the listener is called only once. If the media player is reset and created again, OnCompletionListener is no longer called.
public void playsong(View view) {
if (mySound != null)
{
if (!pause) {mySound.start();
btnStartStop.setBackgroundResource(R.drawable.button);
pause = true;
} else {
if (mySound.isPlaying()) {
mySound.stop();
do_it();
}
}
}
}
mySound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mySound) {
do_it();
}
});
public void do_it() {
mySound.reset();
mySound = MediaPlayer.create(this, R.raw.a);
pause = false;
btnStartStop.setBackgroundResource(R.drawable.button2);
}
Try this code ?
In this example oncompletionlistner is called everytime
btnDemo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(objPlayer!=null){
if(objPlayer.isPlaying())
stopPlaying(objPlayer);
}
objPlayer = MediaPlayer.create(getApplicationContext(),R.raw.demoaudio);
if(objPlayer.isPlaying())
objPlayer.pause();
else
objPlayer.start();
Log.d("null", "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d("null", "Problem in Playing Audio");
}
objPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
objPlayer.release();
objPlayer = null;
};
});
}
});

Categories

Resources