gpt4 book ai didi

ios - 无法演奏非常简单的音调

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

我正在关注 this question ,但我尝试使用 AVAudioPCMBuffer 播放的音调没有播放。代码非常简单:

class Player: NSObject {
var engine = AVAudioEngine()
var player = AVAudioPlayerNode()
var mixer: AVAudioMixerNode!
var buffer: AVAudioPCMBuffer!

override init() {
mixer = engine.mainMixerNode
buffer = AVAudioPCMBuffer(pcmFormat: player.outputFormat(forBus: 0), frameCapacity: 100)
buffer.frameLength = 100

let sr = mixer.outputFormat(forBus: 0).sampleRate
let nChannels = mixer.outputFormat(forBus: 0).channelCount

var i = 0
while i < Int(buffer.frameLength) {
let val = sin(441 * Double(i) * Double.pi / sr)
buffer.floatChannelData?.pointee[i] = Float(val * 0.5)
i += Int(nChannels)
}

engine.attach(player)
engine.connect(player, to: mixer, format: player.outputFormat(forBus: 0))

engine.prepare()
}

func play() {
do {
try engine.start()
} catch {
print(error)
}

player.scheduleBuffer(buffer, at: nil, options: .loops) {
print("Played!")
}

player.play()
}
}

但是,出于某种原因,iPhone 没有发出任何声音。在我的 ViewController 中,我有这个:

class ViewController: UIViewController {
var player = Player()

override func viewDidAppear(_ animated: Bool) {
player.play()
}

}

如您所见,player 是一个类变量,因此不应从内存中释放它。

当我在我的物理设备(iPhone 6s iOS 11)上运行该应用程序时,它无法运行,但它可以在模拟器上运行。为什么这不会发出任何声音,怎么会我修理它?

提前致谢!

最佳答案

确保您的设备未处于静音模式。

我刚刚创建了一个项目,您可以自行下载进行测试:https://github.com/mugx/TestSound

关于ios - 无法演奏非常简单的音调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47707077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com