UISlider控制AVAudioPlayer [英] UISlider to control AVAudioPlayer

查看:97
本文介绍了UISlider控制AVAudioPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中实现一个小功能。我目前正在播放声音作为AVAudioPlayers,并且工作正常。我想补充的是用UISlider控制声音的位置(currentTime):有一种简单的方法吗?

I'm trying to implement a little function in my app. I am currently playing sounds as AVAudioPlayers and that works fine. What I would like to add is to control the sound's position (currentTime) with an UISlider: is there a simple way to do it ?

我看了一个Apple项目但是它是非常混乱....你有样品或建议吗?

I looked at an Apple project but it was quite messy....have you got samples or suggestions ?

感谢所有人提前

推荐答案

要扩展paull的答案,你可以将滑块设置为连续的音频播放器的持续时间的最大值,然后添加您的某些对象(可能是视图控制器)作为滑块的 UIControlEventValueChanged 事件的目标;当您收到操作消息时,您可以将 AVAudioPlayer currentTime 属性设置为滑块的值。
您可能还想使用 NSTimer 在音频播放器播放时更新滑块的值; + scheduledTimerWithTimeInterval:target:selector:userInfo:重复:是最简单的方法。

To extend on paull's answer, you'd set the slider to be continuous with a maximum value of your audio player's duration, then add some object of yours (probably the view controller) as a target for the slider's UIControlEventValueChanged event; when you receive the action message, you'd then set the AVAudioPlayer's currentTime property to the slider's value. You might also want to use an NSTimer to update the slider's value as the audio player plays; +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: is the easiest way to do that.

这篇关于UISlider控制AVAudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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