SwiftUI项目中如何使用AVAudioPlayer播放音频
How to play audio using AVAudioPlayer in SwiftUI project
使用此方法无法播放音频。
play() 函数正在执行,没有任何错误。
请帮忙
var audioPlayer = AVAudioPlayer()
let path = Bundle.main.path(forResource: "a", ofType: "mp3")
@State var isPlaying : Bool = false
var body: some View {
Button(action: {
self.isPlaying.toggle()
let url = URL(fileURLWithPath: self.path!)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url)
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}catch {
print("Eror")
}
}, label: {
if isPlaying {
Image(systemName: "pause")
.font(Font.system(.largeTitle).bold())
}else {
Image(systemName: "play.fill")
.font(Font.system(.largeTitle).bold())
}
})
}
音频文件在吗?请 select 项目,转到构建阶段选项卡,然后在 'Copy Bundle Resources' 下您必须看到音频文件。如果有那么就是这个问题了。
我试过你的代码,它播放了声音然后崩溃了。我这样更改它以使其工作
@State var audioPlayer:AVAudioPlayer?
@State var isPlaying : Bool = false
var body: some View {
Button(action: {
if let path = Bundle.main.path(forResource: "a", ofType: ".mp3") {
self.audioPlayer = AVAudioPlayer()
self.isPlaying.toggle()
let url = URL(fileURLWithPath: path)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url)
self.audioPlayer?.prepareToPlay()
self.audioPlayer?.play()
}catch {
print("Error")
}
}
}, label: {
----
您是否考虑过将音频模型与 UI 分开?如果将代码放在单独的 Swift 文件
中,它会使您的代码更加清晰
import AVFoundation
class Sounds {
static var audioPlayer:AVAudioPlayer?
static func playSounds(soundfile: String) {
if let path = Bundle.main.path(forResource: soundfile, ofType: nil){
do{
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}catch {
print("Error")
}
}
}
}
只需一行即可在 UI
中使用它
var body: some View {
Button(action: {
self.isPlaying.toggle()
Sounds.playSounds(soundfile: "0.wav")
}, label: {
使用此方法无法播放音频。 play() 函数正在执行,没有任何错误。 请帮忙
var audioPlayer = AVAudioPlayer()
let path = Bundle.main.path(forResource: "a", ofType: "mp3")
@State var isPlaying : Bool = false
var body: some View {
Button(action: {
self.isPlaying.toggle()
let url = URL(fileURLWithPath: self.path!)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url)
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}catch {
print("Eror")
}
}, label: {
if isPlaying {
Image(systemName: "pause")
.font(Font.system(.largeTitle).bold())
}else {
Image(systemName: "play.fill")
.font(Font.system(.largeTitle).bold())
}
})
}
音频文件在吗?请 select 项目,转到构建阶段选项卡,然后在 'Copy Bundle Resources' 下您必须看到音频文件。如果有那么就是这个问题了。
我试过你的代码,它播放了声音然后崩溃了。我这样更改它以使其工作
@State var audioPlayer:AVAudioPlayer?
@State var isPlaying : Bool = false
var body: some View {
Button(action: {
if let path = Bundle.main.path(forResource: "a", ofType: ".mp3") {
self.audioPlayer = AVAudioPlayer()
self.isPlaying.toggle()
let url = URL(fileURLWithPath: path)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url)
self.audioPlayer?.prepareToPlay()
self.audioPlayer?.play()
}catch {
print("Error")
}
}
}, label: {
----
您是否考虑过将音频模型与 UI 分开?如果将代码放在单独的 Swift 文件
中,它会使您的代码更加清晰import AVFoundation
class Sounds {
static var audioPlayer:AVAudioPlayer?
static func playSounds(soundfile: String) {
if let path = Bundle.main.path(forResource: soundfile, ofType: nil){
do{
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}catch {
print("Error")
}
}
}
}
只需一行即可在 UI
中使用它var body: some View {
Button(action: {
self.isPlaying.toggle()
Sounds.playSounds(soundfile: "0.wav")
}, label: {