2013-09-23 125 views
0

我發現搜索條與媒體播放聯機工作的一些代碼:搜索欄不工作

@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(); 
} 

我得到因爲覆蓋的錯誤,告訴我說,他們必須覆蓋或實現超方法。當我刪除它們時,我的代碼會編譯並運行,但seekbar與音樂播放無關,因爲我可以移動它,但音樂仍在播放。

謝謝。

@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(); 
     . . . 
} 
+2

一個簡短的問題,您的類是否已經實現了「SeekBar.OnSeekBarChangeListener」。如果沒有,你需要實現它並帶回@override註釋。 – prijupaul

+0

另外,你能展示一些圍繞這些重寫方法的代碼嗎? – kabuto178

+0

當我添加「seekBar.setOnSeekBarChangeListener(this);」,我得到的錯誤:類型SeekBar中的方法setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener)不適用於參數(MainActivity) –

回答

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) { 

    } 
}