android - 如何让搜索栏在歌曲播放时自动移动?

标签 android seekbar android-seekbar

我到处寻找解决我的问题的方法,但我似乎无法解决问题。

如何让搜索栏随着歌曲播放自动滑动?

这就是我到目前为止所拥有的。

ArrayList<String> arrlist = new ArrayList<String>(20);
private Handler seekHandler = new Handler();
ImageButton next, playPause, previous;
SeekBar seekBar;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        getSupportActionBar().hide();

        getInit();
    }

    public void getInit() {

        songCurrentDurationLabel = (TextView) findViewById(R.id.startTime);
        songTotalDurationLabel = (TextView) findViewById(R.id.endTime);

        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.firstsong);

        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        seekBar.setMax(mediaPlayer.getDuration());
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // remove message Handler from updating progress bar
                seekHandler.removeCallbacks(mUpdateTimeTask);
                int totalDuration = mediaPlayer.getDuration();
                int currentPosition = progressToTimer(seekBar.getProgress(), totalDuration);

                // forward or backward to certain seconds
                mediaPlayer.seekTo(currentPosition);

                // update timer progress again
                updateProgressBar();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // remove message Handler from updating progress bar
                seekHandler.removeCallbacks(mUpdateTimeTask);
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mediaPlayer != null && fromUser) {
                    mediaPlayer.seekTo(progress);
//                   mediaPlayer.seekTo(progress * 1000);
                }
            }
        });

        spinner = ((Spinner) findViewById(R.id.spinner1));
        spinner.setAdapter(songAdapter);

        previous = ((ImageButton) findViewById(R.id.previous));
        playPause = ((ImageButton) findViewById(R.id.play));
        next = ((ImageButton) findViewById(R.id.next));

        spinner.setOnItemSelectedListener(this);
        previous.setOnClickListener(this);
        playPause.setOnClickListener(this);
        next.setOnClickListener(this);

        totalDuration = mediaPlayer.getDuration();
        currentDuration = mediaPlayer.getCurrentPosition() / 1000;

        // Displaying Total Duration time
        songTotalDurationLabel.setText("" + milliSecondsToTimer(totalDuration));

        // Displaying time completed playing
        songCurrentDurationLabel.setText("" + milliSecondsToTimer(currentDuration));
    }

    public void updateProgressBar() {
        seekHandler.postDelayed(mUpdateTimeTask, 100);
    }

    private Runnable mUpdateTimeTask = new Runnable() {
        @Override
        public void run() {
            long totalDuration = mediaPlayer.getDuration();
            long currentDuration = mediaPlayer.getCurrentPosition() / 1000;
            int progress = (int) getProgressPercentage(currentDuration, totalDuration);

            // Updating progress bar
            seekBar.setProgress(progress);

            // Running this thread after 100 milliseconds
            seekHandler.postDelayed(this, 100);
        }
    };

    public int progressToTimer(int progress, int totalDuration) {
        int currentDuration = 0;
        totalDuration = (int) (totalDuration / 1000);
        currentDuration = (int) ((((double) progress) / 100) * totalDuration);

        // return current duration in milliseconds
        return currentDuration * 1000;
    }

    public int getProgressPercentage(long currentDuration1, long totalDuration1) {
        Double percentage = (double) 0;

        long currentSeconds = (int) (currentDuration1 / 1000);
        long totalSeconds = (int) (totalDuration1 / 1000);

        // calculating percentage
        percentage = (((double) currentSeconds) / totalSeconds) * 100;

        // return percentage
        return percentage.intValue();
    }

    public String milliSecondsToTimer(long milliseconds) {
        String finalTimerString = "";
        String secondsString = "";

        // Convert total duration into time
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours > 0) {
            finalTimerString = hours + ":";
        }

        // Prepending 0 to seconds if it is one digit
        if (seconds < 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }

        finalTimerString = finalTimerString + minutes + ":" + secondsString;

        // return timer string
        return finalTimerString;
    }

}

我没有包含歌曲列表和所有其他内容,因为我不明白为什么有必要把它放在这里。但无论如何,当我执行这里的操作时,我不会收到任何错误或任何错误,搜索栏不会自动移动,当我尝试将其手动移动到某个位置时,它会立即返回到 0。

最佳答案

首先,您应该定义一个每秒触发的Runnable对象。对于您的情况,每秒都会触发该类。

我将粘贴一些示例代码。这是可运行的类。

Runnable timerRunnable = new Runnable() {

   public void run() {
    // Get mediaplayer time and set the value                           

    // This will trigger itself every one second.
    updateHandler.postDelayed(this, 1000);
   }
};

而且您还应该有一个将触发 Runnable 实例的 Handler

Handler updateHandler = new Handler();

updateHandler.postDelayed(timerRunnable, 1000);

希望该示例对您有所帮助。

关于android - 如何让搜索栏在歌曲播放时自动移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621336/

相关文章:

android - 为什么我的 Dialog Seekbar 崩溃了?空指针异常

带有自定义缩略图的 Android 搜索栏

android - 不需要 Seekbar 焦点

Android WebView 获取内容高度()不返回正确的值

java - 日期选择器的 Android IllegalArgumentException

android - 即使成功登录到 Facebook,session.isOpened() 也会返回 false

android - 媒体播放器搜索栏问题

java - 如何反转 SeekBar 值?

android - 为什么 seekbar 刻度标记显示在拇指前面?

java - 自定义 Firebase 单例类以减少 Android 应用程序开发代码行