注意事项之前,我对编码很陌生,但我已尽力自己寻找答案,如果我问得太愚蠢,我很抱歉D:
我试图制作一个设置页面,您可以关闭整个应用程序的声音/ bgm,到目前为止,我已设法将视图链接到另一个代码。
ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController {
var ButtonSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("tamborine", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
var BGMaudioPlayer = AVAudioPlayer()
func playBgMusic(){
let musicPath = NSBundle.mainBundle().pathForResource("Waiting-in-the-Woods", ofType: "mp3")
let url = NSURL(fileURLWithPath: musicPath!)
BGMaudioPlayer = AVAudioPlayer(contentsOfURL: url, error: nil)
BGMaudioPlayer.numberOfLoops = -1
//-1為循環播放
BGMaudioPlayer.volume = 1
BGMaudioPlayer.prepareToPlay()
BGMaudioPlayer.play()
}
override func viewDidLoad() {
super.viewDidLoad()
audioPlayer = AVAudioPlayer(contentsOfURL: ButtonSound, error: nil)
//Bgm.play()
}
override func viewWillAppear(animated: Bool) {
playBgMusic()
if !BGMaudioPlayer.playing {
playBgMusic()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func quitBtn(sender: UIButton) {
audioPlayer.play()
exit(0);
}
@IBAction func Start(sender: UIButton) {
audioPlayer.play()
}
}
Setting.Swift
import UIKit
import AVFoundation
class Setting: UIViewController {
let mainView = ViewController()
@IBOutlet weak var BGMSwitch: UISwitch!
@IBOutlet weak var SoundsSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func soundsswitch(sender: AnyObject) {
if !SoundsSwitch.on{
mainView.BGMaudioPlayer.stop()
}else{
mainView.BGMaudioPlayer.play()
}
}
}
它构建但是当我关闭开关时它崩溃了...... 提前致谢。 d:
答案 0 :(得分:0)
要在视图之间发送数据,您可以实现
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "Segue Identifier") {
}
或强>
使用委托在视图之间进行通信。
你可以在堆栈溢出时找到很多关于这两个实现的内容。