使用AVAudioEngine循环音频



我一直在obj-c工作,但我想让我的头脑'快速'圆。我的代码几乎在那里,但我只是不知道如何循环我的wav.播放一次后停止。我已经找到了一些说明,但我还没有找到我的代码的解决方案。我希望任何人都知道答案并可以帮助我。所以问题是:当按下 func playButtonTap 时@IBAction我必须做什么才能使我的wav循环?我会给出我所有的代码,只是为了确定。提前感谢:-(

class ViewController: UIViewController {
@IBOutlet var startAllButton: UIButton!
var audioEngine = AVAudioEngine()
var playerNode = AVAudioPlayerNode()

let timeShift = AVAudioUnitTimePitch()
let pedoMeter = CMPedometer()
let bpm: Float = 110

var avgStarted: Bool = false
var steps: Int = 0
var timer = Timer()
var adjustedBpm: Float = 110
var timerCount = 10 {
didSet {
if timerCount == 0 {
stopCountingSteps()

}
}
}
var lastTap: Date? = nil
@IBOutlet weak var tempoTap: UIButton!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var stepCountLabel: UILabel!
@IBOutlet weak var avgLabel: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
label.text = "110"
audioEngine.attach(playerNode)
audioEngine.attach(timeShift)
audioEngine.connect(playerNode, to: timeShift, format: nil)
audioEngine.connect(timeShift, to: audioEngine.mainMixerNode, format: nil)
audioEngine.prepare()
timerLabel.text = ""
stepCountLabel.text = ""
do {
try audioEngine.start()
} catch {
print("Could not start audio engine")
}
}

@IBAction func sliderValueChanged(_ sender: UISlider) {
label.text = String(sender.value)

self.label.text = String(format:"%.f", sender.value)
adjustedBpm = sender.value
timeShift.rate = adjustedBpm/bpm
}
@IBAction func playButtonTapped(_ sender: Any) {

let url = Bundle.main.url(forResource: "25loop110", withExtension: ".wav")
if let url = url {
do {
let audioFile = try AVAudioFile(forReading: url)

timeShift.rate = adjustedBpm/bpm
playerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)
} catch {
print("could not load audio file")
}
} else {
print("could not load audio file")
}
playerNode.play()
}

问题是这些行:

let audioFile = try AVAudioFile(forReading: url)
playerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)

循环在哪里?无处。该代码播放一次文件。

您不能循环使用 AVAudioEngine 中的文件。使用缓冲区循环。将文件读入缓冲区并调用scheduleBuffer(buffer, at: nil, options: .loops)

相关内容

  • 没有找到相关文章

最新更新