【发布时间】:2019-06-13 22:02:39
【问题描述】:
尝试在 xcode 10.1 中构建木琴时,在 iOS12 上使用 swift 4.2,我使用按钮播放 .wav 文件并输入以下代码,但出现以下错误:
"type 'String' 没有成员 'playback'"
func playSound() {
guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
try AVAudioSession.sharedInstance().setActive(true)
/* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
/* iOS 10 and earlier require the following line:
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
【问题讨论】:
-
您确定您的项目是为 Swift 4.2 而不是早期版本设置的吗?
-
如果您使用的是 swift 4,请使用
AVAudioSessionCategoryPlayback而不是.playback作为类别。 -
您的代码需要一个 .mp3 文件,但声音文件真的是一个 .wav 文件吗?
-
我尝试使用 AVAudioSessionCategoryPlayback 而不是 .playback。还是不行。是的,我还检查了 swift 版本的 4.2。它有一个 .wav 扩展名,它必须是 .wav 文件,对吗?我很困惑。