I run the stream that I create with this method:

void UIInitialize(){ UIUpdater = new Thread(new Runnable() { @Override public void run() { while (!UIUpdater.isInterrupted()) { //блок паузы потока для того, что-бы включать его только на время работы synchronized (UIMonitor) { while (!isUpdUI) { try { UIMonitor.wait(); } catch (InterruptedException e) { e.printStackTrace(); return; } } } if (mediaPlayer != null) { runOnUiThread(new Runnable() { @Override public void run() { int CurrentProgress = mediaPlayer.getCurrentPosition(); Log.e("Thread", "SendUiUpd: "+CurrentProgress); tvProgress.setText(String.valueOf(CurrentProgress)); } }); } else { isUpdUI=false;} try { Thread.sleep(15); } catch (InterruptedException e) { return; } } } }); UIUpdater.start(); } 

To log

 04-17 18:27:09.907 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2873 04-17 18:27:09.924 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2890 04-17 18:27:09.940 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2906 04-17 18:27:09.957 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 04-17 18:27:09.959 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 04-17 18:27:09.973 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 04-17 18:27:09.988 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 04-17 18:27:10.003 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 04-17 18:27:10.022 27365-27365/etere.wingscontrol E/Thread: SendUiUpd:2926 

By making the delay equal to 1, it was possible to notice the pattern, it slips a gap of 0.5–0.7 seconds correctly with a quick change of numbers, and then hangs for 0.3–0.7 seconds, and then quickly works correctly again (it looks like a problem in buffering?)

As a result of studying the logs, it was noticed that by the time of the pause, getCurrentTime is longer than the actual elapsed time, and the output becomes equal ... or even such a joke ... ( 04-17 20:26:37.559 E/Thread: SendUiUpd: 0 - первый апдейт )

 04-17 20:26:39.212 E/Thread: SendUiUpd: 1658 04-17 20:26:39.218 E/Thread: SendUiUpd: 1664 - рельное время - 1.659 04-17 20:26:39.220 E/Thread: SendUiUpd: 1608 - рельное время - 1.661 04-17 20:26:39.221 E/Thread: SendUiUpd: 1608 - вис на 223 милисек 04-17 20:26:39.222 E/Thread: SendUiUpd: 1608 

Why is updating so uneven and how to fix it?

    1 answer 1

    The fact is that the Thread.sleep(15) method - you stop the main stream for 15ms. I advise you to do this: 1) Create a method

     static void sleep(int sleepTime, TimeUnit unitTime){ try { unitTime.sleep(sleepTime); }catch (InterruptedException ignored){} } 

    2) Put it in Runnable ()

     new Runnable() { @Override public void run(){ sleep(100, TimeUnit.MILLISECONDS) } } 

    A thread in which this method will be overridden in the Runnable run () method will fall asleep by the number of milliseconds specified in the arguments

    • In the code, the external thread UIUpdater is infinite (when unlocked) (it stops thread.sleep), runOnUiThread (block with changes). The problem is that mediaPlayer.getCurrentPosition returns the same values ​​and then sharply updated and a jerk from 2.96 -> 3.23 occurs and so hangs, what the log says, what prevents the 15ms delay if these jerks are visible and with an accuracy of tens of (2.9-> 3.1 visually different update rate)? - great_Suvorov
    • the fact of the matter is that it is not he who suspends, but the main one - Implozia