[英]Automatically Play audio while showing SwiftUI screen
我想顯示如下所示的傳入屏幕。 在顯示此屏幕時,我想播放鈴聲文件 (mp3)。 我在 window 對象之前添加了Sound.playSounds(soundfile: "ringing.mp3")
行。 但是 mp3 文件沒有播放,但函數 playsounds 正在執行。
DispatchQueue.main.async{
Sound.playSounds(soundfile: "ringing.mp3")
let window = UIApplication.shared.windows.first
let contentView = IncomingCallView(viewNavigator: ViewNavigator())
window?.rootViewController = UIHostingController(rootView:contentView )
window?.makeKeyAndVisible()
}
聲音文件如下所示
import AVFoundation
class Sound {
static var player:AVAudioPlayer?
static func playaudio(soundfile: String) {
if let path = Bundle.main.path(forResource: soundfile, ofType: nil){
print("<<<<<<<<<<<<<<<<<<<<<PATH >>>>>>>>>>>>>>>>>>>>>>>>>",path)
do{
player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
player?.prepareToPlay()
player?.play()
}catch {
print("<<<<<<<<<<<<<<<<<<<<<Error >>>>>>>>>>>>>>>>>>>>>>>>")
}
}
}
}
在出現方法上使用
SomeView()
.onAppear{ Sound.playAudio }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.