Java Android:音乐播放时,如何让seekbar工作?
我到处寻找来解决我的问题,但我似乎找不到答案 它在继续 如何使seekbar随歌曲播放自动滑动 这就是我目前所拥有的Java Android:音乐播放时,如何让seekbar工作?,java,android,seekbar,android-seekbar,Java,Android,Seekbar,Android Seekbar,我到处寻找来解决我的问题,但我似乎找不到答案 它在继续 如何使seekbar随歌曲播放自动滑动 这就是我目前所拥有的 ArrayList arrlist=新的ArrayList(20); 私有处理程序seekHandler=新处理程序(); 图像按钮下一步,播放暂停,上一步; SeekBar-SeekBar; 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.l
ArrayList arrlist=新的ArrayList(20);
私有处理程序seekHandler=新处理程序();
图像按钮下一步,播放暂停,上一步;
SeekBar-SeekBar;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
getSupportActionBar().hide();
getInit();
}
public void getInit(){
songCurrentDurationLabel=(TextView)findViewById(R.id.startTime);
songTotalDurationLabel=(TextView)findViewById(R.id.endTime);
mediaPlayer=新的mediaPlayer();
mediaPlayer=mediaPlayer.create(this,R.raw.firstsong);
seekBar=(seekBar)findViewById(R.id.seekBar1);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekbarchaneListener(新的seekBar.onSeekbarchaneListener(){
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
//从更新进度条中删除消息处理程序
请参见handler.removeCallbacks(mUpdateTimeTask);
int totalDuration=mediaPlayer.getDuration();
int currentPosition=progressToTimer(参见kbar.getProgress(),totalDuration);
//向前或向后到某一秒
mediaPlayer.seekTo(当前位置);
//再次更新计时器进度
updateProgressBar();
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
//从更新进度条中删除消息处理程序
请参见handler.removeCallbacks(mUpdateTimeTask);
}
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser){
if(mediaPlayer!=null&&fromUser){
mediaPlayer.seekTo(进度);
//mediaPlayer.seekTo(进度*1000);
}
}
});
微调器=((微调器)findViewById(R.id.spinner1));
spinner.setAdapter(songAdapter);
previous=((ImageButton)findViewById(R.id.previous));
播放暂停=((图像按钮)findViewById(R.id.play));
next=((ImageButton)findViewById(R.id.next));
spinner.setOnItemSelectedListener(此);
previous.setOnClickListener(此);
playPause.setOnClickListener(此);
next.setOnClickListener(this);
totalDuration=mediaPlayer.getDuration();
currentDuration=mediaPlayer.getCurrentPosition()/1000;
//显示总持续时间
songTotalDurationLabel.setText(“+millissecondstotimer(totalDuration));
//显示播放完成的时间
songCurrentDurationLabel.setText(“+millissecondstotimer(currentDuration));
}
public void updateProgressBar(){
参见khandler.postDelayed(mUpdateTimeTask,100);
}
private Runnable mUpdateTimeTask=new Runnable(){
@凌驾
公开募捐{
long totalDuration=mediaPlayer.getDuration();
长currentDuration=mediaPlayer.getCurrentPosition()/1000;
int progress=(int)getProgressPercentage(currentDuration,totalDuration);
//更新进度条
seekBar.setProgress(进度);
//在100毫秒后运行此线程
参见Khandler.postDelayed(本,100);
}
};
public int progressToTimer(int progress,int totalDuration){
int currentDuration=0;
总持续时间=(整数)(总持续时间/1000);
当前持续时间=(整数)((双)进度)/100)*总持续时间;
//返回当前持续时间(毫秒)
返回电流持续时间*1000;
}
public int getProgressPercentage(长currentDuration1,长totalDuration1){
双倍百分比=(双倍)0;
长currentSeconds=(int)(currentDuration1/1000);
长总秒数=(整数)(总持续时间1/1000);
//计算百分比
百分比=((双)当前秒数)/总秒数)*100;
//回报率
返回百分比;
}
公共字符串毫秒计时器(长毫秒){
字符串finalTimerString=“”;
字符串secondssString=“”;
//将总持续时间转换为时间
整数小时=(整数)(毫秒/(1000*60*60));
整数分钟=(整数)(毫秒%(1000*60*60))/(1000*60);
整数秒=(整数)((毫秒%(1000*60*60))%(1000*60)/1000);
//如果有,请加上小时数
如果(小时数>0){
finalTimerString=hours+“:”;
}
//如果是一位数字,则在0到秒之前加上前缀
如果(秒<10){
secondsString=“0”+秒;
}否则{
secondsString=“”+秒;
}
finalTimerString=finalTimerString+minutes+“:”+secondsString;
//返回计时器字符串
返回finalTimerString;
}
我没有包括歌曲列表和所有其他我看不到的东西
为什么有必要把它放在这里。但不管怎样,当我做我想做的事时
在这里我没有得到任何错误或任何东西,seekbar就是没有
自动移动,当我尝试手动将其移动到某个位置时
返回到0。U setseekBar.setMax(mediaPlayer.getDuration())代码>其中持续时间~7位数值。
在那之后,你计算百分比~2位数。
您需要设置seekBar.setMax(100)
或不计算百分比并按原样设置进度 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
ArrayList<String> arrlist = new ArrayList<String>(20);
private Handler seekHandler = new Handler();
ImageButton next, playPause, previous;
SeekBar seekBar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
getSupportActionBar().hide();
getInit();
}
public void getInit() {
songCurrentDurationLabel = (TextView) findViewById(R.id.startTime);
songTotalDurationLabel = (TextView) findViewById(R.id.endTime);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.firstsong);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
seekHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mediaPlayer.getDuration();
int currentPosition = progressToTimer(seekBar.getProgress(), totalDuration);
// forward or backward to certain seconds
mediaPlayer.seekTo(currentPosition);
// update timer progress again
updateProgressBar();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
seekHandler.removeCallbacks(mUpdateTimeTask);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {
mediaPlayer.seekTo(progress);
// mediaPlayer.seekTo(progress * 1000);
}
}
});
spinner = ((Spinner) findViewById(R.id.spinner1));
spinner.setAdapter(songAdapter);
previous = ((ImageButton) findViewById(R.id.previous));
playPause = ((ImageButton) findViewById(R.id.play));
next = ((ImageButton) findViewById(R.id.next));
spinner.setOnItemSelectedListener(this);
previous.setOnClickListener(this);
playPause.setOnClickListener(this);
next.setOnClickListener(this);
totalDuration = mediaPlayer.getDuration();
currentDuration = mediaPlayer.getCurrentPosition() / 1000;
// Displaying Total Duration time
songTotalDurationLabel.setText("" + milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText("" + milliSecondsToTimer(currentDuration));
}
public void updateProgressBar() {
seekHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
@Override
public void run() {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition() / 1000;
int progress = (int) getProgressPercentage(currentDuration, totalDuration);
// Updating progress bar
seekBar.setProgress(progress);
// Running this thread after 100 milliseconds
seekHandler.postDelayed(this, 100);
}
};
public int progressToTimer(int progress, int totalDuration) {
int currentDuration = 0;
totalDuration = (int) (totalDuration / 1000);
currentDuration = (int) ((((double) progress) / 100) * totalDuration);
// return current duration in milliseconds
return currentDuration * 1000;
}
public int getProgressPercentage(long currentDuration1, long totalDuration1) {
Double percentage = (double) 0;
long currentSeconds = (int) (currentDuration1 / 1000);
long totalSeconds = (int) (totalDuration1 / 1000);
// calculating percentage
percentage = (((double) currentSeconds) / totalSeconds) * 100;
// return percentage
return percentage.intValue();
}
public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}