Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android MediaPlayer getCurrentPosition滞后_Java_Android_Media Player_Android Mediaplayer - Fatal编程技术网

Java Android MediaPlayer getCurrentPosition滞后

Java Android MediaPlayer getCurrentPosition滞后,java,android,media-player,android-mediaplayer,Java,Android,Media Player,Android Mediaplayer,大家好,希望你们能帮我解决这个问题: 我有一个MediaPlayer实例,指向本地存储器中的一个mp3文件,还有一个链接到播放的搜索栏。在我通过更新seekBar的Handler.postdayed或AsynTask启动线程之前,一切都很正常 每次播放该player.getCurrentPosition时;在我的应用程序中可以听到一个小的延迟,这很尴尬。有没有一种方法可以调用此方法而不产生这种效果?有没有其他方法来跟踪回放 他是我启动媒体播放器的代码: Uri uri = Uri.fro

大家好,希望你们能帮我解决这个问题:

我有一个MediaPlayer实例,指向本地存储器中的一个mp3文件,还有一个链接到播放的搜索栏。在我通过更新seekBar的Handler.postdayed或AsynTask启动线程之前,一切都很正常

每次播放该player.getCurrentPosition时;在我的应用程序中可以听到一个小的延迟,这很尴尬。有没有一种方法可以调用此方法而不产生这种效果?有没有其他方法来跟踪回放

他是我启动媒体播放器的代码:

    Uri uri = Uri.fromFile(mp3);
    player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        player.setDataSource(getApplicationContext(), uri);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                playerSeek.setMax(player.getDuration());
                playerSeek.setProgress(0);

                // 3. Start playing the reel.
                onPlayAction(findViewById(R.id.playButton));
                AsyncTask<Void, Integer, Void> playback = prepareSeekUpdater();

                player.start();
                playback.execute();
            }
        });
        player.prepareAsync();
    } catch (IOException e) {
        Log.e(TAG, String.format("Couldn't load the mp3 %s", mp3.getName()), e);
        return;
    }
这是为搜索栏更新创建AsyncTask的代码:

    private AsyncTask<Void, Integer, Void> prepareSeekUpdater(){
    return new AsyncTask<Void, Integer, Void>() {
        /** {@inheritDoc} */
        @Override
        protected Void doInBackground(Void... params) {
            // 1. if the player has started and while the
            // playback is running, the seek bar will be updated and once the playback
            // is finished or interrupted, the thread will be stopped.
            if (null == player) {
                return null;
            }

            int progress;
            while (player.isPlaying() && !isCancelled()) {
                progress = player.getCurrentPosition();
                publishProgress(progress);
                // Wait a little bit.
                try {
                    Thread.sleep(Constants.SEEKER_DELAY);
                } catch (InterruptedException e) {
                    Log.e(TAG, String.format("Couldn't update seek bar."),e);
                }
            }

            return null;
        }

        /** {@inheritDoc} */
        @Override
        protected void onProgressUpdate(Integer... values) {
            playerSeek.setProgress(values[0]);
        }
    };
}