オンラインでメディアプレーヤーと連携するためのシークバーのコードをいくつか見つけました。
@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();
. . .
}