Android MediaPlayer seekBar无法正常工作

时间:2018-09-26 00:53:22

标签: java android android-studio android-mediaplayer

当前,我正在开发媒体播放器,我需要seekBar一起工作,目前,一切正常!但是,当我触摸导航栏的确定位置时,媒体播放器必须遵循并将歌曲设置为该确定位置,但歌曲和seekBar 返回到开始,但我不知道为什么会这样。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Handler mHandler = new Handler();

    public TextView song_detail;
    public TextView time1;
    public TextView time2;
    private String player_status = "playing";
    private ImageButton player_img;

    public static SeekBar seekBar;

    private static MediaPlayer mediaPlayer;

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        song_detail = findViewById(R.id.song_detail);
        song_detail.setVisibility(View.GONE);

        time1 = findViewById(R.id.time_1);
        time2 = findViewById(R.id.time_2);

        seekBar = findViewById(R.id.seekBar);

        final Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                if(mediaPlayer != null){
                    int CurrentPosition = mediaPlayer.getCurrentPosition();
                    String m_1;
                    String s_1;
                    seekBar.setProgress(CurrentPosition/1000);
                    final int minutes_1 = (CurrentPosition/1000)/60;
                    final int seconds_1 = ((CurrentPosition/1000)%60);

                    if (minutes_1 < 10) {
                        m_1 = "0" + minutes_1;
                    } else {
                        m_1 = "" + minutes_1;
                    }

                    if (seconds_1 < 10) {
                        s_1 = "0" + seconds_1;
                    } else {
                        s_1 = "" + seconds_1;
                    }
                    time1.setText(m_1 + ":" + s_1);


                    int Duration = mediaPlayer.getDuration();
                    String m_2;
                    String s_2;
                    final int minutes_2 = (Duration/1000)/60;
                    final int seconds_2 = ((Duration/1000)%60);

                    if (minutes_2 < 10) {
                        m_2 = "0" + minutes_2;
                    } else {
                        m_2 = "" + minutes_2;
                    }

                    if (seconds_2 < 10) {
                        s_2 = "0" + seconds_2;
                    } else {
                        s_2 = "" + seconds_2;
                    }

                    time2.setText(m_2 + ":" + s_2);

                }
                mHandler.postDelayed(this, 1000);
            }
        };
        mRunnable.run();


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mediaPlayer != null && fromUser){
                    Toast.makeText(getApplicationContext(), String.valueOf(progress), Toast.LENGTH_LONG).show();
                    mediaPlayer.seekTo(progress);
                    seekBar.setProgress(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

    public void initAudio(final Context context, final String url) {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(context, Uri.parse(url));
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //Toast.makeText(context, "TEST", Toast.LENGTH_LONG).show();
                killMediaPlayer();
            }
        });
        seekBar.setMax(mediaPlayer.getDuration()/1000);
        mediaPlayer.start();
    }

    public void killMediaPlayer() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void pauseAudio() {
        if (!(mediaPlayer == null)) {
            mediaPlayer.pause();
        }
    }
    public static void startAudio() {
        if (!(mediaPlayer == null)) {
            mediaPlayer.start();
        }
    }
}

这是我用于 mediaPlayer seekBar

的代码的一部分
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mediaPlayer != null && fromUser){
                    Toast.makeText(getApplicationContext(), String.valueOf(progress), Toast.LENGTH_LONG).show();
                    mediaPlayer.seekTo(progress);
                    seekBar.setProgress(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

但是,正如我所说的,不是mediaPlayer从我单击的位置开始查找并继续播放音频,而是直接将音频返回到开头。我在做什么错了?

谢谢。

1 个答案:

答案 0 :(得分:1)

progress的最大值为100。seekTo函数花费的时间以毫秒为单位。这就是为什么它寻求(几乎)音频的开始。

因此,您需要执行以下操作,而不是mediaPlayer.seekTo(progress);

long newTime = (progress/100.0) * total_audio_duration_in_millisecond; 
mediaPlayer.seekTo(newTime);