0

我创建了一个模型并添加了嵌套结构。根据结构详细信息之一创建了一个数组,我想从模型中检索详细信息以查看模型以播放音乐。

我想检索这个“aURL”字符串并将其插入到播放函数中以播放音乐。

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

2 回答 2

0

var feelSongs: [myModel.M.A] 静态一样

static var feelSongs: [myModel.M.A] 

并像这样在视图模型中使用它

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

注意:类名和结构名以大写字母开头。

于 2021-01-27T10:42:06.700 回答
0

你的结构嵌套在结构中嵌套在结构中很奇怪,反正我不知道你的想法。

请测试一下:

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()
    }
}
于 2021-01-27T18:49:38.887 回答