Seekbar进度计时器Mp3 android

时间:2013-07-05 07:39:44

标签: android

我有一个使用搜索栏播放歌曲的媒体播放器。当歌曲正在播放时,我想将歌曲的持续时间设置为30秒并暂停播放。我的问题是我已经在歌曲的持续时间内实现了一个计时器。例如从00:00,00:01,00:02,00:03 ... 00.10 ... 00.13,00:29,0:30开始,但计时器显示。

07-05 11:36:57.155: I/System.out(30174): progress: 0
07-05 11:37:00.395: I/System.out(30174): progress: 418
07-05 11:37:00.575: I/System.out(30174): progress: 653
07-05 11:37:00.985: I/System.out(30174): progress: 1071
07-05 11:37:01.405: I/System.out(30174): progress: 1515
07-05 11:37:01.580: I/System.out(30174): progress: 1724
07-05 11:37:01.985: I/System.out(30174): progress: 2168
07-05 11:37:02.405: I/System.out(30174): progress: 2612
07-05 11:37:02.580: I/System.out(30174): progress: 2821
07-05 11:37:02.985: I/System.out(30174): progress: 3265
07-05 11:37:03.410: I/System.out(30174): progress: 3709
07-05 11:37:03.575: I/System.out(30174): progress: 3918
07-05 11:37:03.990: I/System.out(30174): progress: 4362
07-05 11:37:04.410: I/System.out(30174): progress: 4780
07-05 11:37:04.595: I/System.out(30174): progress: 4963
07-05 11:37:04.995: I/System.out(30174): progress: 5407
07-05 11:37:05.410: I/System.out(30174): progress: 5851
07-05 11:37:05.590: I/System.out(30174): progress: 6060
07-05 11:37:06.000: I/System.out(30174): progress: 6504
07-05 11:37:06.410: I/System.out(30174): progress: 6949
07-05 11:37:06.585: I/System.out(30174): progress: 7210
07-05 11:37:07.000: I/System.out(30174): progress: 7654
07-05 11:37:07.420: I/System.out(30174): progress: 8124
07-05 11:37:07.585: I/System.out(30174): progress: 8307
07-05 11:37:08.000: I/System.out(30174): progress: 8751
07-05 11:37:08.425: I/System.out(30174): progress: 9195
07-05 11:37:08.590: I/System.out(30174): progress: 9404
07-05 11:37:09.000: I/System.out(30174): progress: 9848
07-05 11:37:09.425: I/System.out(30174): progress: 10266
07-05 11:37:09.590: I/System.out(30174): progress: 10475
07-05 11:37:10.010: I/System.out(30174): progress: 10919
07-05 11:37:10.425: I/System.out(30174): progress: 11363
07-05 11:37:10.600: I/System.out(30174): progress: 11572
07-05 11:37:11.010: I/System.out(30174): progress: 11990
07-05 11:37:11.425: I/System.out(30174): progress: 12408
07-05 11:37:11.590: I/System.out(30174): progress: 12617
07-05 11:37:12.015: I/System.out(30174): progress: 13061
07-05 11:37:12.425: I/System.out(30174): progress: 13505
07-05 11:37:12.600: I/System.out(30174): progress: 13714
07-05 11:37:13.015: I/System.out(30174): progress: 14132
07-05 11:37:13.430: I/System.out(30174): progress: 14576
07-05 11:37:13.600: I/System.out(30174): progress: 14785
07-05 11:37:14.015: I/System.out(30174): progress: 15229
07-05 11:37:14.430: I/System.out(30174): progress: 15673
07-05 11:37:14.605: I/System.out(30174): progress: 15856
07-05 11:37:15.020: I/System.out(30174): progress: 16300
07-05 11:37:15.425: I/System.out(30174): progress: 16744
07-05 11:37:15.605: I/System.out(30174): progress: 16953
07-05 11:37:16.020: I/System.out(30174): progress: 17371
07-05 11:37:16.435: I/System.out(30174): progress: 17815
07-05 11:37:16.610: I/System.out(30174): progress: 18024
07-05 11:37:17.030: I/System.out(30174): progress: 18469
07-05 11:37:17.430: I/System.out(30174): progress: 18939
07-05 11:37:17.610: I/System.out(30174): progress: 19148
07-05 11:37:18.030: I/System.out(30174): progress: 19618
07-05 11:37:18.435: I/System.out(30174): progress: 20088
07-05 11:37:18.615: I/System.out(30174): progress: 20297
07-05 11:37:19.030: I/System.out(30174): progress: 20741
07-05 11:37:19.435: I/System.out(30174): progress: 21185
07-05 11:37:19.615: I/System.out(30174): progress: 21420
07-05 11:37:20.025: I/System.out(30174): progress: 21838
07-05 11:37:20.435: I/System.out(30174): progress: 22256
07-05 11:37:20.615: I/System.out(30174): progress: 22491
07-05 11:37:21.030: I/System.out(30174): progress: 22935
07-05 11:37:21.435: I/System.out(30174): progress: 23353
07-05 11:37:21.615: I/System.out(30174): progress: 23589
07-05 11:37:22.030: I/System.out(30174): progress: 24059
07-05 11:37:22.445: I/System.out(30174): progress: 24529
07-05 11:37:22.615: I/System.out(30174): progress: 24764
07-05 11:37:23.035: I/System.out(30174): progress: 25182
07-05 11:37:23.445: I/System.out(30174): progress: 25626
07-05 11:37:23.620: I/System.out(30174): progress: 25835
07-05 11:37:24.035: I/System.out(30174): progress: 26279
07-05 11:37:24.445: I/System.out(30174): progress: 26723
07-05 11:37:24.625: I/System.out(30174): progress: 26906
07-05 11:37:25.040: I/System.out(30174): progress: 27350
07-05 11:37:25.445: I/System.out(30174): progress: 27794
07-05 11:37:25.620: I/System.out(30174): progress: 28029
07-05 11:37:26.045: I/System.out(30174): progress: 28447
07-05 11:37:26.450: I/System.out(30174): progress: 28865
07-05 11:37:26.625: I/System.out(30174): progress: 29074
07-05 11:37:27.060: I/System.out(30174): progress: 29492
07-05 11:37:27.445: I/System.out(30174): progress: 29884
07-05 11:37:27.625: I/System.out(30174): progress: 30093
07-05 11:37:28.050: I/System.out(30174): progress: 30511
07-05 11:37:28.460: I/System.out(30174): progress: 30668

How can i make it display 00:00,00:01,00:02,00:03... 00.10...00.13, 00:29, to 00:30 only.

这是我的搜索栏的setOnSeekBarChangeListener

      sbMusicProgress.setOnSeekBarCh

angeListener(new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                 // TODO Auto-generated method stub
                    System.out.println("maxvolume"+Integer.toString(maxVolume));
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                 // TODO Auto-generated method stub
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
                 // TODO Auto-generated method stub

                    if (mp!=null){

                        mp.seekTo(progress); 
                        sbMusicProgress.setProgress(progress);
                        System.out.println("progress: "+ progress);

                        //context.txMp3Prog.setText("00:"+String.valueOf(progress));
                        context.txMp3Prog.setText("00:"+String.valueOf(progress));

    }}..

2 个答案:

答案 0 :(得分:4)

您需要将毫秒编码转换为秒,如下所示: 首先在您的活动类上添加方法:

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

并使用以下方法:

  context.txMp3Prog.setText(milliSecondsToTimer(progress));

并且如果mp3播放器持续时间大于分钟的时间显示为01:00,01:01.......

,则使用此方法

答案 1 :(得分:2)

您的progress变量以毫秒表示。您可以将其除以1000,将其转换为秒。

context.txMp3Prog.setText("00:" + String.valueOf(progress/1000));