0

アセットにオーディオ ファイルがあり、Windows Media Player のようにシークバーを表示して再生する必要があります。

songProgressBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            mHandler.removeCallbacks(mUpdateTimeTask);

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            mHandler.removeCallbacks(mUpdateTimeTask);
            int totalDuration = mp.getDuration();
            int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

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

            // update timer progress again
            updateProgressBar(false);

        }
    });


 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;
}

持続時間が 1 秒の小さなオーディオでは正しく機能しません。

4

2 に答える 2

1

Utiliesというクラスを作成し、そこに次のコードを追加します。

public class Utilities {

/**
 * Function to convert milliseconds time to
 * Timer Format
 * Hours:Minutes:Seconds
 * */
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;
}

/**
 * Function to get Progress percentage
 * @param currentDuration
 * @param totalDuration
 * */
public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);

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

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

/**
 * Function to change progress to timer
 * @param progress - 
 * @param totalDuration
 * returns current duration in milliseconds
 * */
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;
}

}

MediaPlayer アクティビティで、Utilitiesクラスのオブジェクトを作成します。

private Utilities utils
utils = new Utilities();

シークを更新するバックグラウンド実行可能スレッドになりました

/**
 * Background Runnable thread
 * */
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        long totalDuration = mediaPlayer.getDuration();
        long currentDuration = mediaPlayer.getCurrentPosition();

        // Displaying Total Duration time
        TOTAL_TIME.setText(""+utils.milliSecondsToTimer(totalDuration));
        // Displaying time completed playing
        CURRENT_TIME.setText(""+utils.milliSecondsToTimer(currentDuration));

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

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


/**
 * Update timer on seekbar
 * */
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);        
}   

そして最後 updateProgressBarmediaPlayer.setOnPreparedListener

これで完了です...これがお役に立てば幸いです:) :)

于 2013-10-22T09:02:38.133 に答える
1

これを試してみてください.....

`

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
int totalDuration = mediaPlayer.getDuration();
double val=((double)arg0.getProgress())/((double)arg0.getMax());    
int  currentDuration =(int) (val *totalDuration);
mediaPlayer.seekTo(currentDuration);
}

`

于 2014-04-17T10:32:38.903 に答える