我想在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)
}
}
答案 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)
}
}