Home > Mobile >  SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expec
SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expec

Time:03-20

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.

CodePudding user response:

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

CodePudding user response:

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