0

I've created a model and added nested struct. created an array based on one of the struct details and I want to retrieve the details from Model to view model to play the music.

I want to retrieve this "aURL" strings and insert it inside the play function to play the music.

class myViewModel: ObservableObject {
@Published var isPlaying = false
var player = AVAudioPlayer()

func play(){
    do{
        player = try AVAudioPlayer(contentsOf: Array[0] as URL )
        if player.isPlaying{player.pause()}
        else{player.play()}
        isPlaying = player.isPlaying
        
    }catch{print("error")}
}
}

2 Answers2

0

Make var feelSongs: [myModel.M.A] to static like

static var feelSongs: [myModel.M.A] 

And use it inside view model like this

class MyViewModel: ObservableObject {
    @Published var isPlaying = false
    var player = AVAudioPlayer()
    var arrData = myModel.M.feelSongs
    func play(with url: URL?){
        guard let url = url else {
            return
        }
        do{
            player = try AVAudioPlayer(contentsOf: url )
            if player.isPlaying{player.pause()}
            else{player.play()}
            isPlaying = player.isPlaying
            
        }catch{print("error")}
    }
}

Note: Class Name and struct name start with Capital latter.

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • thank you for the reply. but var arrData = myModel.feelSongs, in this line it didn't retrieve the feelSongs variable to view model even though I've changed it to static var. – binavol329 Jan 27 '21 at 10:54
  • still it didn't work with static keyword but when I use only with var keyword like (var arrData = myModel.feelSongs), it appears but I'm getting "Cannot use instance member 'myModel' within property initializer; property initializers run before 'self' is available" – binavol329 Jan 27 '21 at 11:03
0

Your struct nested in struct nested in struct is strange, anyway I don't know your idea.

please test this:

class MyModel: Codable {

    var feelSongs: [MyModel.M.A] = [
        MyModel.M.A.init(id: 1, afcontent: "Feeling Happy", aURL: "a1.mp3", isOn: true),
        MyModel.M.A.init(id: 2, afcontent: "Feeling Sad", aURL: "a2.mp3", isOn: true),
        MyModel.M.A.init(id: 3, afcontent: "Feeling Positive", aURL: "a3.mp3", isOn: true),
        MyModel.M.A.init(id: 4, afcontent: "Feeling Healthy", aURL: "a4.mp3", isOn: true)
    ]
    struct M: Codable{
        var mURL: String
        var bgMusic : String
        var bgVol : Double

        struct A : Codable, Identifiable {
            var id : Int
            var afcontent : String
            var aURL : String
            var isOn : Bool
            
            
        }
    }
}


class MyViewModel: ObservableObject {
    @Published var isPlaying = false
    var player = AVAudioPlayer()

    var theModel = MyModel()
    
    **//ADDED THIS**
    var playNow = 0

    func play(){

        if playNow >= theModel.feelSongs.count {
            return
        }
        let url = theModel.feelSongs[playNow]
        guard let url = URL(string: url) else {
            return
        }
        
        **//ADDED THIS**
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item) 
        do {
            player = try AVAudioPlayer(contentsOf: url )
            if player.isPlaying{player.pause()}
            else{player.play()}
            isPlaying = player.isPlaying

        }
        catch{print("error")}
    }

    **//ADDED THIS**
    func playerDidFinishPlaying(note: NSNotification) {
        play()
    }
}
//
struct ContentView: View {
    var mymodel = MyViewModel()
    
    var body: some View {
        //Text("3")
        List(mymodel.theModel.feelSongs) { song in
            Text(song.afcontent)                    
        }
        .onAppear {
             mymodel.play()
        }
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30