Mediaplayer.getCurrentPosition播放时返回0

时间:2019-04-07 11:31:30

标签: android android-mediaplayer executorservice android-music-player delayed-execution

启动媒体播放器时,正在播放音乐,但是当前位置会暂时返回0(约200毫秒)。我想在播放音乐时在UI上显示当前位置。我试图写出良好的条件,但是if(mediaPlayer.isPlaying())还是不够的。

mediaPlayer.start();
    future=service.scheduleAtFixedRate(new Runnable() {
        public void run() {
            try{
                if(mediaPlayer.isPlaying())
                {
                    startTime = System.currentTimeMillis();
                    curPosition = mediaPlayer.getCurrentPosition();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //to do part
                            setTimeText();
                        }
                    });

                    Log.d("mydebug","cur:"+curPosition);

                    delay = (System.currentTimeMillis() - startTime);

                    nextThread = millPerFrame - delay;
                    if (nextThread<0)
                        nextThread=0;
                }
            }
            catch (Exception e)
            {
                Log.w("mydebug",e.getMessage());
            }
        }
    }, 0, nextThread, TimeUnit.MILLISECONDS);

我每40毫秒称一次此踏步。前7个线程curPosition返回0。它比预期多6倍。在下面,您可以查看我的Logcat结果;

04-07 15:40:23.984 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.022 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.063 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.104 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.145 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.186 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.227 5349-6947/com.example.user.project mydebug: cur:0.0
04-07 15:40:24.272 5349-6947/com.example.user.project mydebug: cur:183.0
04-07 15:40:24.309 5349-6947/com.example.user.project mydebug: cur:183.0
04-07 15:40:24.350 5349-6947/com.example.user.project mydebug: cur:220.0
04-07 15:40:24.391 5349-6947/com.example.user.project mydebug: cur:261.0
04-07 15:40:24.432 5349-6947/com.example.user.project mydebug: cur:302.0

0 个答案:

没有答案