Reputation:
In my app i maid a MediaPlayer playing from Service, to update SeekBar i maid a timer task in Service
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, 1 * 1000);
class RemindTask extends TimerTask {
@Override
public void run() {
if (mediaPlayer!=null && mediaPlayer.isPlaying()){
MusicPlayerActivity.progress=mediaPlayer.getCurrentPosition();
MusicPlayerActivity.total=mediaPlayer.getDuration();
}
}
}
and using runnable list i made a run method in activity page,
@Override
public void run()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar.setMax(total);
seekBar.setProgress(progress);
}
});
}
But my issue is that app is very much slow and going stuck.
Upvotes: 4
Views: 5242
Reputation: 433
You can find a working solution in the android-UniversalMusicPlayer reference implementation in https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/ui/FullScreenPlayerActivity.java.
The concept involves setting the current position on a MediaSession instance and then polling the MediaController for changes + calculating the current position:
private void updateProgress() {
if (mLastPlaybackState == null) {
return;
}
long currentPosition = mLastPlaybackState.getPosition();
if (mLastPlaybackState.getState() != PlaybackState.STATE_PAUSED) {
// Calculate the elapsed time between the last position update and now and unless
// paused, we can assume (delta * speed) + current position is approximately the
// latest position. This ensure that we do not repeatedly call the getPlaybackState()
// on MediaController.
long timeDelta = SystemClock.elapsedRealtime() -
mLastPlaybackState.getLastPositionUpdateTime();
currentPosition += (int) timeDelta * mLastPlaybackState.getPlaybackSpeed();
}
mSeekbar.setProgress((int) currentPosition);
}
Upvotes: -1
Reputation: 4743
Use bound service if you need service to activity communication check it here. Or use libraries like EventBus
Upvotes: 0
Reputation: 3873
Put in Service to update the value of seek variable(global and static variable)
private final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
// update the seek variable here
}
};
In your activity:
PlayerConstants.PROGRESSBAR_HANDLER = new Handler(){
@Override
public void handleMessage(Message msg){
//get seek bar variable value and set to progress bar
}
};
}catch(Exception e){}
Upvotes: 1
Reputation: 18978
use BroadcastReceiver
to update seek-bar form service to activity
add broadcast in activity to update your seek bar
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
// UPDATE YOUR UI FROM HERE
}
};
register receiver in activity like below
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
send broadcast from your service -> add below code in your service to call broadcast
Intent intent = new Intent();
intent.setAction("android.mybroadcast");
this.context.sendBroadcast(intent);
pass data in intent -> integer value for seek-bar :)
Upvotes: 4