Can't show ProgressDialog on mp3 player
I have a media player that plays mp3 from url. I want to show a progress dialog when a sound is loaded from a url. Here is my code. I'm new, so please don't mind if you can't understand my question well. thanks in advance
playback function
public void playSong(int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setText("Buffering....");
tv.setSelected(true);
final ProgressDialog progressDialog = ProgressDialog.show(this,
"Loading Title", "Loading Message");
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss();
}
mp.start();
}
});
try {
mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true);
mp.prepare();
// Set focus to the textview
tv.setText(naattitles[naatindex]);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
+3
source to share
1 answer
You must use prepareAsync (); and create another textual representation whose text is buffered. and prepareAsync () is text buffering visible. and on a prepared full sheet preserving text buffering. I hope that work
public void playSong(final int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);
bf = (TextView) this.findViewById(R.id.showbuffering);
try {
mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv.setVisibility(View.INVISIBLE);
bf.setVisibility(View.VISIBLE);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp){
bf.setVisibility(View.INVISIBLE);
tv.setVisibility(View.VISIBLE);
tv.setSelected(true);
mp.start();
tv.setText(naattitles[naatindex]);
tv.setSelected(true);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(o);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}
});
// Set focus to the textview
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
+1
source to share