由于出现以下错误,请帮助我修复代码:
错误在以下代码行上:
let urlpathString:String? = NSBundle.mainBundle().pathForResource(url)
我的完整代码:
import UIKit
import AVKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController {
let avPlayerViewControler = AVPlayerViewController()
var avPlayer:AVPlayer?
var videoNumber:Int!
var url:String!
override func viewDidLoad() {
super.viewDidLoad()
}
func playVideo() {
switch videoNumber {
case 1 : url="20"
case 2 : url="10"
default : break
let urlpathString:String? = NSBundle.mainBundle().pathForResource(url)
if let urlPath = urlpathString {
let movieUrl = NSURL(fileURLWithPath: urlPath)
self.avPlayer = AVPlayer(URL: movieUrl)
self.avPlayerViewControler.player = self.avPlayer
}
}
}
@IBAction func Button(sender: UIButton){
videoNumber=1
playVideo()
}
@IBAction func Button2(sender: UIButton) {
videoNumber=2
playVideo()
}
}
最佳答案
没有pathForResource
的简单NSBundle
方法,至少有第二个参数,例如pathForResource:ofType:
由于仍然需要NSURL
,因此请使用URL发布的API,将整个if let
表达式替换为
func playVideo() {
switch videoNumber {
case 1 : url="20"
case 2 : url="10"
default : break
}
if let movieUrl = NSBundle.mainBundle().URLForResource(url, withExtension: "mp4") {
self.avPlayer = AVPlayer(URL: movieUrl)
self.avPlayerViewControler.player = self.avPlayer
}
}
您必须将
mp4
更改为预期的扩展名。关于ios - Xcode Swift-无法使用 'pathForResource'类型的参数列表调用 '(String!)',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37712603/