gpt4 book ai didi

ios - 类型 'string' 没有成员 'playback'

转载 作者:行者123 更新时间:2023-11-28 07:26:01 26 4
gpt4 key购买 nike

尝试在 xcode 10.1 中构建木琴时,在 iOS12 上使用 swift 4.2,我使用按钮播放 .wav 文件并输入以下代码,但出现以下错误:

"type 'String' has no member '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)
}
}

最佳答案

我四处查看 StackOverflow 并借用了这段有效的代码,但我不知道它是否是传统的做法。

swift 4.2

import AVFoundation
var player = AVAudioPlayer()

let url = URL(fileURLWithPath: Bundle.main.path(forResource: "note1", ofType: "wav")!)
do {
player = try AVAudioPlayer(contentsOf: url)
player.play()
} catch {
print("couldn't load file :(")
}

关于ios - 类型 'string' 没有成员 'playback',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56589098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com