Hi I just released an app which is live. i have a strange issue: while the audio files in the app play fine on my device, but some users are unable to hear. One friend said it played yesterday but not today. Any idea why? The files are mp3, I see them in Build Phase, and in the project obviously. Here's the audio view code, thank you!
import AVFoundation
struct MeditationView: View {
@State private var player: AVAudioPlayer?
@State private var isPlaying = false
@State private var selectedMeditation: String?
var isiPad = UIDevice.current.userInterfaceIdiom == .pad
let columns = [GridItem(.flexible()),GridItem(.flexible())]
let tracks = ["Intro":"intro.mp3",
"Peace" : "mysoundbath1.mp3",
"Serenity" : "mysoundbath2.mp3",
"Relax" : "mysoundbath3.mp3"]
var body: some View {
VStack{
VStack{
VStack{
Image("dhvani").resizable().aspectRatio(contentMode: .fit)
.frame(width: 120)
Text("Enter the world of Dhvani soundbath sessions, click lotus icon to play.")
.font(.custom("Times New Roman", size: 20))
.lineLimit(nil)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.italic()
.foregroundStyle(Color.ashramGreen)
.padding()
}
LazyVGrid(columns:columns, spacing:10){
ForEach(tracks.keys.sorted(),id:\.self){ track in
Button {
self.playMeditation(named: tracks[track]!)
} label: {
Image("lotus")
.resizable()
.frame(width: 40,height: 40)
.background(Color.ashramGreen)
.cornerRadius(10)
}
Text(track)
.font(.custom("Times New Roman", size: 22))
.foregroundStyle(Color.ashramGreen)
.italic()
}
}
HStack(spacing:20) {
Button(action: { self.togglePlayPause() }) {
Image(systemName: isPlaying ? "playpause.fill" : "play.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(Color.ashramGreen)
}
Button(action: {
self.stopMeditation()
}) {
Image(systemName: "stop.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(Color.ashramGreen)
}
}
}.padding()
.background(Color.ashramBeige)
.cornerRadius(20)
Spacer()
//video play
VStack{
Text("Chant")
.font(.custom("Times New Roman", size: 24))
.foregroundStyle(Color.ashramGreen)
.padding(5)
WebView(urlString: "https://www.youtube.com/embed/ny3TqP9BxzE") .frame(height: isiPad ? 400 : 200)
.cornerRadius(10)
.padding()
Text("Courtesy Sri Ramanasramam").font(.footnote).italic()
}
}.background(Color.ashramBeige)
}
//View
func playMeditation(named name: String) {
if let url = Bundle.main.url(forResource: name, withExtension: nil) {
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
isPlaying = true
} catch {
print("Error playing meditation")
}
}
}
func togglePlayPause() {
if let player = player {
if player.isPlaying {
player.pause()
isPlaying = false
} else {
player.play()
isPlaying = true
}
}
}
func stopMeditation() {
player?.stop()
isPlaying = false
}
}
#Preview {
MeditationView()
}