我会直截了当,因为我觉得我的头衔可能需要进一步解决才能真正理解我的困境。实质上;我有两个视图控制器与模态segue链接;我的第一个View的主要功能的初始工作部分看起来像这样:
@IBAction func beginButton(_ sender: Any) {
audioPlayer.play()
}
第二个视图控制器设置如下;其功能完全不包括播放相同audioPlayer的愿望
func performSegueToReturnBack() {
audioPlayer.play()
if let nav = self.navigationController {
nav.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}
}
任何有关如何使这项工作的想法将不胜感激;谢谢你的帮助!
答案 0 :(得分:1)
看起来你可能错过了一些代码。这就是如何完成你想要做的事情。
var audioPlayer = AVAudioPlayer()
let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "CheckoutScannerBeep", ofType: "mp3")!) // If sound is not in an asset
//let alertSound = NSDataAsset(name: "CheckoutScannerBeep") // If sound is in an Asset
func testSound(){
do {
// audioPlayer = try AVAudioPlayer(data: (alertSound!.data), fileTypeHint: AVFileTypeMPEGLayer3) //If in asset
audioPlayer = try AVAudioPlayer(contentsOf: alertSound) //If not in asset
audioPlayer.pan = -1.0 //left headphone
//audioPlayer.pan = 1.0 // right headphone
audioPlayer.prepareToPlay()
audioPlayer.volume = 50
audioPlayer.play()
} catch {
print("error")
}
}
func performSegueToReturnBack() {
testSound()
if let nav = self.navigationController {
nav.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}
}