Hello, everyone,
I have a problem I'm stuck with and have been trying to solve without success for the past 2 weeks.
I am developing a SwiftUI application for Apple Watch which for now I am only running on the simulator. This application has to play sounds but unfortunately so far without success for Apple Watch simulator.
When I tap the button on the application running on the Series 9 (45mm) simulator with watchOS 10.2. I cannot hear any sounds on my MacBook Pro. This happens for all Apple Watch simulators I have in my MacBook.
The same exact code works on the iPhone 15 simulator with iOS 17.2 and I hear the file 1.mp3 from MacBook Pro speakers and also from bluetooth hearphones, if I connect them.
The code is this
import SwiftUI
import AVFoundation
struct ContentView: View {
@State var audioPlayer:AVPlayer?
@State var isPlaying : Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Button("Play"){
if let path = Bundle.main.path(forResource: "1", ofType: "mp3") {
let fileUrl = URL(fileURLWithPath: path)
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = AVPlayer(url: fileUrl)
guard let audioPlayer = audioPlayer else { return }
audioPlayer.play()
} catch {
}
}
}
}
.padding()
}
}
Thanks for your support!