此代码AVPlayer仅适用于Playground
import AVFoundation
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
Run Code Online (Sandbox Code Playgroud)
当我尝试在物理设备上的 SwiftUI 应用程序上运行它时,使用以下代码:
Button(action:{
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
print("Works")
},label:{
Image("play")
})
Run Code Online (Sandbox Code Playgroud)
它将Works打印到控制台。但是,它不会在设备上播放任何声音。
希望得到任何帮助,在这里找不到任何东西。
非常感谢!