使用 AVAudio 循环播放 mp3

Posted

技术标签:

【中文标题】使用 AVAudio 循环播放 mp3【英文标题】:Loop mp3 with AVAudio 【发布时间】:2021-04-06 18:33:57 【问题描述】:

我尝试循环播放一个 mp3 文件,以便在播放时更改其音高。 我可以更改音高,但无法循环播放文件。这是我的代码:

import AVKit

class ViewController: UIViewController 

    @IBOutlet weak var speedSlider: UISlider!
    @IBOutlet weak var speedLabel: UILabel!
    let pitchControl = AVAudioUnitTimePitch()
    let engine = AVAudioEngine()
    
    override func viewDidLoad()
        super.viewDidLoad()
        do try playSound(soundName: "audiSound1") catch
    

    @IBAction func speedSlided(_ sender: Any) 
        pitchControl.pitch = speedSlider.value
        speedLabel.text = String(speedSlider.value)
    
    
    func playSound(soundName: String) throws
        guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else  return 
        let file = try AVAudioFile(forReading: url)
        let audioPlayer = AVAudioPlayerNode()
        engine.attach(audioPlayer)
        engine.attach(pitchControl)
        engine.connect(audioPlayer, to: pitchControl, format: nil)
        engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
        audioPlayer.scheduleFile(try! AVAudioFile(forReading: url), at: nil)
        try engine.start()
        audioPlayer.play()
    

为了让它循环,我尝试使用 AVAudioPCMBuffer(),请参阅:

import AVKit

class ViewController: UIViewController 

    @IBOutlet weak var speedSlider: UISlider!
    @IBOutlet weak var speedLabel: UILabel!
    let pitchControl = AVAudioUnitTimePitch()
    let engine = AVAudioEngine()
    
    override func viewDidLoad()
        super.viewDidLoad()
        do try playSound(soundName: "audiSound1") catch
    

    @IBAction func speedSlided(_ sender: Any) 
        pitchControl.pitch = speedSlider.value
        speedLabel.text = String(speedSlider.value)
    
    
    func playSound(soundName: String) throws
        guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else  return 
        let file = try AVAudioFile(forReading: url)
        let audioPlayer = AVAudioPlayerNode()
        
        //New:
        let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: file.fileFormat, frameCapacity: AVAudioFrameCount(file.length))
        try? file.read(into: audioFileBuffer!)

        engine.attach(audioPlayer)
        engine.attach(pitchControl)
        engine.connect(audioPlayer, to: pitchControl, format: nil)
        engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
        audioPlayer.scheduleBuffer(audioFileBuffer!,at: nil, options: .loops, completionHandler: nil) //Changed
        try engine.start()
        audioPlayer.play()
    

但是当我运行项目时,它给了我一个 Thread 1: "required condition is false: isPCMFormat" 错误:/确切的错误信息是:

2021-04-06 20:31:51.824454+0200 avas[67546:8825321] [avae] AVAEInternal.h:76 所需条件为假:[AVAudioBuffer.mm:175:-[AVAudioPCMBuffer initWithPCMFormat:frameCapacity :]: (是PCM格式)] 2021-04-06 20:31:51.832637+0200 avas[67546:8825321] *** 由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“必需条件为 false:isPCMFormat”

我不知道如何让它以另一种(工作)方式循环。有人能帮我吗? :)

非常感谢!

【问题讨论】:

【参考方案1】:

这行得通:

import AVKit

class ViewController: UIViewController 

    @IBOutlet weak var speedSlider: UISlider!
    @IBOutlet weak var speedLabel: UILabel!
    let pitchControl = AVAudioUnitTimePitch()
    let engine = AVAudioEngine()
    
    override func viewDidLoad()
        super.viewDidLoad()
        do try playSound(soundName: "audiSound1") catch
    

    @IBAction func speedSlided(_ sender: Any) 
        pitchControl.pitch = speedSlider.value
        speedLabel.text = String(speedSlider.value)
    
    
    func playSound(soundName: String) throws
        guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else  return 
        let file = try AVAudioFile(forReading: url)
        let audioPlayer = AVAudioPlayerNode()
        let audioFormat = file.processingFormat
        let audioFrameCount = UInt32(file.length)
        let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)
        
        try! file.read(into: audioFileBuffer!)
        let mainMixer = engine.mainMixerNode
        
        engine.attach(audioPlayer)
        engine.attach(pitchControl)
        engine.connect(audioPlayer, to: mainMixer, format: audioFileBuffer?.format)
        engine.connect(audioPlayer, to: pitchControl, format:audioFileBuffer?.format)
        engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
        
        try engine.start()
        audioPlayer.play()
        audioPlayer.scheduleBuffer(audioFileBuffer!, at: nil, options: .loops, completionHandler: nil)
    

【讨论】:

以上是关于使用 AVAudio 循环播放 mp3的主要内容,如果未能解决你的问题,请参考以下文章

iOS,AVPlayer - 循环播放 MP3 片段

html中的语法自动循环播放.mp3文件

JavaScript click 函数播放 mp3,不循环播放

使用pygame不能在循环中连续播放mp3文件?

如何在 AVAudioPlayer 中循环播放多个音频文件?

VB.net 循环播放 mp3 直到选择下一个 IF