Player is pausing but not resuming
My code
btnplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
Currentposition = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
mediaPlayer.seekTo(currentPosition);
btnplay.setImageResource(R.drawable.img_btn_play);
else {
mediaPlayer.start();
btnplay.setImageResource(R.drawable.img_btn_pause);
try {
mediaPlayer.setDataSource(songs_url[counter]);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.start();
btnplay.setImageResource(R.drawable.img_btn_pause);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
Related
I am developing an app in which I have to fetch audio from firebase. I am using firebase recycler view. I am getting a problem that when I click the first song it plays good, but when I click second item then the first song's play button does not change and seekbar of first and second song changes.
Here is the code.
holder.play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String audioUrl3 = model.getMedia();
if (!isPlaying){
if (mPlayer!=null){
mPlayer.release();
}
Toast.makeText(mContext, "Loading....", Toast.LENGTH_SHORT).show();
isPlaying = true;
Uri uri = Uri.parse(audioUrl3);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.reset();
try {
mPlayer.setDataSource(mContext,uri);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(mContext, "Error Occured", Toast.LENGTH_SHORT).show();
}
try{
mPlayer.prepareAsync();
}catch (Exception e){
Toast.makeText(mContext, "Error Occured", Toast.LENGTH_SHORT).show();
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
}
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if(mPlayer.isPlaying()){
mPlayer.stop();
}
mPlayer.start();
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_pause));
holder.seekBar.setMax(mPlayer.getDuration());
Toast.makeText(mContext, "Playing...", Toast.LENGTH_SHORT).show();
updateSeekBar(holder.seekBar);
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
isPlaying = false;
}
});
}
else {
isPlaying=false;
stopPlaying();
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer=null;
holder.play_btn.setImageDrawable(holder.play_btn.getContext().getResources().getDrawable(R.drawable.ic_play));
}
});
}
private void updateSeekBar(final SeekBar seek) {
try{
int pos = mPlayer.getCurrentPosition();
seek.setProgress(pos);
}catch (Exception e){
}
runnable = new Runnable() {
#Override
public void run() {
updateSeekBar(seek);
}
};
handler.postDelayed(runnable, 1800);
}
I want to play/stream audio from url one after other automatically.
here what I have tried:
final String audioUrl[] = {"https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/NV.wav?alt=media", "https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/cqo.wav?alt=media"};
backtobackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
Log.d("Current track",currentTrack+"");
Log.d("audioUrl.length",audioUrl.length+"");
if (currentTrack < audioUrl.length) {
currentTrack++;
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
mediaPlayer.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
// make something
}
}
});
My first audio url gets played when I click on backtoback named button but failed to play second audio from url.
changed this part of code
mediaPlayer.reset();
mediaPlayer.setDataSource(audioUrl[currentTrack]);
mediaPlayer.prepareAsync();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
currentTrack++;
and worked.
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mediaPlayer=new MediaPlayer();
try {
mediaPlayer.setDataSource("https://firebasestorage.googleapis.com/v0/b/photo-on-birthday-cake-cccf5.appspot.com/o/birthday%20(1).mp3?alt=media&token=c1e988b4-f482-4020-8057-53c68195d2fc");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if(flag==0) {
mediaPlayer.start();
Toast.makeText(MusicActivity.this, "start", Toast.LENGTH_SHORT).show();
flag=1;
play.setBackgroundResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else {
mediaPlayer.pause();
Toast.makeText(MusicActivity.this, "Stop", Toast.LENGTH_SHORT).show();
flag=0;
}
}
});
mediaPlayer.prepare();
}
catch (IOException e){
e.printStackTrace();
}
}
});
I tried this code but this is not working on pause function while working on play.
Hi i have a little question I want to use mediaplayer but with the url doesnt work, it means after a lot of minutess it sounds
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://176.31.246.109:8000/jcradio13");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
// mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
} else if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
I am trying to play an audio file when the app started, but I am getting an error says that start called in state 1 Error (-38,0), here is my code:
public void audioPlayer(){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.create(this, R.raw.music);
onPrepared(mp);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mp) {
mp.start();
}
public void audioPlayer(){
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener(mListener);
try {
mp.setDataSource(this, R.raw.music);
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
private OnPreparedListener mListener=new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};