简体   繁体   中英

How to pass array of string from to view model to play song in swiftUI

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")}
}
}

Make var feelSongs: [myModel.MA] 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.

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM