无法使用 .onTapGesture 播放声音(应用程序包中的 mp3 文件)
这几乎是我第一次尝试编写我的第一个应用程序,而且我基本上仍在学习基础知识。我正在构建一个基于闪存卡的动物应用程序,其中图像从数组显示(带有来自本地 JSON 文件的解析数据)。动物的每个对象都包含名称、图像名称和声音名称作为字符串。我编写了一个函数来从我的 cardView 中调用 playSound() ,当点击该函数时,将播放动物对象的相应声音(使用基于索引的 for 循环)。这工作得很好,直到我的文件不同步并且必须再次编码。然而,代码对我来说看起来是一样的,而且我似乎根本无法播放声音。任何帮助将不胜感激。
我的 cardView 主体视图中的代码片段:
GeometryReader { geo in
TabView { ForEach (0..<model.animals.count, id: \.self) { index in
ZStack {
Rectangle()
.foregroundColor(.white)
VStack {
Image(model.animals[index].image)
.resizable()
.aspectRatio(contentMode: .fill)
Text(model.animals[index].name)
.foregroundColor(.black)
.font(.title)
.padding(.bottom, 10)
}
.onTapGesture {
playSound(sound: model.animals[index].sound, type: "mp3")
}
}
.frame(width: geo.size.width - 40, height: geo.size.height - 50, alignment: .center)
.cornerRadius(10)
.shadow(color: .black, radius: 10, x: -5, y: 5)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .never))
}
我的 playSound 函数在捆绑包中自己的 swift 文件中编码如下:
import Foundation
import AVFoundation
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
}
catch {
print("Could not find and play the sound file.")
}
}
}
我尝试使用相同的 playSound() 方法调用运行以前的项目,尽管针对不同的应用程序(另一个基于抽认卡的应用程序)循环遍历对象数组)。尽管 playSound 方法是相同的,但它不适用于我上面的代码。任何帮助或指导将不胜感激。另外,我明白我可能解释得不太好或者可能需要进一步澄清,如果需要的话我很乐意这样做。我将不胜感激您对我的无知的耐心。
this is pretty much my first attempt at coding my first app and I am essentially still learning the foundations. I am building a flash card based animals app, where images are displayed from an array (with parsed data from a local JSON file). Each object of the animals contains the name, image name, and sound names as strings. I coded a function to call playSound() from within my cardView that when tapped will play the corresponding sound of the animal object (using an index based for loop). This worked perfectly fine until my files went out of sync and had to be coded again. However, the code looks identical to me and I cannot seem to get the sounds to play at all. Any help will be appreciated.
The code snippet from my cardView's body view:
GeometryReader { geo in
TabView { ForEach (0..<model.animals.count, id: \.self) { index in
ZStack {
Rectangle()
.foregroundColor(.white)
VStack {
Image(model.animals[index].image)
.resizable()
.aspectRatio(contentMode: .fill)
Text(model.animals[index].name)
.foregroundColor(.black)
.font(.title)
.padding(.bottom, 10)
}
.onTapGesture {
playSound(sound: model.animals[index].sound, type: "mp3")
}
}
.frame(width: geo.size.width - 40, height: geo.size.height - 50, alignment: .center)
.cornerRadius(10)
.shadow(color: .black, radius: 10, x: -5, y: 5)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .never))
}
My playSound function is coded as follows in its own swift file in the bundle:
import Foundation
import AVFoundation
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
}
catch {
print("Could not find and play the sound file.")
}
}
}
I have tried running through a previous project with identical playSound() method call albeit for a different application (another flashcard based app looping through an array of objects). Although the playSound method is identical, it does not work for my code above. Any help or direction will be most appreciated. Also, I appreciate I may not be explaining very well or may need to clarify further, and I am happy to do this if needed. Your patience with my ignorance will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直面临着同样的问题,并研究了我实现这段代码的多个地方,
对我来说,将文件从 mp3 更改为 wav 使这段代码可以工作。我不确定为什么音频文件在 mp3 文件上时无法渲染,但它对我有用。希望有帮助!
I've been facing hte same issue and looked into multiple places i implemented this piece of code
For me changing the file from mp3 to wav made this piece of code work. I'm not sure why the audio file can't get rendered when it's on mp3 file but it worked for me. Hope it helps!