Reproducción de un sonido embedded mediante el código Swift 2.0 Xcode 7.1 IOS 9.1

Copiado y pegado este código principalmente. Comstack y ejecuta, pero no juega nada. Utilizando Xcode 7.1 y iOS 9.1. Lo que me he perdido … Archivo de sonido cargado en el progtwig principal y AVAssets …

import UIKit import AVFoundation class ViewController: UIViewController { var buttonBeep : AVAudioPlayer? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. buttonBeep = setupAudioPlayerWithFile("hotel_transylvania2", type:"mp3") //buttonBeep?.volume = 0.9 buttonBeep?.play() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? { //1 let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String) let url = NSURL.fileURLWithPath(path!) //2 var audioPlayer:AVAudioPlayer? // 3 do { try audioPlayer? = AVAudioPlayer(contentsOfURL: url) } catch { print("Player not available") } return audioPlayer } } 

Tienes esta línea al revés:

 try audioPlayer? = AVAudioPlayer(contentsOfURL: url) 

Debería ser:

 audioPlayer = try AVAudioPlayer(contentsOfURL: url) 

Nota al margen: la conversión hacia y desde NSString no es necesaria aquí, simplemente use String – y no debería forzar el desenvolver el resultado de NSBundle:

 func setupAudioPlayerWithFile(file:String, type:String) -> AVAudioPlayer? { //1 guard let path = NSBundle.mainBundle().pathForResource(file, ofType: type) else { return nil } let url = NSURL.fileURLWithPath(path) //2 var audioPlayer:AVAudioPlayer? // 3 do { audioPlayer = try AVAudioPlayer(contentsOfURL: url) } catch { print("Player not available") } return audioPlayer }