Seekbar不工作

时间:2013-09-23 04:10:43

标签: android seekbar

我找到了一些搜索栏的代码,可以在线使用媒体播放器:

 @Override
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
     // remove message Handler from updating progress bar
     mHandler.removeCallbacks(mUpdateTimeTask);
 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
     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();
 }

我因为覆盖而得到错误,告诉我他们必须覆盖或实现超类型方法。当我删除它们时,我的代码会编译并运行,但是搜索栏与音乐播放没有联系,因为我可以移动它,但音乐会继续播放。

感谢。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        seekBar.setOnSeekBarChangeListener(this); //this line causes an error
        try {
            mp.setDataSource(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath() + "/SpaceJam.mp3");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onStart(){
        super.onStart();

        btnPlay = (ImageButton) findViewById(R.id.play_pause);
        btnStop = (ImageButton) findViewById(R.id.stop);
        songTitleLabel = (TextView) findViewById(R.id.songname);
        songTitleArtist = (TextView) findViewById(R.id.artist);
        songCurrentDurationLabel = (TextView) findViewById(R.id.current);
        songTotalDurationLabel = (TextView) findViewById(R.id.total);
        albumArt = (ImageView) findViewById(R.id.albumArt);
        res = getResources().getDrawable(R.drawable.album_art);
        utils = new Utilities();
        . . .
}

1 个答案:

答案 0 :(得分:1)

正如prijupaul所提到的,你必须实施OnSeekBarChangeListener。更改您的代码如下:

 public class MainActivity extends Activity implements OnSeekBarChangeListener
 {
     @Override
     protected void onCreate(Bundle savedInstanceState) {

          ...
          seekBar.setOnSeekBarChangeListener(this);
          ...
     }

     @Override
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

     }

     @Override
     public void onStartTrackingTouch(SeekBar seekBar) {

     }

     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {

     }
 }