xcode 如何使 Progressbar(UISlider) 在我的应用程序中动态工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7481405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make the Progressbar(UISlider) work dynamically in my app?
提问by George
Possible Duplicate:
UISlider to control AVAudioPlayer
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.
我正在尝试为 iPhone 开发一个简单的音频播放器应用程序,我需要实现必须根据歌曲(媒体文件)长度运行的进度条。我怎样才能做到这一点?我可以使用带计时器的滑块吗?提前感谢任何帮助,谢谢。
回答by weston
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
:
如果你正在播放你的音频,AVPlayer
那么你可以使用它的**addPeriodicTimeObserverForInterval**:
方法来更新你的 UISlider。例如,如果滑块的名称是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;
}
}];
And later when you're done:
稍后当你完成时:
[player removeTimeObserver:playbackObserver];
Goodluck!
祝你好运!
回答by Krumelur
I'd say yes, a UISlider is your best friend. That's what iPod uses, too. You'll have to modify t a bit to indicate the progress visually. Make a timer that polls the position of your playback and updates the slider's value accordingly. You might make the slider's minimum value 0 and the maximum value the time of the playback in seconds.
我会说是的,UISlider 是你最好的朋友。这也是 iPod 使用的东西。您必须修改 ta 位以直观地指示进度。制作一个计时器来轮询播放的位置并相应地更新滑块的值。您可以将滑块的最小值设为 0,将最大值设为以秒为单位的播放时间。
Have a look at this post here on Stack Overflow, it has a pretty good demonstration: UISlider with ProgressView combined
看看 Stack Overflow 上的这篇文章,它有一个很好的演示:UISlider with ProgressView combine
回答by Mikayil Abdullayev
If you use AVAudioPlayer then it has a property called currentTime. You can assign its value to the UIProgressBar value like:
如果您使用 AVAudioPlayer,那么它有一个名为 currentTime 的属性。您可以将其值分配给 UIProgressBar 值,例如:
progressBar.value=myPlayer.currentTime;
You'll need to set up a timer to constantly update the progress.
您需要设置一个计时器来不断更新进度。