首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带拇指的NSProgressIndicator

带拇指的NSProgressIndicator
EN

Stack Overflow用户
提问于 2021-09-29 13:58:18
回答 1查看 43关注 0票数 0

我正在开发音乐播放器应用程序,我使用了NSProgressIndicator的歌曲时间轴,我如何才能在NSProgressIndicator中添加拇指?我试着添加NSSlider,但是我不能隐藏滑块而只显示拇指,有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2021-09-29 14:49:13

实际上,如果没有更多关于你想要实现什么的信息,给你一个完整的答案并不容易。

最好的做法可能是完全避免使用NSProgressIndicator,而使用NSSlider,正如您在问题中提到的那样。

然后,您可以设置一个periodicTimeObserverForInterval (我假设您正在使用AVPlayer播放您的歌曲?)并根据歌曲时间线上的到达点改变滑块值。

最后,您应该有一个函数,该函数在每次用户更改滑块值时执行,该函数根据用户输入更改时间轴上的当前位置。

下面是我会怎么做:

代码语言:javascript
复制
// Assuming you have a player instance of type AVPlayer in your ViewController
// var player = AVPlayer()

// Move the slider to reflect the current timeline position
func setupPlayer() {
    let interval = CMTime(value: 1, timescale: 2)
    player.addPeriodicTimeObserverForInterval(interval, queue: Dispatch.main) { progress in
        let seconds = CMTimeGetSeconds(progressTime)
        
        if let duration = self.player.currentItem?.duration {
            // Get the number of seconds of the whole file
            let durationSeconds = CMTimeGetSeconds(duration)
            self.slider.value = Float(seconds / durationSeconds)
        }
    }
}

// Change the current timeline position according to the user input
func setupSlider() {
    slider.addTarget(self, action: #selector(slider_valueChanged), for: .valueChanged)
}

@objc func slider_valueChanged() {
    if let duration = player.currentItem?.duration {
        let totalSeconds = CMTimeGetSeconds(duration)
        let value = Float64(slider.value) * totalSeconds
        let seekTime = CMTime(value: Int64(value), timescale: 1)
       
        player.seek(to: seekTime) { completedSeek in 
            // Add code here if you need to do something after the time was seeked
        }
    }
}

如果您使用AVAudioPlayer播放音乐,那么我建议您切换到AVPlayer或使用NStimer,因为AVAudioPlayer不像this answer points out那样附带addPeriodicTimeObserverForInterval方法。

资源

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69377871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档