我可以使用Android媒体播放器播放音频文件。我可以显示进度条和附加的2个按钮来暂停和播放音轨。我的问题是我无法使用进度条控制音轨。我想播放音轨以根据我将进度条拖动到的位置进行更新。
以下是我的代码
public class MyMediaPlayerView extends AppCompatActivity implements View.OnClickListener {
/**
* var for controller
* restore timestamp today 12:11
*/
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_webview);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getInit();
seekUpdation();
}
Runnable run = new Runnable() {
@Override public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
onProgressChanged(seek_bar, seek_bar.getProgress());
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button: text_shown.setText("Playing...");
player.start();
break; case R.id.pause_button: player.pause();
text_shown.setText("Paused...");
}
}
public void onProgressChanged(SeekBar seek_Bar, int progress,
boolean fromUser) {
player.seekTo(progress);
}
}
答案 0 :(得分:3)
我这样做
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)
player.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
答案 1 :(得分:0)
onCreate()
:中
seek_bar = (SeekBar)findViewById(R.id.yourseekbarhere);
seek_bar.setOnSeekBarChangeListener(this);
应该帮助你