gpt4 book ai didi

ios - 修复音频播放器错误

转载 作者:行者123 更新时间:2023-11-30 13:05:13 25 4
gpt4 key购买 nike

我在 var filePathUrl 行中遇到错误。错误代码是“可选类型‘字符串’的值?”未打开:您的意思是使用“!”吗?或者 '?'?'”。我该如何修复这个错误?

import UIKit
import AVFoundation

class PlayMusicViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

var daten = NSBundle.mainBundle().pathForResource("Kool_Savas_Auge", ofType: "mp3")
var filePathUrl = NSURL.fileURLWithPath(daten)
var audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, fileTypeHint: nil)


}

@IBAction func PlayMusic(sender: UIButton) {
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

最佳答案

尝试使用

var filePathUrl = NSURL.fileURLWithPath(daten!)

Could you please tell me what I did wrong?

enter image description here

可选值后面的感叹号 (!) 可强制展开其值。请检查这个 Swift 2: !, ? -" Value of optional type "..." not unwrapped"

更新:

例如:

var daten = NSBundle.mainBundle().pathForResource("Kool_Savas_Auge", ofType: "mp3")

var filePathUrl = NSURL.fileURLWithPath(daten!)

do{
var audioPlayer = try AVAudioPlayer(contentsOfURL: filePathUrl, fileTypeHint: nil)

audioPlayer.prepareToPlay()
audioPlayer.play()
}
catch let err as NSError{
print(err.debugDescription);
}

关于ios - 修复音频播放器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476286/

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