简体   繁体   English

如何传递字符串数组以查看 model 以在 swiftUI 中播放歌曲

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

I've created a model and added nested struct.我创建了一个 model 并添加了嵌套结构。 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.根据结构详细信息之一创建了一个数组,我想从 Model 中检索详细信息以查看 model 以播放音乐。

I want to retrieve this "aURL" strings and insert it inside the play function to play the music.我想检索这个“aURL”字符串并将其插入到播放 function 中以播放音乐。

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制作var feelSongs: [myModel.MA]到 static 之类的

static var feelSongs: [myModel.M.A] 

And use it inside view model like this并像这样在视图 model 内使用它

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.注意: Class 名称和结构名称以大写字母开头。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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