K
K
Kostya Bakay2016-04-25 04:04:19
Android
Kostya Bakay, 2016-04-25 04:04:19

How can I wait until VKRequest is completed?

I have a VKRequest that starts playing an online song. I also have a SeekBar that updates the playback progress. The problem is that the method to update the SeekBar is called before my VKRequest is executed, which will start the song. And in more detail, first request.executeWithListener is executed (without onComplete), then my method for updating the SeekBar, and then the code in the onComplete method starts running. How can I wait until the onComplete method is finished (preferably with some kind of UI update in onProgress) and only then start executing the listenSeekBar method?

request.executeWithListener(new VKRequest.VKRequestListener() {
            @Override
            public void onComplete(VKResponse response) {
                super.onComplete(response);
                // тут прописана реализация запуска на проигрывание песни
            }

            @Override
            public void onProgress(VKRequest.VKProgressType progressType, long bytesLoaded, long bytesTotal) {
                super.onProgress(progressType, bytesLoaded, bytesTotal);
            }
        });

    // метод для обновления SeekBar в другом классе
    private void listenSeekBar() {
        AppData.sAudioPlayer.getMediaPlayer()
                .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    public void onPrepared(MediaPlayer mp) {
                        mTotalDuration = mp.getDuration();
                        mTimelineSeekBar.setMax(mTotalDuration);
                        mHandler.postDelayed(runnable, 100);
                    }
                });
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kostya Bakay, 2016-05-01
@kostyabakay

The problem was solved in this way - I passed the fragment to the class where the audio file is launched and from there I called the method to update the UI, namely SeekBar, in another fragment.

D
dchuvasov, 2016-04-25
@dchuvasov

onComplete is called "later" because it is a callback, and the executeWithListener request itself is asynchronous.
What prevents calling listenSeekBar() in onComplete?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question