Sound


import SpriteKit
import GameplayKit
import AVFoundation


class GameScene: SKScene {
  var toggle:Bool = true
  let volSlider  = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
  let defaultMax = Float(0.123456789)
  var player: AVPlayer!
  var videoNode: SKVideoNode!
 @objc func volSliderChangedValue(sender: UISlider) {
  player.volume = sender.value
  }
override func didMove(to view: SKView) {
 removeAllChildren()  // Delete this in your actual project.
 let label = SKLabelNode(fontNamed: "HelveticaNeue-Light")
 label.text = "Sound On Off"
 let labelSize: CGFloat = 30.0
 label.fontSize = labelSize
 label.position = CGPoint(x:self.frame.midX, y:self.frame.height/2 + 70)
 label.fontColor = SKColor.yellow
 self.addChild(label)
label.name = "button"
let volLabel  = SKLabelNode(text: "Volume")
volLabel.setScale(0.6)
 volLabel.verticalAlignmentMode = .center
 volLabel.position = CGPoint(x: frame.minX + 120, y: frame.minY + 360)
 let url = Bundle.main.url(forResource: "s1", withExtension: "wav")!
 player = AVPlayer(url: url)
 videoNode = SKVideoNode(avPlayer: player!)
 volSlider.addTarget(self, action: #selector(volSliderChangedValue), for: UIControl.Event.valueChanged)
 volSlider.value = 1
 let volOrigin = convertPoint(toView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY))
 volSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15))
  view.addSubview(volSlider)
  addChild(volLabel)
  addChild(videoNode)
 videoNode.alpha = 0
  }
    func toggleTest() {
        if(toggle){
            videoNode.play()
        toggle = false;
        }else{
        videoNode.pause()
        toggle = true;
  }
}
 override func touchesBegan(_ touches: Set, with event: UIEvent?) {
 for touch in touches {
    let location = touch.location(in: self)
    let touchedNode = atPoint(location)
   if touchedNode.name == "button" {
   toggleTest()
  }
  }
  }
}