2016-03-03 94 views
0

我正在研究這個記錄器項目,這個代碼是用swift 2.0編寫的,它給出了這個問題! 我似乎類似的標題文章,但不涉及我有線程1:exc_bad_instruction(代碼= exc_i386_invop子代碼= 0x0)錯誤

進口的UIKit 進口AVFoundation

類PlaySoundViewController問題:的UIViewController {

var audioPlayer: AVAudioPlayer! 
var receivedAudio: AudioRecorded! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 




    do{ 

     let  audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl) --> ***The error happens here*** 

      audioPlayer.enableRate = true 
    }catch let ErrorType { 

     NSLog("Could not create AVAudioPlayer: \(ErrorType)") 
    } 

}

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 

} 

@IBAction func playFastSound(sender: AnyObject) { 
    audioPlayer.stop() 
    audioPlayer.play() 
    audioPlayer.rate = 2.0 



} 

@IBAction func playSlowSound(sender: AnyObject) 
{ 
    audioPlayer.stop() 
    audioPlayer.play() 
    audioPlayer.rate = 0.5 

} 


@IBAction func stopPlaying(sender: AnyObject) { 

    audioPlayer.stop() 

} 
/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

}

+0

你能發佈爲「receivedAudio.filePathUrl」返回的網址嗎? –

回答

0

您的問題可能是分配期間使用try!

由於HWS puts it -

有一兩件事來討論,這是做什麼,如果你知道一個電話根本不能失敗,無論出於何種原因。現在,顯然這是您需要根據具體情況做出的基本決定,但是如果您知道方法調用絕對沒有辦法可能會失敗,或者如果它失敗了,那麼您的代碼就會被徹底打破,以至於您可能會以及崩潰,你可以使用嘗試!向Swift發出這個信號。

所以這可能不是你想要的。 對於錯誤處理,通常使用try(不含!)。

do { 
     let audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl) 
     // Do stuff with audio player 
    } 
    catch let error { 
     NSLog("Could not create AVAudioPlayer: \(error)") 
    } 

現在,這並不能解釋爲什麼音頻播放器沒有被創建。 URL很可能是無效的,它指向的文件不存在,或者它被AVAudioPlayer無法讀取的東西編碼。通常你會想要一個像mp3,mp4等標準文件格式。

+0

我嘗試了,現在我又出現了兩個錯誤: – user2074226

+0

有條件粘合劑的初始化程序必須有選項類型,而不是'AVAudioPlayer'呼叫可以縮小,但不會標記爲'嘗試'並且不處理錯誤 – user2074226

+0

@ user2074226 - 答覆已更新。 – mszaro

相關問題