【问题标题】:How to call an action while AVAudio is playing at a specific time?如何在特定时间播放音频时调用动作?
【发布时间】:2015-07-17 16:50:16
【问题描述】:

我想知道如何在 AVAudio 在特定时间播放时调用动作。

假设 AVAudio 播放“19 秒”,然后调用任何你想要的动作。

【问题讨论】:

    标签: ios swift cocoa-touch avfoundation avaudioplayer


    【解决方案1】:

    你可以使用 AVPlayer 来做到这一点,AVPlayer 有一个方法来添加 addPeriodicTimeObserverForInterval。请注意,时间间隔为 19, 1,这意味着该块每 19 秒被调用一次。你可以让你只在前 19 秒内执行你的任务。

    player = AVPlayer(URL: NSBundle.mainBundle().URLForResource("UpTown", withExtension: "mp3")!)
    
    player.addPeriodicTimeObserverForInterval(CMTimeMake(19, 1), queue: dispatch_get_main_queue()) { time in
    // perform your task here 
    }
    
    player.play()
    

    【讨论】:

    • 它不会让我继承 AVPlayer 类。我收到一个错误“从类 'UIViewController' 和 'AVPlayer 的多重继承”
    • 为什么要继承 AVPlayer ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多