import UIKit
import AVFoundation
classViewController:UIViewController {
varplayer —AVAudioPlayer=AVAudioPlayer()
override func viewDidLoad(){
super.viewDidLoad()
let audioPath
= NSBundle.mainBundle().pathForResource("bomb", ofType:"m4a")!
do{
try player
= AVAudioPlayer (contentsOfURL:NSURL (fileURLWithPath:audioPath))
player.play()
} catch{
// Process error here
}
}
override funcdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When you type and run the app, you get the following error:
fatal error: unexpectedly found nil while unwrapping an optional value
(lldb)
What is the cause?
In fact, the file bomb.m4a is inserted into the project, and you can actually listen to it as music.
However, I have not been able to run the application.
Thank you for your cooperation.
It is reasonable to assume that the resource acquisition has failed.
let audioPath
= NSBundle.mainBundle().pathForResource("bomb", ofType:"m4a")
print(audioPath!=nil? "Exists": "Empty")
Insert the print()
function in this way to verify that the audioPath
has correctly substituted the file path for the audio file.
As a result of the above verification, we found that the path did not pass because it was "Empty".
If a resource is bundled with bomb.m4a
, and it is a non-playable file,
Abundled sound cloudn't load.
The message appears on the console, so you can't think of the line.
Therefore, you should suspect that the file names do not match and that resource files are not bundled correctly during build.
Why don't you consider this kind of action?
© 2024 OneMinuteCode. All rights reserved.