1

我有一个用 animateWithDuration 和setAnimationRepeatCount().

我正在尝试在动画块中添加声音效果,女巫正在同步“点击”。但音效只播放一次。我在任何地方都找不到任何提示。

UIView.animateWithDuration(0.5,
                           delay: 0,
                           options: UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.Repeat,
                           animations: {
                               UIView.setAnimationRepeatCount(4)
                               self.audioPlayer.play()
                               self.img_MotronomLight.alpha = 0.1
                           }, completion: nil)

音效应该播放四次,但没有。

音频实现:

//global:
var metronomClickSample = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("metronomeClick", ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()

    audioPlayer = AVAudioPlayer(contentsOfURL: metronomClickSample, error: nil)
    audioPlayer.prepareToPlay()
....
}

@IBAction func act_toggleStartTapped(sender: AnyObject) {
 ....
    UIView.animateWithDuration(....
                    animations: { 
                           UIView.setAnimationRepeatCount(4)
                           self.audioPlayer.play()
                           self.img_MotronomLight.alpha = 0.1
                    }, completion: nil)
}
4

2 回答 2

2

Providing the UIViewAnimationOptions.Repeat option does NOT cause the animation block to be called repeatedly. The animation is repeated on a CoreAnimation level. If you place a breakpoint in the animation block, you'll notice that it is only executed once.

If you want the animation to execute in a loop alongside the sound, create a repeating NSTimer and call the animation / sound from there. Keep in mind that the timer will retain the target, so don't forget to invalidate the timer to prevent retain cycle.

EDIT: Added implementation below

First, we'll need to create the timer, assuming we have an instance variable called timer. This can be done in viewDidLoad: or init method of a view. Once initialized, we schedule for execution with the run loop, otherwise it will not fire repeatedly.

self.timesFired = 0
self.timer = NSTimer(timeInterval: 0.5, target: self, selector:"timerDidFire:", userInfo: nil, repeats: true)
if let timer = self.timer {
    NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
}

The following is the method fired by the timer every interval (in this case 0.5 seconds). Here, you can run your animations and audio playback. Note that UIViewAnimationOptions.Repeat option has been removed since the timer is now responsible for handling the repeating animation and audio. If you only the timer to fire a specific number of times, you can add an instance variable to keep track of times fired and invalidate the timer if the count is above the threshold.

func timerDidFire(timer: NSTimer) {

    /* 
     * If limited number of repeats is required 
     * and assuming there's an instance variable 
     * called `timesFired`
     */
    if self.timesFired > 5 {
        if let timer = self.timer {
            timer.invalidate()
        }
        self.timer = nil
        return
    }
    ++self.timesFired

    self.audioPlayer.play()

    var options = UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.CurveEaseOut;
    UIView.animateWithDuration(0.5, delay: 0, options: options, animations: {
            self.img_MotronomLight.alpha = 0.1
    }, completion: nil)
}
于 2015-08-26T03:53:26.860 回答
0

在没有看到音频播放器的实现的情况下,我能想到的一些事情包括:

  • 音频文件太长,结尾可能有静音,所以它没有播放文件的第一部分,其中有声音

  • 每次都需要将音频文件设置为文件的开头(它可能只是在播放文件末尾的其他 3 次导致没有音频输出)

  • The animation is happening to quickly and the audio doesn't have time to buffer

Hopefully these suggestions help you narrow down the problem but without seeing the implementation of the player, it is really hard to say what the actual problem is.

于 2015-08-25T22:06:17.160 回答