将开关设置为视频背景上的静音

时间:2018-04-05 10:03:52

标签: ios swift xcode swift3

我想在swift 3 +

中设置一个开关或按钮以静音视频背景

CODE:

import UIKit
import SwiftVideoBackground

class ViewController: UIViewController {
private let videoBackground = VideoBackground()

override func viewDidLoad() {
    super.viewDidLoad()

    videoBackground.play(view: view, videoName: "intro", videoType: "mp4", isMuted: false, alpha : 0.25, willLoopVideo : true)
}

}

2 个答案:

答案 0 :(得分:-1)

//将此方法添加到VideoBackground类

public var isMuted = true {
        didSet {
            playerLayer.player?.isMuted = isMuted
        }
    }

//按钮操作方法

@IBAction func mute(_ sender: Any) {
    videoBackground.isMuted = true
}

//当app在后台时

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    videoBackground.pause() // OR videoBackground.isMuted = true
}

答案 1 :(得分:-1)

解决方案:

解决方案:

 import UIKit
 import SwiftVideoBackground
 import AVFoundation




class ViewController: UIViewController {
private let videoBackground = VideoBackground()

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

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

}


override func viewDidLoad() {
    super.viewDidLoad()

    videoBackground.play(view: view, videoName: "intro", videoType: "mp4", isMuted: false, alpha : 0.25, willLoopVideo : true)
}

}