gpt4 book ai didi

ios - 让 AVAudioEngine 重复一个声音

转载 作者:搜寻专家 更新时间:2023-10-30 22:31:35 26 4
gpt4 key购买 nike

我一直无法让下面的代码一遍又一遍地重复 audioURL 上的声音。现在它只在 View 打开时播放一次,然后停止。

import UIKit
import AVFoundation
class aboutViewController: UIViewController {

var audioUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!)
var audioEngine = AVAudioEngine()
var myPlayer = AVAudioPlayerNode()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

audioEngine.attachNode(myPlayer)
var audioFile = AVAudioFile(forReading: audioUrl, error: nil)
var audioError: NSError?
audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)
myPlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(&audioError)
myPlayer.play()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

谢谢!

最佳答案

经过数小时的搜索,终于找到了:

class aboutViewController: UIViewController {

var audioEngine: AVAudioEngine = AVAudioEngine()
var audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


let filePath: String = NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!
println("\(filePath)")
let fileURL: NSURL = NSURL(fileURLWithPath: filePath)!
let audioFile = AVAudioFile(forReading: fileURL, error: nil)
let audioFormat = audioFile.processingFormat
let audioFrameCount = UInt32(audioFile.length)
let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)
audioFile.readIntoBuffer(audioFileBuffer, error: nil)

var mainMixer = audioEngine.mainMixerNode
audioEngine.attachNode(audioFilePlayer)
audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format)
audioEngine.startAndReturnError(nil)

audioFilePlayer.play()
audioFilePlayer.scheduleBuffer(audioFileBuffer, atTime: nil, options:.Loops, completionHandler: nil)
}

...
}

关于ios - 让 AVAudioEngine 重复一个声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26852728/

26 4 0
文章推荐: ios - 我如何在 swift ios 的导航栏标题中使用图像
文章推荐: javascript - 我如何告诉 Vue-cli 我的应用程序的入口点在哪里?
文章推荐: html - 在