i am developing Call recording application. i am managed to show the audio files in ListView and able to play in MediaPlayer. know i am having a problem in SeekBar Handler. when i play the audio file, the MediaPlayer and SeekBar works absolutely right but when i close my application the Handler of SeekBar remains active. how can i stop the Handler.
private void playSong(String string)
throws IllegalArgumentException, SecurityException,
IllegalStateException, IOException {
// seekBar
setContentView(R.layout.dialog_activity);
startButton = (Button) findViewById(R.id.play_button);
stopButton = (Button) findViewById(R.id.pause_button);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
startButton.setOnClickListener(MainActivity.this);
stopButton.setOnClickListener(MainActivity.this);
// MediaPlayer
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(string);
mp.prepare();
mp.start();
seekBar.setMax(mp.getDuration());
try {
seekUpdation();
} catch (Exception e) {
Toast.makeText(MainActivity.this,
"SeekUpdation " + e.toString(), Toast.LENGTH_SHORT)
.show();
}
}
Runnable run = new Runnable() {
#Override
public void run() {
try {
seekUpdation();
} catch (Exception e) {
Log.d("Runnable ", "error in Runnable =" + e.toString());
}
}
};
thats the Handler who remain active after closing the application.
public void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}
});
}
i found the answer by my self.
we have to change the code from this
Runnable run = new Runnable() {
#Override
public void run() {
try {
seekUpdation();
} catch (Exception e) {
Log.d("Runnable ", "error in Runnable =" + e.toString());
}
}
};
public void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}
});
}
to this
Runnable run = new Runnable() {
public void run() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(this, 1000);
}
};
private void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}
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 write this function to get video time . This functions works fine but when I backpress then it throws exception .
public void videoTimer ()
{
try
{
running = true;
final int duration = mVideoView.getDuration();
thread = new Thread(new Runnable()
{
public void run()
{
do
{
tvVideoTimer.post(new Runnable()
{
public void run()
{
int time = (duration - mVideoView.getCurrentPosition()) / 1000;
tvVideoTimer.setText(getTimeString(mVideoView.getCurrentPosition()));
}
});
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() <= duration);
}
});
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
I have listview with layout for playing audio.in that layout i have seekbar and play/pause button when i click on play button media file is played but seekbar not updating I also user Runnable and handler but it not works.
I am doing this in baseadapter inside getview method.
Please help me to sort out this
mAudioViewHolder.play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String yofile = "file://" + filepath;
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(yofile);
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
// TODO Auto-generated catch block
Log.e("Node", "ERRORSS Part 1 is" + e);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mAudioViewHolder.isPlaying = false;
/**
* since audio is stop playing, remove its position value
* */
playingAudioPosition = -1;
}
});
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Node", "ERRORSS is Part 2 " + e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Node", "ERRORSS is Part 3 " + e);
} // might take long! (for buffering, etc)
mediaPlayer.start();
mAudioViewHolder.progressBar.setMax(mediaPlayer.getDuration());
Runnable mRunnable = new Runnable() {
#Override
public void run() {
if (mediaPlayer != null) {
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
mAudioViewHolder.progressBar.setProgress(mCurrentPosition);
Log.d("Node", "get current position " + mCurrentPosition);
}
}
};
Log.d("Node", "StartTime Updation " + mediaPlayer.getDuration() + " " + mediaPlayer.getCurrentPosition());
mHandler.postDelayed(mRunnable, 1000);
//mAudioViewHolder.isPlaying = false;
}
You are calling your runnable only once, when you need to call it every time you want to update your progress bar.
You can do this
Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
mAudioViewHolder.progressBar.setProgress(mCurrentPosition);
Log.d("Node", "get current position "+mCurrentPosition);
mHandler.postDelayed(this, 1000);
}
}
};
Log.d("Node", "StartTime Updation "+mediaPlayer.getDuration()+" "+mediaPlayer.getCurrentPosition());
mHandler.postDelayed(mRunnable, 1000);
Make sure you remove the runnable from the handler when you stop the playback.
mHandler.removeCallbacks(mRunnable);
I want to play a music so I used seekber to show the progress, everything is fine until I press pause button, the seekbar stops but when I press the play button the seekbar does not move, I figured out it is because of getcurrentposition() method, it does not update after pausing, below is my code, what's wrong with it?
#Override
public void onClick(View v) {
try {
if (isPlaying) {
buttonPlayPause.setImageResource(R.drawable.play);
mediaPlayer.pause();
isPlaying = false;
} else {
buttonPlayPause.setImageResource(R.drawable.pause);
isPlaying = true;
mediaPlayer.start();
primarySeekBarProgressUpdater();
}
} catch (Exception e) {
onDestroy();
}
}
});
private void primarySeekBarProgressUpdater() {
try {
seekBarProgress
.setProgress((int) (((float) mediaPlayer
.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100));
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
} catch (Exception e) {
onDestroy();
}
}
Am trying to play a .wav file using media-player. Actually, I had done so far shown below. It's playing an updating the progress correctly but the problem media player is not seeking completely. Media-player stops giving out progress before the total duration of the file and stops updating progress. I have also implemented OnComplete listener also am not getting the callback. Here is my code can anyone spot me with where am missing.
public void playAudioFile() {
try {
mMediaPlayer.reset();
setMediaPlayerSource();
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Graphbar.setProgress(mMediaPlayer.getDuration());
}
});
bPlay.setBackgroundResource(R.drawable.pause_selector);
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("Playing");
updateProgressBar();
}
}).start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateProgressBar() {
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if(mMediaPlayer.isPlaying()){
long totalDuration = Graphbar.getMax();
long currentDuration =mMediaPlayer.getCurrentPosition();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
System.out.println(currentDuration+"/"+totalDuration);
Graphbar.setProgress((int)currentDuration);
mHandler.postDelayed(this, 1);
}
}
};
private void setMediaPlayerSource() {
String audioFile = getFilename();
try {
mMediaPlayer.setDataSource(audioFile);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
for total duration use mediaplayer.getduration. Remove unnecessary code from the runnable handler , because it will create load to ur seek bar . Because at every second u are calculating something and updating layout based on that calculation .
I would suggest you to use chronometer view of android.
public void playAudioFile() {
try {
mMediaPlayer.reset();
setMediaPlayerSource();
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Graphbar.setProgress(mMediaPlayer.getDuration());
}
});
bPlay.setBackgroundResource(R.drawable.pause_selector);
//set this only for once , because for one song total duration is always constant.
updateProgressBar();
}
public void updateProgressBar() {
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
long totalDuration = mMediaPlayer.getDuration();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if(mMediaPlayer.isPlaying()){
//long totalDuration = mMediaPlayer.getDuration();=> no need of calculating total time and updating it to seekbar at every second
long currentDuration =mMediaPlayer.getCurrentPosition();
tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
System.out.println(currentDuration+"/"+totalDuration);
Graphbar.setProgress((int)currentDuration);
mHandler.postDelayed(this, 1);
}
}
};
private void setMediaPlayerSource() {
String audioFile = getFilename();
try {
mMediaPlayer.setDataSource(audioFile);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}