我怎么能停止视频背景?

时间:2018-04-05 12:23:06

标签: ios swift xcode

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

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

}

3 个答案:

答案 0 :(得分:1)

按照以下步骤操作:

1)为交换机制作IBOutlet

@IBOutlet private var muteSwitch: UISwitch!

不要忘记将它连接到故事板中的开关。

2)从UserDefaults(在viewDidLoad中)读取保存的值:

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, alpha : 0.25, willLoopVideo : true)
    muteSwitch.isOn = shouldMute
}

3)将开关值保存到UserDefaults(在@IBAction func`witch`中):

@IBAction func `switch`(_ sender: UISwitch) {
    let shouldMute = sender.isOn
    videoBackground.isMuted = shouldMute
    UserDefaults.standard.set(shouldMute, forKey:"isMuted")
}

答案 1 :(得分:-1)

override func viewDidLoad() {
    super.viewDidLoad()

    let userDefaults = UserDefaults.standard
    if userDefaults.value(forKey: "isMuted") != nil{

        let object = userDefaults.value(forKey: "isMuted") as? NSNumber
        yourSwitch.isOn = (object?.boolValue)!
    }
}
@IBAction func switchChanged(sender: UISwitch) {
    let uDefault = UserDefaults.standard
    if sender.isOn {
        uDefault.setValue(NSNumber(value: true), forKey:"isMuted")

    }
    else {
        uDefault.setValue(NSNumber(value: false), forKey:"isMuted")

    }

}

答案 2 :(得分:-1)

  • <强> 1。目标 - C版

    #import&#34; ViewController.h&#34;

    @interface ViewController ()
    
    @property (strong, nonatomic) IBOutlet UISwitch *bluetoothSwitch;
    
    - (IBAction)saveSwitchState:(id)sender;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if ([defaults objectForKey:@"SwitchState"])
            self.bluetoothSwitch.on = [defaults boolForKey:@"SwitchState"];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)saveSwitchState:(id)sender
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if ([self.bluetoothSwitch isOn]) 
            [defaults setBool:YES forKey:@"SwitchState"];
        else 
           [defaults setBool:NO forKey:@"SwitchState"];
    }
    @end
    
  • Swift版本

    导入UIKit

    class ViewController: UIViewController {
        @IBOutlet weak var textField: UITextField!
        @IBOutlet weak var stateSwitch: UISwitch!
    
        @IBAction func buttonClicked(_ sender: Any) {
            if stateSwitch.isOn {
                textField.text = "The Switch is Off"
                stateSwitch.setOn(false, animated:true)
            } else {
                textField.text = "The Switch is On"
                stateSwitch.setOn(true, animated:true)
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            stateSwitch.addTarget(self, action: #selector(stateChanged), for: UIControlEvents.valueChanged)
        }
    
        @objc func stateChanged(switchState: UISwitch) {
            if switchState.isOn {
                textField.text = "The Switch is On"
            } else {
                textField.text = "The Switch is Off"
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }