为媒体播放器制作搜索栏。 [英] Make a seek bar for media player.

查看:128
本文介绍了为媒体播放器制作搜索栏。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作使用JMF的媒体播放器,我想使用我自己的控制组件
任何人都可以帮我制作媒体播放器的搜索栏,以便它可以根据滑块位置播放歌曲。 / p>

只是建议我一些逻辑,之后我可以弄清楚编码部分

  if(player!= null){
long durationNanoseconds =
(player.getDuration()。getNanoseconds());
durationbar.setMaximum((int)player.getDuration()。getSeconds());
int duration =(int)player.getDuration()。getSeconds();
int percent = durationbar.getValue();
long t =(durationNanoseconds / duration)* percent;
时间newTime =新时间(t);
player.stop();
player.setMediaTime(newTime);
player.start();
mousedrag = true;

这是代码。现在我该如何让滑块与歌曲一起移动?
当我拖动/点击它时,滑块有效,但它不随着歌曲移动。

解决方案

使用滑块的问题是,当以编程方式移动滑块位置时,它会触发事件。在滑块上触发事件时,通常表示应用程序。必须做点什么,比如移动歌曲位置。效果是一个永无止境的循环。通过设置标志和忽略某些事件可能有办法解决这个问题,但我决定采用不同的方式。



相反,我使用了 JProgressBar 表示轨道中的位置, MouseListener 用于检测用户点击单独位置的时间。使用Swing Timer 更新进度条,每50-200毫秒检查一次轨道位置。当检测到 MouseEvent 时,重新定位轨道。



此栏位于此GUI的右上角。将鼠标悬停在它上方会生成一个工具提示,显示该鼠标位置的轨道时间。




I am making a media player using JMF, I want to use my own control components Can anyone please help me in making a seek bar for media player so that it can play song according to the slider position.

Just suggest me some logic, I can figure out the coding part afterwards

if(player!=null){
    long durationNanoseconds = 
    (player.getDuration().getNanoseconds());
    durationbar.setMaximum((int) player.getDuration().getSeconds());
    int duration=(int) player.getDuration().getSeconds();
    int percent = durationbar.getValue();
    long t = (durationNanoseconds / duration) * percent;
    Time newTime = new Time(t);
    player.stop();
    player.setMediaTime(newTime);
    player.start();
    mousedrag=true;

Here is the code. Now how can I make the slider move along with the song? Slider works when I drag/click on it, but it doesn't move with the song.

解决方案

The problem with using a slider for this is that when the slider position is moved programmatically, it fires events. When an event is fired on a slider, it typically means the app. has to do something, such as move the song position. The effect is a never ending loop. There is probably a way around this by setting flags and ignoring some events, but I decided to go a different way.

Instead I used a JProgressBar to indicate the location in the track, and a MouseListener to detect when the user clicks on a separate position. Update the progress bar use a Swing Timer that checks the track location every 50-200 milliseconds. When a MouseEvent is detected, reposition the track.

The bar can be seen in the upper right of this GUI. Hovering over it will produce a tool tip showing the time in the track at that mouse position.

这篇关于为媒体播放器制作搜索栏。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆