当我移动到另一个viewcontroller时,如何停止视频背景?

时间:2018-04-06 10:57:26

标签: ios swift xcode

当我移动到另一个UIViewController时,如何停止视频背景? 因为当我移动到另一个UIViewController时,视频和背景音乐继续播放,当我返回主页面时,两个视频重叠。所以当我移动到另一个UIViewController时,我希望视频和音乐停止,例如当我移动UIViewController进行注册时

import UIKit
import SwiftVideoBackground
import Firebase
import FirebaseAuth

class ViewController: UIViewController {

private let videoBackground = VideoBackground()

@IBOutlet weak var usernameField: UITextField!

@IBOutlet weak var passwordField: UITextField!

@IBOutlet weak var mute_img: UIImageView!

@IBOutlet private var muteSwitch: UISwitch!

@IBAction func `switch`(_ sender: UISwitch) {



    if (sender.isOn == true)
    {
        mute_img.isHidden = false
        videoBackground.isMuted = true
    }
    else
    {
        mute_img.isHidden = true
        videoBackground.isMuted = false
    }

    let shouldMute = sender.isOn
    videoBackground.isMuted = shouldMute
    UserDefaults.standard.set(shouldMute, forKey:"isMuted")
   }


override func viewDidLoad() {
    super.viewDidLoad()
    let userDefaults = UserDefaults.standard
    let shouldMute = userDefaults.bool(forKey: "isMuted")
    videoBackground.play(view: view, videoName: "intro", videoType: 
    "mp4", isMuted: shouldMute, willLoopVideo : true)
    muteSwitch.isOn = shouldMute


    }

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if textField == usernameField {
        passwordField.becomeFirstResponder()
    } else if textField == passwordField {
        textField.resignFirstResponder()
    }
    return true
}

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

}

2 个答案:

答案 0 :(得分:0)

前进导航:

videoBackground.pause()  

返回导航:

videoBackground.resume()

答案 1 :(得分:0)

根据Documentation

您只需使用pause()

即可

离开控制器时

videoBackground.pause()

返回控制器时:

videoBackground.resume()

enter image description here