我会直截了当,因为我认为我的头衔可能需要进一步处理才能真正理解我的困境。本质上;我有两个与模态搜索链接的视图控制器;第一个View的主要功能的初始工作部分如下所示:

 @IBAction func beginButton(_ sender: Any) {
        audioPlayer.play()
            }

第二个视图控制器的设置如下:它的功能完美,排除了播放同一音频播放器的需要
 func performSegueToReturnBack()  {
         audioPlayer.play()
            if let nav = self.navigationController {

                nav.popViewController(animated: true)
            } else {

                self.dismiss(animated: true, completion: nil)
            }

    }

任何有关如何使它工作的想法将不胜感激;谢谢你的帮助!

最佳答案

看起来您可能缺少一些代码。这就是完成您尝试做的事情的方式。

    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)
        }

}

10-08 12:01