在iPhone中播放声音时如何执行操作? [英] How to perform operations when playing sound in iPhone?

查看:132
本文介绍了在iPhone中播放声音时如何执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AVAudioPlayer在iPhone应用程序中播放MP3;我需要在某些时间(例如30秒1分钟)执行一些操作;有没有一种方法可以根据mp3播放时间来调用回调函数?

I play a MP3 in my iPhone app using AVAudioPlayer; i need to perform some operations at certain times (say 30th seconds, 1 minute); is there a way to invoke callback functions based on mp3 playing time?

推荐答案

我相信最好的解决方案是在开始播放AVAudioPlayer时启动NSTimer.您可以将计时器设置为每半秒左右触发一次.然后,每次您的计时器启动时,请查看音频播放器上的currentTime属性.

I believe the best solution is to start an NSTimer as you start the AVAudioPlayer playing. You could set the timer to fire every half second or so. Then each time your timer fires, look at the currentTime property on your audio player.

为了以一定的间隔执行某项操作,建议您从上次调用计时器回调开始,保留一个实例变量作为回放时间.然后,如果您已在上次回调和此回调之间传递了关键点,请执行操作.

In order to do something at certain intervals, I'd suggest you kept an instance variable for the playback time from last time your timer callback was called. Then if you had passed the critical point between last callback and this, do your action.

因此,使用伪代码,计时器回调:

So, in pseudocode, the timer callback:

  1. 获取AVAudioPlayer的currentTime
  2. 检查currentTime是否大于criticalPoint
  3. 如果是,请检查lastCurrentTime是否小于criticalPoint
  4. 如果也是,请执行操作.
  5. lastCurrentTime设置为currentTime
  1. Get the currentTime of your AVAudioPlayer
  2. Check to see if currentTime is greater than criticalPoint
  3. If yes, check to see if lastCurrentTime is less than criticalPoint
  4. If yes to that too, do your action.
  5. Set lastCurrentTime to currentTime

这篇关于在iPhone中播放声音时如何执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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