Android Media Player SeekBar无法通过Songfile查找

时间:2018-11-22 22:43:53

标签: java android media seekbar

我无法使Seekbar正常工作,第一首加载的歌曲不会让我使用SeekBar,但是当我加载第二首或第三首歌曲时,SeekBar会为他们工作。在加载歌曲Uri dataSource之后,我尝试初始化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();
                    }
                }
            }
        }

    ...

    ...
}

2 个答案:

答案 0 :(得分:0)

我能够解决自己的问题,在错误的位置初始化了seekBar更改侦听器。将其放在try catch块之后即可解决此问题。现在,我无法在播放歌曲时更新搜索栏。

SeekBar seekBar;

public class MainActivity extends AppCompatActivity implements Runnable {

    ....

    @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()) {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(this, audioFileUri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        seekBar.setMax(mediaPlayer.getDuration());
                        Toast.makeText(this, "Was Playing", Toast.LENGTH_SHORT);
                    } else {
                        mediaPlayer.setDataSource(this, audioFileUri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        seekBar.setMax(mediaPlayer.getDuration());
                        /*seekBar.getVisibility();
                        seekBar.setVisibility(View.INVISIBLE);*/
                        Toast.makeText(this, "Was not playing", Toast.LENGTH_SHORT);
                    }

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

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    ...

}

答案 1 :(得分:0)

好吧,我获得了搜索栏进度,以歌曲进行更新,以便搜索栏在播放时沿着音频文件跟踪。这是一个简单的带有处理程序的可运行程序,我已在此处的代码中发布了该处理程序。您必须有一个可运行的循环才能在播放歌曲时跟踪歌曲,以更新搜索栏。那么您必须编写一个处理程序来调用该可运行循环。要注意的一件事是,如果不将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()){}
    }
};