0

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

Philiong
  • 188
  • 1
  • 8
  • 1
    I don't believe you can add audio file to your assets. Take a look at the second answer - 7 upvotes - to this question, see if it helps: https://stackoverflow.com/questions/46231153/how-to-add-sound-files-to-your-bundle-in-xcode –  Aug 28 '19 at 14:25
  • Hey thanks for your answer, I know its possible to do it from the assets.xcassets because I did it in my old project. Unless is there a difference with the watch and the iOS phone after it builds that could maybe affect it in any way? – Philiong Aug 28 '19 at 15:11
  • 1
    I don't use audio in my app, so my experience is *today*, trying to be of help. (1) What caught my eye was your first thing you tried - asset usually aren't added through Finder, but through Xcode. (2) I then tried Xcode and found nothing obvious for adding audio - unlike images and colors. Remember - SwiftUI is *very* raw... if you had this audio file available like in your "old" (Xcode 10? iOS? SwiftUI?) project, but instead it was a UIKit project, does it find everything? –  Aug 28 '19 at 15:33
  • Hey dfd, I shared the project with another person who just changed the name to "Glad.wav" and got it to work. Before sharing it I added the sound file everywhere in every folder of the project. So now the step is to delete them one by one to see where it fails. Now I have another problem though, I can't get my setup to play the sound through my simulator, I have updated Catalina and Xcode. I also tried to make some new simulators, but I have run out of ideas to try. But it plays fine on another pc – Philiong Aug 29 '19 at 08:42
  • Update: IT WORKS I ran the normal iPhoneXR simulator in another project and did nothing in it. 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. I was speechless. my GUESS is that the iPhone simulator made a configuration but I have no idea. – Philiong Aug 29 '19 at 09:13

0 Answers0