SharingMusicPlayer.swift:12:79:无法转换“AVAudioPlayer.Type”类型的值;预期参数类型“AVAudioPlayer”

发布于 2025-01-15 07:34:02 字数 1268 浏览 3 评论 0原文

这就是我在单例中的内容:

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'

这是我调查过的另一篇文章,但该解决方案似乎不适用于此处:

“无法将“AVAudioPlayer.Type”类型的值转换为预期的参数类型”

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

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

有人有什么建议吗?

更新:

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

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

但我想在这种情况下播放背景音乐,因此上述方法不起作用。

This is what I have in my singleton:

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...
    }
}

But I am getting this error:

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

Here is another post I have investigated but the solution does not appear to apply here:

"Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type"

BTW, I based my singleton pattern on this article:

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

Does anyone have any suggestions?

UPDATE:

Please note that I realize that I can play a sound in my app using this code:

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

But I want to play background music in this case so the above doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2025-01-22 07:34:02

当您需要传递 AVAudioPlayer实例时,您将传递 type AVAudioPlayer。我想这应该有效:

更改

static let sharingMusicPlayer = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer)

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

Update

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

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...
    }
}

You are passing it the type AVAudioPlayer when you need to pass an instance of AVAudioPlayer. This should work I guess:

Change

static let sharingMusicPlayer = SharingMusicPlayer(backgroundMusicPlayer: AVAudioPlayer)

to

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

Update

This is the full code that compiles at my end

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...
    }
}
薄荷→糖丶微凉 2025-01-22 07:34:02

这就是我想到的,尽管我仍然不确定这是否是快速执行单例的正确方法:

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 对象不同。

This is what I came up with, though I am still not sure if this is the right way to do a singleton in 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()
    }
}

Then this is how I would call the functions to start and stop the music:

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()
       }
   }

Please note that the above buttons are in SpriteKit so they are different than the usual UIButton objects in Main.storyboard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文