Android Media Player SeekBar 不通过歌曲文件搜索

Android Media Player SeekBar not seeking through songfile

我在使用 Seekbar 时遇到一些问题,加载的第一首歌曲不允许我使用 SeekBar,但是当我加载第二首或第三首歌曲时,SeekBar 会为它们工作。我尝试在加载歌曲 Uri 数据源后初始化 SeekBar,但它仍然表现出相同的行为。

关于如何让 SeekBar 在第一首歌曲上工作有什么想法吗?

谢谢

...

public class MainActivity extends AppCompatActivity {

    final int REQUEST_OPEN_FILE = 1;
    MediaPlayer mediaPlayer = new MediaPlayer();
    Uri audioFileUri;
    /*SeekBar seekBar;*/
    /*TextView SongProgress = findViewById(R.id.SongProgress);*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    ...

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_OPEN_FILE) {
            if (resultCode == RESULT_OK) {
                audioFileUri = data.getData();
                SeekBar seekBar;
                seekBar = findViewById(R.id.seekbar);
                seekBar.setMax(mediaPlayer.getDuration());
                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        seekBar.setProgress(progress);
                        mediaPlayer.seekTo(progress);
                        /*int Minutes=((progress/1000)/60);
                        int Seconds=((progress/1000)%60);
                        int time = Minutes + Seconds;
                        SongProgress.setText(time);*/
                    }


                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });

                    try {
                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource(this, audioFileUri);
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                            Toast.makeText(this, "Was Playing", Toast.LENGTH_SHORT);

                        }
                        else{
                            mediaPlayer.setDataSource(this, audioFileUri);
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                            Toast.makeText(this, "Was not playing", Toast.LENGTH_SHORT);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    ...

    ...
}

好的,我得到了搜索栏进度以随歌曲更新,以便搜索栏在播放时沿着音频文件进行跟踪。这是一个带有处理程序的简单可运行程序,我已将其发布在此处的代码中。您必须有一个可运行的循环来跟踪正在播放的歌曲以更新搜索栏。然后您必须编写一个处理程序来调用该可运行循环。需要注意的一件重要事情是,如果您不在 seekbar onchanged 侦听器中添加 Boolean if(fromUser),则该线程将导致其播放断断续续,因为它也在更新与 seekBar 更新进度函数相关的进度。

    SeekBar seekBar;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        seekBar = (SeekBar) findViewById(R.id.seekbar);

        ...
    }

    ...

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_OPEN_FILE) {
            if (resultCode == RESULT_OK) {
                audioFileUri = data.getData();
                try {

                    if (mediaPlayer.isPlaying()) {
                        seekBar.setProgress(0);
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(this, audioFileUri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        seekBar.setMax(mediaPlayer.getDuration()); // Set the Maximum range of the
                        Toast.makeText(this, "Was Playing", Toast.LENGTH_SHORT);
                    } else {
                        seekBar.setProgress(0);
                        mediaPlayer.setDataSource(this, audioFileUri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        seekBar.setMax(mediaPlayer.getDuration()); // Set the Maximum range of the
                        /*seekBar.getVisibility();
                        seekBar.setVisibility(View.INVISIBLE);*/
                        Toast.makeText(this, "Was not playing", Toast.LENGTH_SHORT);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                int mediaPos = mediaPlayer.getCurrentPosition();
                int mediaMax = mediaPlayer.getDuration();
                seekBar.setMax(mediaMax); // Set the Maximum range of the
                seekBar.setProgress(mediaPos);// set current progress to song's

                handler.removeCallbacks(moveSeekBarThread);
                handler.postDelayed(moveSeekBarThread, 1000);
            }

        }

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser) {
                    seekBar.setProgress(progress);
                    mediaPlayer.seekTo(progress);
                }
                else{}
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }


private Runnable moveSeekBarThread = new Runnable() {
    public void run() {
        if(mediaPlayer.isPlaying()){
            int mediaPos_new = mediaPlayer.getCurrentPosition();
            int mediaMax_new = mediaPlayer.getDuration();
            seekBar.setMax(mediaMax_new);
            seekBar.setProgress(mediaPos_new);
            handler.postDelayed(this, 100); //Looping the thread after 0.1 second
            // seconds
        }else if(mediaPlayer.getCurrentPosition() == mediaPlayer.getDuration()){}
    }
};