按钮触发视频打开和播放

时间:2016-05-31 21:40:57

标签: ios swift button video

我对编码很新,没有经验。 我找到了制作视频播放的方法,但它自动播放/自动播放。我需要视频只在按下按钮时打开和播放。请有人帮我调整我的代码来执行此操作。

这是我目前的代码:

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.
    }

}

2 个答案:

答案 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中的任何位置,只要它不在其他功能中。

然后将其与故事板中的按钮连接:

How to connect

答案 1 :(得分:0)

  1. 创建操作连接 as explained here
  2. (contentSizeX - collectionviews frame X) / cells = contentOffset per item (780 - 383) / 12 => 33中的代码移至您在步骤1中创建的操作。
  3. 利润。
  4. 此外,您不需要viewDidAppearviewDidLoad的默认实施:

    didReceiveMemoryWarning