SharingMusicPlayer.swift:12:79:无法将类型 'AVAudioPlayer.Type' 的值转换为预期的参数类型 'AVAudioPlayer'

SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type 'AVAudioPlayer'

这是我的单身人士:

import AVFoundation
import Foundation

class SharingMusicPlayer {
    static let sharingMusicPlayer = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer)
    let backgroundMusicPlayer : AVAudioPlayer

    private init(backgroundMusicPlayer: AVAudioPlayer) {
        self.backgroundMusicPlayer = backgroundMusicPlayer
    }

    func playMusic() {
        // open and play an mp3 file...
    }
}

但是我收到这个错误:

SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type 'AVAudioPlayer'

这是另一个 post 我已经调查过,但解决方案似乎不适用于此处:

顺便说一句,我的单例模式基于这篇文章:

https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift

有人有什么建议吗?

更新:

请注意,我知道我可以使用以下代码在我的应用程序中播放声音:

run(SKAction.playSoundFileNamed("MySound.mp3", waitForCompletion: false))

但是我想在这种情况下播放背景音乐,所以上面的方法不起作用。

当您需要传递 AVAudioPlayerinstance 时,您正在传递 type AVAudioPlayer。我猜这应该有效:

改变

static let sharingMusicPlayer = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer)

static let sharingMusicPlayer
    = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer())

更新

这是我最后编译的完整代码

class SharingMusicPlayer {
    static let sharingMusicPlayer
        = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer())
    let backgroundMusicPlayer : AVAudioPlayer

    private init(backgroundMusicPlayer: AVAudioPlayer) {
        self.backgroundMusicPlayer = backgroundMusicPlayer
    }

    func playMusic() {
        // open and play an mp3 file...
    }
}

这是我想出来的,虽然我仍然不确定这是否是在 swift 中做单例的正确方法:

class SharingMusicPlayer {
    static var sharingMusicPlayer = SharingMusicPlayer()
    var backgroundMusicPlayer : AVAudioPlayer?
    
    private init() {
        
    }
    
    func playBackgroundMusic(filename: String) {
        let url = Bundle.main.url(
            forResource: filename, withExtension: nil)
        if (url == nil) {
            print("Could not find file: \(filename)")
            return
        }
        do {
            try self.backgroundMusicPlayer = AVAudioPlayer(contentsOf: url!)
        } catch {
            print("Could not create audio player")
        }
        

        backgroundMusicPlayer!.numberOfLoops = -1
        backgroundMusicPlayer!.prepareToPlay()
        backgroundMusicPlayer!.play()
    }
    
    func stopBackgroundMusic(filename: String) {
        self.backgroundMusicPlayer!.stop()
    }
}

那么这就是我调用函数来启动和停止音乐的方式:

override func touchesBegan(_ touches: Set<UITouch>,
                                    with event: UIEvent?) {
   /* Called when a touch begins */
   for touch: AnyObject in touches {
      //1
      let location = touch.location(in:self)
      //2
      let theNode = self.atPoint(location)

      //... 
       
      if theNode.name == playMusicButton!.name {
           print("The \(playMusicButton!.name!) is touched ")
          SharingMusicPlayer.sharingMusicPlayer.playBackgroundMusic(
            filename: "BeethovenPianoSonataNr15InDmajorOp28Pastoral.mp3")
      }

       if theNode.name == stopMusicButton!.name {
            print("The \(stopMusicButton!.name!) is touched ")
           SharingMusicPlayer.sharingMusicPlayer.stopBackgroundMusic()
       }
   }

请注意,上述按钮位于 SpriteKit 中,因此它们与 Main.storyboard 中的常用 UIButton 对象不同。