如何将字符串数组从视图模型传递到 swiftUI 中播放歌曲
Posted
技术标签:
【中文标题】如何将字符串数组从视图模型传递到 swiftUI 中播放歌曲【英文标题】:How to pass array of string from to view model to play song in swiftUI 【发布时间】:2021-01-27 10:36:28 【问题描述】:我创建了一个模型并添加了嵌套结构。根据结构详细信息之一创建了一个数组,我想从模型中检索详细信息以查看模型以播放音乐。
我想检索这个“aURL”字符串并将其插入到播放函数中以播放音乐。
class myViewModel: ObservableObject
@Published var isPlaying = false
var player = AVAudioPlayer()
func play()
do
player = try AVAudioPlayer(contentsOf: Array[0] as URL )
if player.isPlayingplayer.pause()
elseplayer.play()
isPlaying = player.isPlaying
catchprint("error")
【问题讨论】:
【参考方案1】:将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.isPlayingplayer.pause()
elseplayer.play()
isPlaying = player.isPlaying
catchprint("error")
注意:类名和结构名以大写字母开头。
【讨论】:
感谢您的回复。但是 var arrData = myModel.feelSongs,在这一行中,即使我已将其更改为静态 var,它也没有检索 feelSongs 变量以查看模型。 它仍然不能与 static 关键字一起使用,但是当我只与 var 关键字(如(var arrData = myModel.feelSongs)一起使用时,它会出现但我得到“不能使用实例成员'myModel'在属性初始化器中;属性初始化器在“self”可用之前运行”【参考方案2】:你的结构嵌套在结构中很奇怪,反正我不知道你的想法。
请测试一下:
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.isPlayingplayer.pause()
elseplayer.play()
isPlaying = player.isPlaying
catchprint("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()
【讨论】:
非常感谢您的回复。但是如何通过播放按钮而不是列表来播放歌曲。 我想让这首歌一个接一个地自动播放。用户无法选择播放哪一个 @binavol329 我编辑了答案,反正改标题,你的要求现在不一样了以上是关于如何将字符串数组从视图模型传递到 swiftUI 中播放歌曲的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI:如何将 NSManagedObjectContext 传递到多个视图模型中
将变量从 UIHostingController 传递到 SWIFTUI 视图
将数据从 UIViewRepresentable 函数传递到 SwiftUI 视图