我尝试将一个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()
答案 0 :(得分:1)
我检查了代码并在行
中发现了错误backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: error)
在这里,您将错误实例作为参数传递,而该函数需要一个String。
要解决此问题,您可以传递一个重新命名文件格式的String。例如:AVFileTypeMPEG4
,AVFileTypeAppleM4V
,AVFileTypeCoreAudioFormat
等
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: AVFileTypeMPEG4)