2014-03-24 79 views
1

我已到處尋找解決我的問題,但我只是不能似乎得到它。如何讓seekbar自動移動歌曲播放?

如何使seekbar自動滑動歌曲播放?

這是我到目前爲止。

ArrayList<String> arrlist = new ArrayList<String>(20); 
private Handler seekHandler = new Handler(); 
ImageButton next, playPause, previous; 
SeekBar seekBar; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout); 
     getSupportActionBar().hide(); 

     getInit(); 
    } 

    public void getInit() { 

     songCurrentDurationLabel = (TextView) findViewById(R.id.startTime); 
     songTotalDurationLabel = (TextView) findViewById(R.id.endTime); 

     mediaPlayer = new MediaPlayer(); 
     mediaPlayer = MediaPlayer.create(this, R.raw.firstsong); 

     seekBar = (SeekBar) findViewById(R.id.seekBar1); 
     seekBar.setMax(mediaPlayer.getDuration()); 
     seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       // remove message Handler from updating progress bar 
       seekHandler.removeCallbacks(mUpdateTimeTask); 
       int totalDuration = mediaPlayer.getDuration(); 
       int currentPosition = progressToTimer(seekBar.getProgress(), totalDuration); 

       // forward or backward to certain seconds 
       mediaPlayer.seekTo(currentPosition); 

       // update timer progress again 
       updateProgressBar(); 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
       // remove message Handler from updating progress bar 
       seekHandler.removeCallbacks(mUpdateTimeTask); 
      } 

      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       if (mediaPlayer != null && fromUser) { 
        mediaPlayer.seekTo(progress); 
//     mediaPlayer.seekTo(progress * 1000); 
       } 
      } 
     }); 

     spinner = ((Spinner) findViewById(R.id.spinner1)); 
     spinner.setAdapter(songAdapter); 

     previous = ((ImageButton) findViewById(R.id.previous)); 
     playPause = ((ImageButton) findViewById(R.id.play)); 
     next = ((ImageButton) findViewById(R.id.next)); 

     spinner.setOnItemSelectedListener(this); 
     previous.setOnClickListener(this); 
     playPause.setOnClickListener(this); 
     next.setOnClickListener(this); 

     totalDuration = mediaPlayer.getDuration(); 
     currentDuration = mediaPlayer.getCurrentPosition()/1000; 

     // Displaying Total Duration time 
     songTotalDurationLabel.setText("" + milliSecondsToTimer(totalDuration)); 

     // Displaying time completed playing 
     songCurrentDurationLabel.setText("" + milliSecondsToTimer(currentDuration)); 
    } 

    public void updateProgressBar() { 
     seekHandler.postDelayed(mUpdateTimeTask, 100); 
    } 

    private Runnable mUpdateTimeTask = new Runnable() { 
     @Override 
     public void run() { 
      long totalDuration = mediaPlayer.getDuration(); 
      long currentDuration = mediaPlayer.getCurrentPosition()/1000; 
      int progress = (int) getProgressPercentage(currentDuration, totalDuration); 

      // Updating progress bar 
      seekBar.setProgress(progress); 

      // Running this thread after 100 milliseconds 
      seekHandler.postDelayed(this, 100); 
     } 
    }; 

    public int progressToTimer(int progress, int totalDuration) { 
     int currentDuration = 0; 
     totalDuration = (int) (totalDuration/1000); 
     currentDuration = (int) ((((double) progress)/100) * totalDuration); 

     // return current duration in milliseconds 
     return currentDuration * 1000; 
    } 

    public int getProgressPercentage(long currentDuration1, long totalDuration1) { 
     Double percentage = (double) 0; 

     long currentSeconds = (int) (currentDuration1/1000); 
     long totalSeconds = (int) (totalDuration1/1000); 

     // calculating percentage 
     percentage = (((double) currentSeconds)/totalSeconds) * 100; 

     // return percentage 
     return percentage.intValue(); 
    } 

    public String milliSecondsToTimer(long milliseconds) { 
     String finalTimerString = ""; 
     String secondsString = ""; 

     // Convert total duration into time 
     int hours = (int) (milliseconds/(1000 * 60 * 60)); 
     int minutes = (int) (milliseconds % (1000 * 60 * 60))/(1000 * 60); 
     int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60)/1000); 
     // Add hours if there 
     if (hours > 0) { 
      finalTimerString = hours + ":"; 
     } 

     // Prepending 0 to seconds if it is one digit 
     if (seconds < 10) { 
      secondsString = "0" + seconds; 
     } else { 
      secondsString = "" + seconds; 
     } 

     finalTimerString = finalTimerString + minutes + ":" + secondsString; 

     // return timer string 
     return finalTimerString; 
    } 

} 

我不包括因爲我不明白爲什麼它會necesary把這裏的歌曲列表和所有其他的東西。但無論如何,當我做我在這裏我沒有得到任何錯誤或任何東西,seekbar只是不會自動移動,當我嘗試將它移動到一個位置,它會馬上回到0.

+0

anyhbody?。,............ – user3439273

回答

0

首先,你應該定義一個將每秒觸發的Runnable對象。對於你的情況,在每一秒都會觸發這個類。

我會粘貼一些示例代碼。這是可運行的類。

Runnable timerRunnable = new Runnable() { 

    public void run() { 
    // Get mediaplayer time and set the value       

    // This will trigger itself every one second. 
    updateHandler.postDelayed(this, 1000); 
    } 
}; 

而且你還應該有一個Handler將觸發Runnable實例。

Handler updateHandler = new Handler(); 

updateHandler.postDelayed(timerRunnable, 1000); 

我希望樣本能幫到你。

+0

如果你看看我發佈的代碼,我已經擁有了所有這些 – user3439273

+0

我在哪裏放置了runnable和handler? – user1172579

0

我使用一個Runnable,它看起來像這樣:

private Runnable mUpdateTimeTask = new Runnable() 
            { 
            public void run() 
             { 
             long currentDuration = mediaPlayer.getCurrentPosition(); 
             long elapsedDuration = mediaPlayer.getDuration() - currentDuration; 

             // Displaying current song progress 
             // playing 
             tvProgressLeft.setText("" + utils.milliSecondsToTimer(currentDuration)); 
             // Displaying remaining time 
             tvProgressRight.setText("" + utils.milliSecondsToTimer(elapsedDuration)); 

             // Updating progress bar 
             int progress = (int) (utils.getProgressPercentage(currentDuration, 
              elapsedDuration)); 
             // Log.d("Progress", ""+progress); 
             songProgressBar.setProgress(progress); 

             // Running this thread after 100 
             // milliseconds 
             handler.postDelayed(this, 100); 
             } 
            }; 

/***/ 

但更重要的是這裏是我所採取的樣本的鏈接。

http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

關於本教程僅是行不通的檢索和從給定文件路徑過濾歌的東西。不知怎的,我無法讓它工作。但我發現了另一個解決方案,它是編寫自己的FileFilter並使用它而不是默認的文件過濾器。