xcode 视图无法识别@EnvironmentObject的示例

nnsrf1az  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在处理应用程序的音频播放器部分,在播放器视图中,我有一个简单的if语句检查。它看起来确保有audioManager.player的示例,如果有,它会创建vstacks/hstacks等。
由于某种原因,它在检查时失败,并说没有audioManager的示例,也没有加载适当的视图。它不与模拟器一起工作,我在应用程序启动时创建了AudioManager()的示例,它也不工作在预览中,我直接注入示例,这是没有意义的。
我注意到.autoconnect()是白色的。我不确定这是否意味着它没有认识到什么或什么。
下面是视图播放器的代码

import SwiftUI

struct listeningActivity: View {
    @EnvironmentObject var audioManager: AudioManager
    var listeningActivityVM:ListeningActivityViewModel
    
    @State private var value: Double = 0.0
    @State private var isEditing: Bool = false
    
    let timer = Timer.publish(every: 0.5, on: .main, in: .common)
        .autoconnect()
    
    
    var body: some View {
        
        ZStack{
            
            Image("pot")
                .resizable()
                .scaledToFill()
                .frame(width: UIScreen.main.bounds.width)
                .ignoresSafeArea()
            
            VStack(spacing: 32) {
                HStack {
                    Button {
                        
                    } label: {
                        Image(systemName: "xmark.circle.fill")
                            .font(.system(size:36))
                            .foregroundColor(.white)
                    }
                    
                    Spacer()
                }
                
                Text("pasta carbonoara")
                    .font(.title)
                    .foregroundColor(.white)
                
                Spacer()
                
                if let player = audioManager.player {
                    VStack(spacing: 5){
                        
                        Slider(value: $value, in: 0...player.duration) { editing
                            in
                            
                            print("editing", editing)
                            isEditing = editing
                            
                            if !editing {
                                player.currentTime = value
                            }
                        }
                        .scaleEffect(1.5)
                        .frame(width: 200, height: 20)
                        .tint(.orange)
                        .padding(.top, 50)
                        
                        HStack{
                            Text(DateComponentsFormatter.positional
                                .string(from: player.currentTime) ?? "0:00")
                            
                            Spacer()
                            
                            Text(DateComponentsFormatter.positional
                                .string(from: player.duration - player.currentTime) ?? "0:00")
                            
                        }.padding([.leading, .trailing], 40)
                            .padding(.top, 15)
                    }
                    
                    HStack{
                        PlaybackControlButton(systemName: "repeat") {
                            
                        }
                        Spacer()
                        PlaybackControlButton(systemName: "gobackward.10") {
                            
                        }
                        Spacer()
                        PlaybackControlButton(systemName: player.isPlaying ? "pause.circle.fill" : "play.circle.fill", fontSize: 44) {
                            audioManager.playPlause()
                        }
                        Spacer()
                        PlaybackControlButton(systemName: "goforward.10") {
                            
                        }
                        Spacer()
                        PlaybackControlButton(systemName: "stop.fill") {
                            
                        }
                    }
                }
                
            }
            .padding(20)
        }
        .onAppear{
            audioManager.startPlayer(track: listeningActivityVM.audioAct.track)
            
        }.onReceive(timer) { _ in
            guard let player = audioManager.player, !isEditing else {return}
            value = player.currentTime
        }
        
    }
}


struct listeningActivity_Previews: PreviewProvider {
    static let listeningActivityVM  = ListeningActivityViewModel(audioAct: audioActivty.data)
    static var previews: some View {
        listeningActivity(listeningActivityVM: listeningActivityVM)
            .environmentObject(AudioManager())
    }
}

这是我的AudioManager()代码

import Foundation
import AVKit

final class AudioManager: ObservableObject{
    
    var player: AVAudioPlayer?
    
    func startPlayer(track: String) {
        guard let url = Bundle.main.url(forResource: track, withExtension: "wav") else {
            print("Resource not found: \(track)")
            return
        }
        
        
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
            try AVAudioSession.sharedInstance().setActive(true)
            player = try AVAudioPlayer(contentsOf: url)
            
            player?.play()
        }catch{
            print("Fail to initialize player", error)
        }
    }
    
    func playPlause() {
        guard let player = player else {
            print("Instance of audio player not found")
            return
        }
        
        if player.isPlaying {
            player.pause()
        }else {
            player.play()
        }
    }
}
c9qzyr3d

c9qzyr3d1#

我回头看了一下我正在关注的YouTube教程,幸运的是,有些人也有同样的问题,并在评论中解决了问题。
我需要在var player: AVAudioPlayer?声明前面添加@Published属性 Package 器。

相关问题