创建播放声音的按钮,获得NSException错误

时间:2017-08-11 04:03:01

标签: ios swift audio

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)。它编译没有错误。但是,当我打开模拟器时,它会崩溃。我的错误日志是:

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 个答案:

答案 0 :(得分:0)

打开故事板并转到连接检查器,您可以看到如下截图所示的内容。

enter image description here

@IBOutlet连接绑定但故事板未知。这可能是导致应用程序崩溃的一个原因。