安排音频文件以备将来使用AVAudioTime播放 [英] Scheduling an audio file for playback in the future with AVAudioTime

查看:201
本文介绍了安排音频文件以备将来使用AVAudioTime播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何在不久的将来正确安排音频文件。我的实际目标是播放多个同步的曲目。

I'm trying to figure out how to correcty schedule an audiofile in the near future. My actual goal is to play multiple tracks synchonized.

因此,如何正确配置 aTime,使其从现在开始大约需要0.3秒。
我想我可能也需要hostTime,但是我不知道如何正确使用它

So how to configure 'aTime' correctly so it starts in about for instance 0.3 seconds from now. I think that I maybe need the hostTime as well, but I don't know how to use that correctly

func createStartTime() -> AVAudioTime? {
    var time:AVAudioTime?
    if let lastPlayer = self.trackPlayerDictionary[lastPlayerKey] {
        if let sampleRate = lastPlayer.file?.processingFormat.sampleRate {
            var sampleTime = AVAudioFramePosition(shortStartDelay * sampleRate )
            time = AVAudioTime(sampleTime: sampleTime, atRate: sampleRate)
        }
    }
    return time
}

这是我用来开始播放的功能:

Here is the function I use to start playback:

func playAtTime(aTime:AVAudioTime?){
    self.startingFrame = AVAudioFramePosition(self.currentTime * self.file!.processingFormat.sampleRate)
    let frameCount = AVAudioFrameCount(self.file!.length - self.startingFrame!)

    self.player.scheduleSegment(self.file!, startingFrame: self.startingFrame!, frameCount: frameCount, atTime: aTime, completionHandler:{ () -> Void in
        NSLog("done playing")//actually done scheduling
    })
    self.player.play()

}


推荐答案

我知道了!

对于我在mach_absolute_time()中填写的hostTime参数,这是计算机/ iPad的现在时间。 AVAudioTime(hostTime:sampleTime:atRate)将sampleTime添加到hostTime并在不久的将来返回一个时间,该时间可用于在同一开始时间安排多个音频片段

for the hostTime parameter I filled in mach_absolute_time(), this is the computer/iPad's 'now' time. the AVAudioTime(hostTime:sampleTime:atRate) adds the sampleTime to the hostTime and gives back a time in the near future that can be used to schedule multiple audio segments at the same startingTime

func createStartTime() -> AVAudioTime? {

    var time:AVAudioTime?
    if let lastPlayer = self.trackPlayerDictionary[lastPlayerKey] {

        if let sampleRate = lastPlayer.file?.processingFormat.sampleRate {

            var sampleTime = AVAudioFramePosition(shortStartDelay * sampleRate )

            time = AVAudioTime(hostTime: mach_absolute_time(), sampleTime: sampleTime, atRate: sampleRate)
        }
    }
    return time

}

这篇关于安排音频文件以备将来使用AVAudioTime播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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