gpt4 book ai didi

ios - 实时更新 slider 值 AVAudioPlayer SwiftUI

转载 作者:行者123 更新时间:2023-12-01 16:05:12 32 4
gpt4 key购买 nike

所以我有一个名为音频设置的类,如下所示:

class audioSettings: ObservableObject {

var audioPlayer: AVAudioPlayer?
var playing = false
var playValue: TimeInterval = 0.0
var playerDuration: TimeInterval = 146
var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()


func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
if playing == false {
if (audioPlayer == nil) {


audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.prepareToPlay()

audioPlayer?.play()
playing = true
}

}
if playing == false {

audioPlayer?.play()
playing = true
}


} catch {
print("Could not find and play the sound file.")
}
}

}



func stopSound() {
// if playing == true {
audioPlayer?.stop()
audioPlayer = nil
playing = false
playValue = 0.0


// }
}

func pauseSound() {
if playing == true {
audioPlayer?.pause()
playing = false


}
}


func changeSliderValue() {
if playing == true {
pauseSound()
audioPlayer?.currentTime = playValue

}

if playing == false {


audioPlayer?.play()
playing = true
}
}
}
当我在 View 中实现代码时,我成功地播放了文件。我成功地停止了它,并且成功地移动了 slider 并在音频中获得了新的位置。
我正在努力的地方是让 slider 在音频播放时实时更新。如果我点击播放, slider 将什么也不做,直到我点击暂停。此时它将移动到正确的位置。
这是 View 代码:
struct myExperienceFearChunk: View {


@ObservedObject var audiosettings = audioSettings()


var body: some View {


HStack {
Button(action: {
if (self.playButton == Image(systemName: "play.circle")) {
print("All Done")
self.audiosettings.playSound(sound: "audio file", type: "mp3")
self.audiosettings.timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
self.playButton = Image(systemName: "pause.circle")

} else {

self.audiosettings.pauseSound()
self.playButton = Image(systemName: "play.circle")


}


}) {
self.playButton
.foregroundColor(Color.white)
.font(.system(size: 44))
}
Button(action: {
print("All Done")
self.audiosettings.stopSound()
self.playButton = Image(systemName: "play.circle")
self.audiosettings.playValue = 0.0

}) {
self.stopButton
.foregroundColor(Color.white)
.font(.system(size: 44))

}
}
Slider(value: $audiosettings.playValue, in: TimeInterval(0.0)...audiosettings.playerDuration, onEditingChanged: { _ in
self.audiosettings.changeSliderValue()
})
.onReceive(audiosettings.timer) { _ in

if self.audiosettings.playing {
if let currentTime = self.audiosettings.audioPlayer?.currentTime {
self.audiosettings.playValue = currentTime

if currentTime == TimeInterval(0.0) {
self.audiosettings.playing = false
}
}

}
else {
self.audiosettings.playing = false
self.audiosettings.timer.upstream.connect().cancel()
}
}
}
}
}
我知道 .onReceive 是我点击暂停时更新 slider 的位置,因为没有它,除非拖动,否则 slider 根本不会更新。
任何想法如何让 slider 在音频播放时更新设置 playValue 。
谢谢。

最佳答案

这是固定代码。使用 Xcode 11.4/iOS 13.4 测试
修复的主要内容是制作 @Published var playValue发布以使观察到的对象有机会获得有关 audioSettings 更改的通知从而更新Slider ……其他的都是次要的。

class audioSettings: ObservableObject {

var audioPlayer: AVAudioPlayer?
var playing = false
@Published var playValue: TimeInterval = 0.0
var playerDuration: TimeInterval = 146
var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()


func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
if playing == false {
if (audioPlayer == nil) {


audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.prepareToPlay()

audioPlayer?.play()
playing = true
}

}
if playing == false {

audioPlayer?.play()
playing = true
}


} catch {
print("Could not find and play the sound file.")
}
}

}

func stopSound() {
// if playing == true {
audioPlayer?.stop()
audioPlayer = nil
playing = false
playValue = 0.0
// }
}

func pauseSound() {
if playing == true {
audioPlayer?.pause()
playing = false
}
}

func changeSliderValue() {
if playing == true {
pauseSound()
audioPlayer?.currentTime = playValue

}

if playing == false {
audioPlayer?.play()
playing = true
}
}
}

struct myExperienceFearChunk: View {
@ObservedObject var audiosettings = audioSettings()
@State private var playButton: Image = Image(systemName: "play.circle")

var body: some View {

VStack {
HStack {
Button(action: {
if (self.playButton == Image(systemName: "play.circle")) {
print("All Done")
self.audiosettings.playSound(sound: "filename", type: "wav")
self.audiosettings.timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
self.playButton = Image(systemName: "pause.circle")

} else {

self.audiosettings.pauseSound()
self.playButton = Image(systemName: "play.circle")
}
}) {
self.playButton
.foregroundColor(Color.white)
.font(.system(size: 44))
}
Button(action: {
print("All Done")
self.audiosettings.stopSound()
self.playButton = Image(systemName: "play.circle")
self.audiosettings.playValue = 0.0

}) {
Image(systemName: "stop.circle")
.foregroundColor(Color.white)
.font(.system(size: 44))
}
}
Slider(value: $audiosettings.playValue, in: TimeInterval(0.0)...audiosettings.playerDuration, onEditingChanged: { _ in
self.audiosettings.changeSliderValue()
})
.onReceive(audiosettings.timer) { _ in

if self.audiosettings.playing {
if let currentTime = self.audiosettings.audioPlayer?.currentTime {
self.audiosettings.playValue = currentTime

if currentTime == TimeInterval(0.0) {
self.audiosettings.playing = false
}
}

}
else {
self.audiosettings.playing = false
self.audiosettings.timer.upstream.connect().cancel()
}
}
}
}
}

关于ios - 实时更新 slider 值 AVAudioPlayer SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63324235/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com