Android SeekBar错误。在播放音乐时。?

时间:2016-08-12 11:45:30

标签: android android-studio seekbar

**

  

我正在开发一个媒体播放器流媒体应用..在播放歌曲时   seekbar正在根据歌曲移动。但onTouching寻求它是吧   没有从触摸的位置上玩..

**

My Java Code is As Folows ...

public class Player extends Activity implements View.OnTouchListener,MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {

    private ImageButton btnPlay;

    private ImageButton btnNext;
    private ImageButton btnPrevious;

    private ImageButton btnRepeat;
    private ImageButton btnShuffle;

    private TextView songTitleLabel;
    private TextView songCurrentDurationLabel;
    private TextView songTotalDurationLabel;


    private int seekForwardTime = 5000; // 5000 milliseconds
    private int seekBackwardTime = 5000; // 5000 milliseconds
    private int currentSongIndex = 0;
    private boolean isShuffle = false;
    private boolean isRepeat = false;
    private SeekBar songProgressBar;
    private int mediaFileLengthInMilliseconds;
    private final Handler handler = new Handler();

    String AudioURL = "http://f23.wapka-files.com/download/9/e/9/1408248_9e9b050b4bea1c48ba6b7329.mp3/bed5656a469655b9f12e/08-Chundari.Penne-Dulquer.Salmaan.mp3";


    MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
//-=------------------------------------------------------------
        final ImageView iv = (ImageView) findViewById(R.id.image);

        final String imgURL  = "https://i.ytimg.com/vi/NLQHBv6lgXE/maxresdefault.jpg";
        new DownLoadImageTask(iv).execute(imgURL);
        //---------------------------------------------------
        // All player buttons
        btnPlay = (ImageButton) findViewById(R.id.btnPlay);
        //btnForward = (ImageButton) findViewById(R.id.btnForward);
        //btnBackward = (ImageButton) findViewById(R.id.btnBackward);
        btnNext = (ImageButton) findViewById(R.id.btnNext);
        btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);

        btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
        btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);

        songTitleLabel = (TextView) findViewById(R.id.songTitle);
        songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

        songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
        songProgressBar.setMax(99); // It means 100% .0-99
        songProgressBar.setOnTouchListener(null);

        mp = new MediaPlayer();

        // Mediaplayer

        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnBufferingUpdateListener(null);
        mp.setOnCompletionListener(null);

        try {




        } catch (Exception h) {
            Toast.makeText(Player.this, "NO MUSIC HERE...........", Toast.LENGTH_SHORT).show();
        }

    }


        private void primarySeekBarProgressUpdater() {
            songProgressBar.setProgress((int) (((float) mp.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100)); // This math construction give a percentage of "was playing"/"song length"
            if (mp.isPlaying()) {
                Runnable notification = new Runnable() {
                    public void run() {
                        primarySeekBarProgressUpdater();
                    }
                };
                handler.postDelayed(notification, 1000);
            }
        }
        public void clik(View view){
            try {
                mp.setDataSource(AudioURL); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
                mp.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
            } catch (Exception e) {
                e.printStackTrace();
            }

            mediaFileLengthInMilliseconds = mp.getDuration(); // gets the song length in milliseconds from URL

            if (!mp.isPlaying()) {
                mp.start();
                btnPlay.setImageResource(R.drawable.btn_pause);

            } else {
                mp.pause();
                btnPlay.setImageResource(R.drawable.btn_play);

            }

            primarySeekBarProgressUpdater();



        }

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        songProgressBar.setSecondaryProgress(i);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        mp.stop();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
            // Log.d(TAG, "Moved , process data, Moved to :" + seekBarProgress.getProgress());
            songProgressBar.setProgress(songProgressBar.getProgress());
            return false;
        }
        // Log.d(TAG, "Touched , Progress :" + seekBarProgress.getProgress());
        return true;

    }
}

请帮助.....

2 个答案:

答案 0 :(得分:0)

也许您忘了在搜索栏移动后,使用MediaPlayer方法在seekTo(int)上设置位置。例如:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
        songProgressBar.setProgress(songProgressBar.getProgress());
        //seek to the position toucehd
        mp.seekTo(songProgressBar.getProgress());
        return false;
    }
    // Log.d(TAG, "Touched , Progress :" + seekBarProgress.getProgress());
    return true;

}

答案 1 :(得分:0)

您将songProgressBar.setMax(99); max设置为99,将其设置为总持续时间,然后搜索栏将搜索媒体播放器。

添加此行,如代码所示 songProgressBar.setMax(mediaFileLengthInMilliseconds);

       public void clik(View view){
            try {
                mp.setDataSource(AudioURL); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
                mp.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
            } catch (Exception e) {
                e.printStackTrace();
            }
            mediaFileLengthInMilliseconds = mp.getDuration(); // gets the song length in milliseconds from URL

            // Add this line here
            songProgressBar.setMax(mediaFileLengthInMilliseconds); 

            if (!mp.isPlaying()) {
                mp.start();
                btnPlay.setImageResource(R.drawable.btn_pause);
            } else {
                mp.pause();
                btnPlay.setImageResource(R.drawable.btn_play);
            }
            primarySeekBarProgressUpdater();

        }