【发布时间】:2020-05-11 15:39:29
【问题描述】:
我想显示如下的传入屏幕。在显示此屏幕时,我想播放铃声文件 (mp3)。我在窗口对象之前添加了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 >>>>>>>>>>>>>>>>>>>>>>>>")
}
}
}
}
【问题讨论】:
-
你发现了吗?
标签: ios swiftui avaudioplayer swift5