计算音乐搜索栏的轨道位置
Calculating track position for a music seek bar
如何计算音乐曲目在搜索栏上的位置?
我创建了一个普通的音乐播放器 GUI 作为 midi 音频播放器的演示,我想为它创建一个搜索栏。我已经为它创建了一切,但我一直在努力计算搜索位置指示器的 x 位置。
播放了 2 秒(尺寸:640x480)(TrackPosX:260/475,其中 475 是搜索栏 endX)
我的渲染方法的片段
int trackPosY = this.getHeight()/2+this.getHeight()/4+40; //Y coordinate of seek bar/seek indicator/end and begin points
//Draws seek bar with end points
g.drawLine(this.getWidth()/4, trackPosY, this.getWidth()/2+this.getWidth()/4, trackPosY); //Draws Seek bar
g.drawLine(this.getWidth()/4, trackPosY-10, this.getWidth()/4, trackPosY+10); //Draws Begin point
g.drawLine(this.getWidth()/2+this.getWidth()/4+1, trackPosY-10, this.getWidth()/2+this.getWidth()/4+1, trackPosY+10); //Draws End line (Not sure why I have to add 1 to the x but if I don't the seek bar breaches the width of the end line.)
if(midiPlayer != null && (midiPlayer.getState() == SimpleMidiPlayer.PLAY || midiPlayer.getState() == SimpleMidiPlayer.PAUSE)){
int trackLineLength = (this.getWidth()/4+this.getWidth()/2 - this.getWidth()/4); //Length of the line, not sure if necessary
int trackPosX = (int) ((midiPlayer.getTickPosition()/trackLineLength*midiPlayer.getTicksTotal()/trackLineLength) + (this.getWidth()/4)); //Attempt at finding X Coordinate
g.fillOval( trackPosX-3, trackPosY-3, 6, 6); //Draws Seek position indicator
debugTrackPos.setDebugText("TrackPosX: "+trackPosX+"/"+(this.getWidth()/4+this.getWidth()/2)+" Tick: "+midiPlayer.getTickPosition()+"/"+midiPlayer.getTicksTotal()); //Debugging purposes (top left of pic)
}
看起来你很接近,基本情况是这样的:
ratio = var/total;
position = (max-min)*ratio +min
您的变量是:
var = midiPlayer.getTickPosition()
total = midiPlayer.getTicksTotal()
ratio = midiPlayer.getTickPosition()/midiPlayer.getTicksTotal()
(you already have max-min defined as trackLineLength)
max = this.getWidth()/4+this.getWidth()/2
min = this.getWidth()/4
所以使用这些变量你会得到这样的结果:
int trackPosX = (int)( ((double)midiPlayer.getTickPosition())/midiPlayer.getTicksTotal()
* trackLineLength + this.getWidth()/4)
请注意,我在 getTickPosition/getTicksTotal
的分区中添加了 (double)
cast。我的猜测是这些是返回的整数值,除非您将其转换为双精度,否则每次除法都会导致 0。
如何计算音乐曲目在搜索栏上的位置?
我创建了一个普通的音乐播放器 GUI 作为 midi 音频播放器的演示,我想为它创建一个搜索栏。我已经为它创建了一切,但我一直在努力计算搜索位置指示器的 x 位置。
播放了 2 秒(尺寸:640x480)(TrackPosX:260/475,其中 475 是搜索栏 endX)
我的渲染方法的片段
int trackPosY = this.getHeight()/2+this.getHeight()/4+40; //Y coordinate of seek bar/seek indicator/end and begin points
//Draws seek bar with end points
g.drawLine(this.getWidth()/4, trackPosY, this.getWidth()/2+this.getWidth()/4, trackPosY); //Draws Seek bar
g.drawLine(this.getWidth()/4, trackPosY-10, this.getWidth()/4, trackPosY+10); //Draws Begin point
g.drawLine(this.getWidth()/2+this.getWidth()/4+1, trackPosY-10, this.getWidth()/2+this.getWidth()/4+1, trackPosY+10); //Draws End line (Not sure why I have to add 1 to the x but if I don't the seek bar breaches the width of the end line.)
if(midiPlayer != null && (midiPlayer.getState() == SimpleMidiPlayer.PLAY || midiPlayer.getState() == SimpleMidiPlayer.PAUSE)){
int trackLineLength = (this.getWidth()/4+this.getWidth()/2 - this.getWidth()/4); //Length of the line, not sure if necessary
int trackPosX = (int) ((midiPlayer.getTickPosition()/trackLineLength*midiPlayer.getTicksTotal()/trackLineLength) + (this.getWidth()/4)); //Attempt at finding X Coordinate
g.fillOval( trackPosX-3, trackPosY-3, 6, 6); //Draws Seek position indicator
debugTrackPos.setDebugText("TrackPosX: "+trackPosX+"/"+(this.getWidth()/4+this.getWidth()/2)+" Tick: "+midiPlayer.getTickPosition()+"/"+midiPlayer.getTicksTotal()); //Debugging purposes (top left of pic)
}
看起来你很接近,基本情况是这样的:
ratio = var/total;
position = (max-min)*ratio +min
您的变量是:
var = midiPlayer.getTickPosition()
total = midiPlayer.getTicksTotal()
ratio = midiPlayer.getTickPosition()/midiPlayer.getTicksTotal()
(you already have max-min defined as trackLineLength)
max = this.getWidth()/4+this.getWidth()/2
min = this.getWidth()/4
所以使用这些变量你会得到这样的结果:
int trackPosX = (int)( ((double)midiPlayer.getTickPosition())/midiPlayer.getTicksTotal()
* trackLineLength + this.getWidth()/4)
请注意,我在 getTickPosition/getTicksTotal
的分区中添加了 (double)
cast。我的猜测是这些是返回的整数值,除非您将其转换为双精度,否则每次除法都会导致 0。