搜索栏不随音轨移动 运行。无法调试
Seek Bar Does not move with audio track running. Unable to debug
学习了 Youtube 教程。但是我的搜索栏不会随着音频移动。根据教程,我已经添加了处理程序和一个可运行的。我是不是漏了什么
private MediaPlayer music;
Handler handler;
Runnable runnable;
SeekBar seek;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
music = MediaPlayer.create(this, R.raw.gnr_sweet);
seek = (SeekBar) findViewById(R.id.seekBar);
seek.setMax(music.getDuration());
playCycle();
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean userinput) {
if(userinput){
music.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playCycle(){
seek.setProgress(music.getCurrentPosition());
if(music.isPlaying()){
runnable= new Runnable() {
@Override
public void run() {
playCycle();
}
};
handler.postDelayed(runnable, 1000);
}
}
我对 android 开发有点陌生,所以任何类型的输入都可能有用。
我在 playCycle() 方法中遗漏了什么吗?
还有一个播放暂停按钮的 clicklistener,我刚刚从代码片段中删除它以获得 Whosebug
的干净代码
删除此条件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean userinput) {
// if(userinput){
music.seekTo(progress);
// }
}
进度条变化
public void playCycle(){
int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
seek.setProgress(mCurrentPosition);
if(music.isPlaying()){
runnable= new Runnable() {
@Override
public void run() {
playCycle();
}
};
handler.postDelayed(runnable, 1000);
}
}
试试这个
mediaPlayer.seekTo((int) length);
mediaPlayer.start();
startTime = mediaPlayer.getCurrentPosition();
finalTime = mediaPlayer.getDuration();
seekbar.setMax((int) finalTime);
handler.postDelayed(runnable, 100);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (mediaPlayer == null) {
seekBar.setProgress(progress);
} else {
mediaPlayer.seekTo(progress);
seekbar.setProgress(progress);
}
length = progress;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
和
Runnable runnable = new Runnable() {
@Override
public void run() {
startTime = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) startTime);
handler.postDelayed(this, 100);
}
};
对我有用
在我这部分,我创建了一个 Runnable。
那么我认为你需要在调用handler.postDelayed之前先设置seekBar.setProgress。这对我有用。
private Runnable runnable = new Runnable() {
@Override
public void run() {
displayCurrentTime();
seekBar.setProgress((int) player.getCurrentTimeMillis());
mHandler.postDelayed(this, 100);
}
学习了 Youtube 教程。但是我的搜索栏不会随着音频移动。根据教程,我已经添加了处理程序和一个可运行的。我是不是漏了什么
private MediaPlayer music;
Handler handler;
Runnable runnable;
SeekBar seek;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
music = MediaPlayer.create(this, R.raw.gnr_sweet);
seek = (SeekBar) findViewById(R.id.seekBar);
seek.setMax(music.getDuration());
playCycle();
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean userinput) {
if(userinput){
music.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playCycle(){
seek.setProgress(music.getCurrentPosition());
if(music.isPlaying()){
runnable= new Runnable() {
@Override
public void run() {
playCycle();
}
};
handler.postDelayed(runnable, 1000);
}
}
我对 android 开发有点陌生,所以任何类型的输入都可能有用。 我在 playCycle() 方法中遗漏了什么吗? 还有一个播放暂停按钮的 clicklistener,我刚刚从代码片段中删除它以获得 Whosebug
的干净代码删除此条件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean userinput) {
// if(userinput){
music.seekTo(progress);
// }
}
进度条变化
public void playCycle(){
int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
seek.setProgress(mCurrentPosition);
if(music.isPlaying()){
runnable= new Runnable() {
@Override
public void run() {
playCycle();
}
};
handler.postDelayed(runnable, 1000);
}
}
试试这个
mediaPlayer.seekTo((int) length);
mediaPlayer.start();
startTime = mediaPlayer.getCurrentPosition();
finalTime = mediaPlayer.getDuration();
seekbar.setMax((int) finalTime);
handler.postDelayed(runnable, 100);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (mediaPlayer == null) {
seekBar.setProgress(progress);
} else {
mediaPlayer.seekTo(progress);
seekbar.setProgress(progress);
}
length = progress;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
和
Runnable runnable = new Runnable() {
@Override
public void run() {
startTime = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) startTime);
handler.postDelayed(this, 100);
}
};
对我有用
在我这部分,我创建了一个 Runnable。 那么我认为你需要在调用handler.postDelayed之前先设置seekBar.setProgress。这对我有用。
private Runnable runnable = new Runnable() {
@Override
public void run() {
displayCurrentTime();
seekBar.setProgress((int) player.getCurrentTimeMillis());
mHandler.postDelayed(this, 100);
}