如何消除这些 AVAudioPlayer 消息/错误?

Sou*_*e65 10 macos avfoundation avaudioplayer swift

使用 AVFoundation 的 AVAudioPlayer 时,我遇到了一些控制台消息/错误。

第一个问题是在调用 AVAudioPlayer 实例上的prepareToPlay()方法后,控制台中立即出现两条消息。

'2022-02-26 15:10:46.372023-0600 TestApp [11705:5688753] [插件] AddInstanceForFactory:没有为 id <CFUUID 0x6000039488a0> F8BB1C28-BAE8-11D6-9C31-00039315CD46 注册工厂'

'2022-02-26 15:10:46.416502-0600 TestApp[11705:5688753] 未找到保存的启用硬件采样率转换器首选项'

上述消息仅在实例化 AVAudioPlayer 实例后第一次调用prepareToPlay() 时出现。

第二个问题是当在 AVAudioPlayer 实例上调用 play() 方法时,控制台中会出现以下消息。

'2022-02-26 15:13:53.005976-0600 TestApp[11731:5691158] [aqme] MEMixerChannel.cpp:1639 客户端 <AudioQueueObject@0x7ff03d824600; [0]; play> 发送格式信息时出现错误 2003332927'

每次在实例化的 AVAudioPlayer 实例上调用 play() 方法以及播放成功结束时,都会出现此消息。

这是一个简短的例子:

import Cocoa
import AVFAudio

class ViewController: NSViewController, AVAudioPlayerDelegate {

    var player: AVAudioPlayer?
    var trackNumber = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        startTimer()
    }
    
    @objc func timerMethod() {
        playAudioFile(url: URL(fileURLWithPath: "/Users/Shared/Sounds/countdown.mp3"))
    }
    
    func startTimer() {
        let _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerMethod), userInfo: nil, repeats: false)
    }

    func playAudioFile(url: URL) {
        player = try! AVAudioPlayer(contentsOf: url)
        player?.delegate = self
        player?.prepareToPlay()
        player?.play()
    }

    @objc func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully: Bool) {
        if trackNumber == 0 {
            playAudioFile(url: URL(fileURLWithPath: "/Users/Shared/Sounds/implode.mp3"))
            trackNumber += 1
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的环境是 Xcode 13.2.1/Swift 5.5.2 和 macOS 12.2.1,在 2015 年末的 Intel iMac 上运行。

作为参考,我通读了这些答案。前两个适用于 iOS,Apple 论坛似乎适用于 macOS。他们都没有适合我的解决方案。

  1. iOS 1
  2. iOS 2
  3. 苹果论坛