我正在创建一个媒体播放器应用程序,我在搜索栏中遇到了问题。 我已经设置了搜寻栏,它可以与音频很好地同步,但是随着搜寻栏的增加,音频会停止一点。
我正在使用此代码来增加搜索栏,这就是问题所在。如果我删除了此代码,音频将流畅播放
@Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}, 0, 1000);
这是我的MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
AudioManager audioManager;
public void playMe(View view){
mediaPlayer.start();
}
public void pauseMe(View view){
mediaPlayer.pause();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer=MediaPlayer.create(this,R.raw.sound);
//get context from audio services
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int MaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int CurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//set context to seekbar
SeekBar volumeRockers = findViewById(R.id.seekBar);
volumeRockers.setMax(MaxVolume);
volumeRockers.setProgress(CurrentVolume);
//set a listener on seelbar
volumeRockers.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
final SeekBar timeline = findViewById(R.id.timeline);
timeline.setMax(mediaPlayer.getDuration());
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mediaPlayer.seekTo(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//customize timeline seekbar
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}, 0, 2000);
}
}