'SharingMusicPlayer.swift:12:79: Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type 'AVAudioPlayer'
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.
Solution 1:[1]
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
.
Solution 2:[2]
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...
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |