2017-04-12 20 views
1

我会直接指出,因为我觉得我的标题可能需要进一步解决,以真正理解我的困境。本质上;我有两个视图控制器与模态segue连接;与我的第一个视图的主要功能的初始工作部分是这样的:斯威夫特3:获得回报继续发挥音效

@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

你想要做什么? – Jack

+0

我只需要执行performSegueToReturnBack函数即可成功播放audioPlayer;但没有声音时产生的函数被调用 – Jordy

+0

尝试一下 - '覆盖FUNC viewWillDisappear(_动画:BOOL){ super.viewWillDisappear(真) audioPlayer.play() }' – Jack

回答

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

}