我对编码很新,没有经验。 我找到了制作视频播放的方法,但它自动播放/自动播放。我需要视频只在按下按钮时打开和播放。请有人帮我调整我的代码来执行此操作。
这是我目前的代码:
import UIKit
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerviewcontroller = AVPlayerViewController()
var playerview = AVPlayer ()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/Warm Up/Video View/Video View/ViewController.swift")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
试试这个:
@IBAction func playMusic(sender: AnyObject) {
var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/Warm Up/Video View/Video View/ViewController.swift")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
您只需将其放在ViewController
中的任何位置,只要它不在其他功能中。
然后将其与故事板中的按钮连接:
答案 1 :(得分:0)
(contentSizeX - collectionviews frame X) / cells = contentOffset per item
(780 - 383) / 12 => 33
中的代码移至您在步骤1中创建的操作。此外,您不需要viewDidAppear
和viewDidLoad
的默认实施:
didReceiveMemoryWarning