2017-04-12 55 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) 
     } 

}