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")の両方として見てきました。再び、これが初歩的なようであれば、ごめんなさい。私はすぐに迅速に始まり、いくつかの...私が苦労している垣間見る障壁があります。
例外的な理由は、このクラスはキー**ボタンのキー値コーディングに準拠していません。_は、あなたの 'ViewController'に' button.'という名前の '@BOutlet'がないことを示唆していますが、それへの接続。何らかの接続が行われた後にソースコードを編集したときによく発生します。すべてのストーリーボード接続を確認し、 '@ IBOutlet'接続(' @ IBAction'接続ではありません)を 'button'に削除してください。 Web上でエラーメッセージを検索すると、多くの記事を見つけることができ、そのうちのいくつかは詳細を_how to_で説明しています。 – OOPer
https://stackoverflow.com/questions/24393495/playing-a-sound-with-avaudioplayerご利用の場合は、このリンクをチェックしてみてください –