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()
    }


}

Hello, I am trying to make a button that plays a sound (Roblox.mp3). It compiles without errors. However, when I open the simulator, it crashes. My error log is:

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) 

I am really not sure what is going wrong. I believe the sound I want is imported into Xcode because I drag and dropped it into my project. If the error is with the line that requests the sound, I may not have formatted it properly. I have seen it as both @"mp3" and typeOf("mp3"). Again, sorry if this seems rudimentary. I am just beginning with swift, and there are some...langauge barriers that I am struggling with.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Julian L
  • 84
  • 1
  • 3
  • 10
  • 1
    The exception reason _this class is not key value coding-compliant for the key **button**_ is suggesting that your `ViewController` does not have an `@IBOutlet` of name `button`, but the storyboard has a connection to it. It often happens when you edited your source code after some connection had made. Check all your storyboard connections and remove the `@IBOutlet` connection (not `@IBAction` connection) to `button`. Searching with the error message on web, you can find many articles and some of them describes _how to_ in details. – OOPer Aug 11 '17 at 04:16
  • https://stackoverflow.com/questions/24393495/playing-a-sound-with-avaudioplayer please try checking this link if it works for you – iOS Geek Aug 11 '17 at 04:40

1 Answers1

0

Open your storyboard and goto Connection inspector you can see something like below screenshot.

enter image description here

@IBOutlet connection is binded but unknown to storyboard. this might be one reason of crashing your application.

Bhautik Ziniya
  • 1,554
  • 12
  • 19