Hey guys I am still kind of new to Stackoverflow so please bear with me if I am doing something wrong, I am trying my best.
I am trying to make a simple app with the new apple watchOS6 and swiftUI in the new Xcode11 beta that plays a sound file from assets.xcassets.
I think I am doing something wrong when trying to find the file I have in the assets, here's the error code:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Model/AudioPlayer.swift, line 21 2019-08-28 15:06:58.396058+0200 WatchOS6Demo WatchKit Extension[12691:794523] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Model/AudioPlayer.swift, line 21
My code is built like this: I have a ContentView that is a list that just displays my button that calls the my audioplayer when its pressed. The code for ContentView is not included.
The AudioPlayerButton struct looks like this and it basically just calls play() on the player:
struct AudioPlayerButton: View {
@State var player = AudioPlayer()
var body: some View {
Button(action: player.play) {
Image("newPlayButton")
}
}
}
This is my AudioPlayer class where I get the error:
import Foundation
import AVFoundation
import Combine
import SwiftUI
class AudioPlayer {
var didChange = PassthroughSubject <Void, Never>()
var isPlaying: Bool = false
var AudioPlayer: AVAudioPlayer?
let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "Glad", ofType: ".wav")!)
//let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "Glad", ofType: ".wav", inDirectory:"/Users/phillipeismark/Desktop/WatchOS6/WatchOS6Demo/WatchOS6Demo WatchKit Extension/Assets.xcassets/" )!)
func play() {
do {
AudioPlayer = try AVAudioPlayer(contentsOf: url)
AudioPlayer?.play()
} catch {
print(error)
}
}
}
I noticed that there are multiple asset.xcassets folders, one inside watchOS6 WatchKit App and one inside of WatchOS6 WatchKit Extension, to be sure I hadn't put it in the wrong place I just added the file to both of the folders.
Here is what I have tried:
1: I noticed that there is multiple asset.xcassets folders and I have tried to add the file to both of them.
2: I have tried to set both the ForResource and ofType parameters as nil to kind of try to "hit more targets" as the Documentation says: "If you specify nil, the method returns the first resource file it finds that matches the remaining criteria".
3: I tried to give a value to the the 3rd parameter in path(forResource:ofType:inDirectory:) as you can see in my AudioPlayer class with the outcommented line.
4: I took an old sound from an old project that I knew worked. I also tested if that project could run and it could.
Anything that could send me in the right direction are very much appreciated! Thanks.
I uploaded the project to GitHub if anybody wanted to look at it.
Update: IT WORKS I ran the normal iPhone simulator in another project. I did not do anything but make a new project and start the simulator. Then I opened the browser went on YouTube and played a video. It still didn't work so I let the video play in the simulator and continued to google around. Then when the video ended and YouTube had to play an add the sound magically started to work - there was an add even before the video started but that one didn't make it work, it was at the videos end. I was speechless. my GUESS is that the iPhone simulator made a configuration but I have no idea. Any theories about how this work or could work is very much appreciated