Android 自定义警告对话框 mediaplayer seekbar

Android custom alert dialog mediaplayer seekbar

在我的警报对话框中,我想从 SD 卡播放 mp3 并更新搜索栏。它与以下代码一起工作正常。但是 Seekbar Ontouchlistener 不起作用,喜欢快退 mp3。

可能的主要活动:

 public class MainActivity extends Activity {
private MediaPlayer mediaPlayer = new MediaPlayer();
SeekBar seekBarProgress;
ImageButton buttonPlayPause;
private int mediaFileLengthInMilliseconds; 
private final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.popup_ringtones);
            dialog.setTitle("Title...");

            buttonPlayPause = (ImageButton) dialog.findViewById(R.id.play);
            seekBarProgress = (SeekBar) dialog.findViewById(R.id.SeekBarTestPlay);

             seekBarProgress.setProgress(0);
          seekBarProgress.setMax(100); 
              dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mediaPlayer.pause();
                }
                });

              buttonPlayPause.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.stop();
                            mediaPlayer.release();
                            mediaPlayer = new MediaPlayer();
                        }

                        AssetFileDescriptor descriptor = getAssets().openFd("a.mp3");
                        mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                        descriptor.close();

                        mediaPlayer.prepare();
                        mediaPlayer.setVolume(1f, 1f);
                        mediaPlayer.setLooping(true);
                        mediaPlayer.start();
                        primarySeekBarProgressUpdater();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                     mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); 
                }
            });

            dialog.show();
          }
        });

    }


private void primarySeekBarProgressUpdater() {
    seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                primarySeekBarProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}
}

我想要ontouch seekbar更新歌曲,我该如何实现?

你必须像这样在你的搜索栏上实现setOnSeekBarChangeListener

 playerSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    if (fromUser) {
                        mMediaPlayer.seekTo(progress);
                        updateTime();
                    }
                }
            });