如何使Progressbar(UISlider)在我的应用程序中动态工作? [英] How to make the Progressbar(UISlider) work dynamically in my app?

查看:104
本文介绍了如何使Progressbar(UISlider)在我的应用程序中动态工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

UISlider控制AVAudioPlayer

我正在努力开发一个简单的iPhone的音频播放器应用程序,我需要实现必须根据歌曲(mediafile)长度运行的进度条。我怎么能完成这个?我可以使用带计时器的滑块吗?
提前感谢任何帮助,谢谢。

I'm trying to develop a simple audio player app for iPhone,i need to implement the progress bar which has to run according to song(mediafile) length.How can i accomplish this?can i use slider with timer? Any help is appreciated in advance, Thank You.

推荐答案

如果你用<$ c播放你的音频$ c> AVPlayer 然后你可以使用它的 ** addPeriodicTimeObserverForInterval **:方法来更新你的UISlider。例如,如果滑块的名称是 playerScrubber

If you're playing your audio with AVPlayer then you can use its **addPeriodicTimeObserverForInterval**: method to update your UISlider. For example, if the name of your slider is playerScrubber:

    AVPlayerItem *newPlayerItem = [[AVPlayerItem alloc] initWithAsset:<your asset>];
    AVPlayer* player = [[AVPlayer alloc] initWithPlayerItem:newPlayerItem];

    CMTime interval = CMTimeMake(33, 1000);  // 30fps
    id playbackObserver = [player addPeriodicTimeObserverForInterval:interval queue:dispatch_get_current_queue() usingBlock: ^(CMTime time) {
            CMTime endTime = CMTimeConvertScale (player.currentItem.asset.duration, player.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
            if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
                    double normalizedTime = (double) player.currentTime.value / (double) endTime.value;
                    playerScrubber.value = normalizedTime;
            }
        }];

以后当你完成时:

[player removeTimeObserver:playbackObserver];

Goodluck!

这篇关于如何使Progressbar(UISlider)在我的应用程序中动态工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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