gpt4 book ai didi

java - java 中带有计时器的 M3 播放器 [快进]

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:24 26 4
gpt4 key购买 nike

我正在我的 Mp3 播放器中制作快进按钮。我已经为此编写了代码,但是有问题如何实现计时器向前跳 5%?我的意思是,当我按下按钮时,计时器应该比总长歌曲向前跳 5%。这是我的 fastForward 方法。

public void FastForward(){
try {
//songTotalLength = fis.available();
fis.skip((long) ((songTotalLength * 0.05)));
} catch (IOException e) {
e.printStackTrace();
}
}

这是按钮方法:

private void jButton3ActionPerformed(ActionEvent e) {
mp3.FastForward();
if(mp3.player.isComplete()){
bar = 100;
}
jProgressBar1.setValue((int)bar);
bar+=5;
}

这个是用于计时器的:

private void setTime(float t) {  
int mili = (int) (t / 1000);
int sec = (mili / 1000) % 60;
int min = (mili / 1000) / 60;
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
end.set(Calendar.MINUTE, 0 + min);
end.set(Calendar.SECOND, 0 + sec);
timer = new javax.swing.Timer(1000, new TimerListener());
percent = (float)100/(min*60+sec);
}

最佳答案

您已经在两个变量 fisbar 中冗余地跟踪进度。为了良好的设计,请使用其中一个变量来确定耗时,而不是使用另一个变量来达到相同的目的。

but have problem how to implement timer to jump 5% forward?

您似乎误解了计时器的用途。根据Timer class documentation ,它:

Fires one or more ActionEvents at specified intervals.

所以计时器并不是为了记录已经过去了多少时间。它只会根据您的配置每秒(每 1000 毫秒)调用 TimerListeneractionPerformed(ActionEvent) 方法。

要获得更完整的答案,请发布您的 TimerListener 类的代码。

注释

  • 您的 setTime(float) 方法似乎需要重复调​​用,因此该方法不应初始化 timer 变量。而是初始化计时器一次,然后让它自行完成其工作。

  • 我假设您希望提供的 float 参数 t 来表示 microseconds .

  • float 数据类型只有 7 位精度。这可能没问题,因为您只对分钟和秒感兴趣,否则 float 最多只能持续大约四个月的秒,然后就会失去准确性。

  • 您似乎希望按钮点击处理程序执行此操作(更快地增加 bar):

private void jButton3ActionPerformed(ActionEvent e) {
mp3.FastForward();
bar+=5; // increment bar before checking complete, and before setting progress
if(mp3.player.isComplete()){
bar = 100;
}
jProgressBar1.setValue((int)bar);
}

关于java - java 中带有计时器的 M3 播放器 [快进],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com