我正在尝试开发带有均衡器的音乐播放器应用程序。我正在使用AVAudioEngine
并附加AVAudioPlayerNode
,但是当我更改歌曲时,上一首歌曲也在播放当前歌曲。更改多首歌曲,同时播放所有歌曲。以前我使用MPMediaItemPlayer
过,但在那种情况下,我无法连接均衡器。我希望我的应用使用均衡器一次播放一首歌曲。
self.AVaudioEngine = AVAudioEngine.init()
self.AVaudioPlayerNode = AVAudioPlayerNode.init()
self.AVaudioUnitEQ = AVAudioUnitEQ(numberOfBands: 10)
self.AVaudioEngine.attach(self.AVaudioPlayerNode)
self.AVaudioEngine.attach(self.AVaudioUnitEQ)
self.AVaudioPlayerNode.scheduleFile(self.AVaudioFile, at: nil, completionHandler: nil)
self.AVaudioEngine.connect(self.AVaudioPlayerNode, to: self.AVaudioUnitEQ, format: self.AVaudioFile.processingFormat)
self.AVaudioEngine.connect(self.AVaudioUnitEQ, to: self.AVaudioEngine.mainMixerNode, format: self.AVaudioFile.processingFormat)
if !self.AVaudioEngine.isRunning {
try! self.AVaudioEngine.start()
}
let sampleRate = self.AVaudioFile.processingFormat.sampleRate / 2
let format = self.AVaudioEngine.mainMixerNode.outputFormat(forBus: 0)
self.AVaudioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(sampleRate), format: format, block:{ (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
})
self.AVaudioPlayerNode.play()
提前致谢!