How to update SeekBar from MediaPlayer running in a service?

In my application, I maid MediaPlayer playing from the Service, to update the SeekBar I assigned a timer task in the 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 on the activity page,

@Override
public void run()
{
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            seekBar.setMax(total);
            seekBar.setProgress(progress);
        }
    });
}

But my problem is that the application is very slow and stuck.

+4
source share
4 answers

Use a related service, if you need a service for exchanging information, check here . Or use libraries like EventBus

0
source

use BroadcastReceiverto update the service of the search form form to activity

,

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //do something based on the intent action
            // UPDATE YOUR UI FROM HERE
        }
    };

,

@Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

Intent intent = new Intent();
intent.setAction("android.mybroadcast");
this.context.sendBroadcast(intent);

→ :)

+4

, ( )

     private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){


      //  update the seek variable here
        }
    };

:

        PlayerConstants.PROGRESSBAR_HANDLER = new Handler(){
             @Override
              public void handleMessage(Message msg){
//get seek bar variable value and set to progress bar
}
        };
     }catch(Exception e){}
+1

android-UniversalMusicPlayer https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/ui/FullScreenPlayerActivity.java.

MediaSession, MediaController + :

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

Source: https://habr.com/ru/post/1605906/


All Articles