无法转换类型和参数标签的值...重载

时间:2016-01-02 13:12:53

标签: swift

我尝试将一个ressource文件复制到我的swift编码中,我得到了这些错误。 xCode,Swift:无法转换'NSError?'类型的值预期的参数类型'String?' 和  参数标签'(contentsOfURL:,error :)'与任何可用的重载都不匹配

这是我的代码

public class SKTAudio {
    public var backgroundMusicPlayer: AVAudioPlayer?
    public var soundEffectPlayer: AVAudioPlayer?

    public class func sharedInstance() -> SKTAudio {
        return SKTAudioInstance
    }

    public func playBackgroundMusic(filename: String) {
        let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
        if (url == nil) {
            print("Could not find file: \(filename)")
            return
        }

        var error: NSError? = nil
        backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: error)`Cannot convert value of type 'NSError?' to expected argument type 'String?'`
        if let player = backgroundMusicPlayer {
            player.numberOfLoops = -1
            player.prepareToPlay()
            player.play()
        } else {
            print("Could not create audio player: \(error!)")
        }
    }

    public func pauseBackgroundMusic() {
        if let player = backgroundMusicPlayer {
            if player.playing {
                player.pause()
            }
        }
    }

    public func resumeBackgroundMusic() {
        if let player = backgroundMusicPlayer {
            if !player.playing {
                player.play()
            }
        }
    }

    public func playSoundEffect(filename: String) {
        let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
        if (url == nil) {
            print("Could not find file: \(filename)")
            return
        }

        var error: NSError? = nil
        soundEffectPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)`Argument labels '(contentsOfURL:, error:)' do not match any available overloads`
        if let player = soundEffectPlayer {
            player.numberOfLoops = 0
            player.prepareToPlay()
            player.play()
        } else {
            print("Could not create audio player: \(error!)")
        }
    }
}

private let SKTAudioInstance = SKTAudio()

这是我想要做的: http://www.raywenderlich.com/100496/video-tutorial-how-to-make-a-game-like-flappy-bird-in-swift-part-1-scene-size-and-aspect-ratios

1 个答案:

答案 0 :(得分:1)

我检查了代码并在行

中发现了错误
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: error)

在这里,您将错误实例作为参数传递,而该函数需要一个String。

要解决此问题,您可以传递一个重新命名文件格式的String。例如:AVFileTypeMPEG4AVFileTypeAppleM4VAVFileTypeCoreAudioFormat

 backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: AVFileTypeMPEG4)