2017-08-11 33 views
0
import UIKit 
import AVFoundation 
import AudioToolbox 



class ViewController: UIViewController { 

    var player : AVAudioPlayer? 

    func playSound(){ 
     let path = Bundle.main.path(forResource: "Roblox", ofType:"mp3")! 
     let url = URL(fileURLWithPath: path) 

     do { 
      let sound = try AVAudioPlayer(contentsOf: url) 
      self.player = sound 
      sound.numberOfLoops = 1 
      sound.prepareToPlay() 
      sound.play() 
     } catch { 
      print("error loading file") 
      // couldn't load file :(
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

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

    @IBAction func button(_ sender: Any) { 
     playSound() 
    } 


} 

您好,我正在嘗試製作播放聲音的按鈕(Roblox.mp3)。它編譯沒有錯誤。但是,當我打開模擬器時,它會崩潰。我的錯誤日誌是:創建播放聲音的按鈕,得到NSException錯誤

2017-08-10 20:52:57.876 Oof[2500:252563] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Oof.ViewController 0x7fc320607800> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010dcd6b0b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000010aa8a141 objc_exception_throw + 48 
    2 CoreFoundation      0x000000010dcd6a59 -[NSException raise] + 9 
    3 Foundation       0x000000010a5a000b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292 
    4 UIKit        0x000000010b0f7994 -[UIViewController setValue:forKey:] + 87 
    5 UIKit        0x000000010b364a09 -[UIRuntimeOutletConnection connect] + 109 
    6 CoreFoundation      0x000000010dc7ce8d -[NSArray makeObjectsPerformSelector:] + 269 
    7 UIKit        0x000000010b3633bf -[UINib instantiateWithOwner:options:] + 1856 
    8 UIKit        0x000000010b0fdfc3 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 
    9 UIKit        0x000000010b0fe8d9 -[UIViewController loadView] + 177 
    10 UIKit        0x000000010b0fec0a -[UIViewController loadViewIfRequired] + 195 
    11 UIKit        0x000000010b0ff45a -[UIViewController view] + 27 
    12 UIKit        0x000000010afc798a -[UIWindow addRootViewControllerViewIfPossible] + 65 
    13 UIKit        0x000000010afc8070 -[UIWindow _setHidden:forced:] + 294 
    14 UIKit        0x000000010afdaebe -[UIWindow makeKeyAndVisible] + 42 
    15 UIKit        0x000000010af5437f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346 
    16 UIKit        0x000000010af5a5e4 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 
    17 UIKit        0x000000010af577f3 -[UIApplication workspaceDidEndTransaction:] + 182 
    18 FrontBoardServices     0x00000001111765f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24 
    19 FrontBoardServices     0x000000011117646d -[FBSSerialQueue _performNext] + 186 
    20 FrontBoardServices     0x00000001111767f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 
    21 CoreFoundation      0x000000010dc7cc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    27 Oof         0x000000010a1429b7 main + 55 
    28 libdyld.dylib      0x0000000110b2d65d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

我真的不確定發生了什麼問題。我相信我想要的聲音被導入到Xcode中,因爲我將它拖放到我的項目中。如果錯誤是在請求聲音的那一行,我可能沒有正確格式化它。我已經將它看作@「mp3」和typeOf(「mp3」)。再次,如果這看起來很簡單,對不起。我剛開始的時候很快,而且還有一些我正在努力掙扎的語言障礙。

+1

異常原因_這個類不是關鍵值編碼兼容鍵**按鈕** _建議你的'ViewController'沒有名爲'button'的'@ IBOutlet',但故事板有一個連接到它。這通常發生在您完成一些連接後編輯源代碼時。檢查所有故事板連接,並將'@ IBOutlet'連接(不是'@ IBAction'連接)刪除到'button'。用網上的錯誤信息搜索,你可以找到很多文章,其中一些文章詳細描述_how to_。 – OOPer

+0

https://stackoverflow.com/questions/24393495/playing-a-sound-with-avaudioplayer請嘗試檢查此鏈接,如果它適合你 –

回答

0

打開你的故事板並轉到連接檢查器,你可以看到類似下面的截圖。

enter image description here

@IBOutlet連接綁定,但未知的故事情節。這可能是導致應用程序崩潰的一個原因。