// // PlayVoiceManager.swift // yinmeng-ios // // Created by yinmeng on 2024/2/29. // import UIKit import Alamofire class PlayVoiceManager: NSObject,AVAudioPlayerDelegate { static let shared = PlayVoiceManager() var player:AVAudioPlayer? var isPlay = false func stopPlayVoice(){ guard let player = player else{return} player.stop() self.player = nil isPlay = false } func beginPlayVoice(url:String){ stopPlayVoice() try? AVAudioSession.sharedInstance().setCategory(.playback, options: .allowBluetooth) try? AVAudioSession.sharedInstance().setActive(true) guard let _url = URL.init(string: url) else { return } self.player = try? AVAudioPlayer(contentsOf: _url) self.player?.volume = 1 self.player?.numberOfLoops = -1 self.player?.delegate = self self.player?.prepareToPlay() self.player?.play() isPlay = true } func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { } func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) { } func downloadVoice(url:String){ let list = url.components(separatedBy: "/") var name = "voiceList" if list.isEmpty == false,let getName = list.last{ name = getName } let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as String let allPath = path + "/kUserVoice/" + name if FileManager.default.fileExists(atPath: allPath){ beginPlayVoice(url: allPath) }else{ try? FileManager.default.createDirectory(atPath: allPath, withIntermediateDirectories: true, attributes: nil) downloadDataWithUrl(url: url, path: allPath) } } func downloadDataWithUrl(url:String,path:String){ //请求 let request = URLRequest(url: URL(string: url)!) let session = URLSession.shared //下载任务 let downloadTask = session.downloadTask(with: request, completionHandler: { (location:URL?, response:URLResponse?, error:Error?) -> Void in guard let locationPath = location?.path else{return} do { if FileManager.default.fileExists(atPath: path){ try FileManager.default.removeItem(atPath: path) } // 文件移动至document try FileManager.default.copyItem(atPath: locationPath, toPath: path) // main DispatchQueue.main.async { self.beginPlayVoice(url:path) } } catch let error { HUDTool.show(with: "\(error.localizedDescription)") } }) //使用resume方法启动任务 downloadTask.resume() } static func downloadFile(url:String,complete:@escaping(_ url:String)->Void,fail:@escaping()->Void){ let list = url.components(separatedBy: "/") var name = "FileList" if list.isEmpty == false,let getName = list.last{ name = getName } let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as String let allPath = path + "/kUserFile/" + name if FileManager.default.fileExists(atPath: allPath){ complete(allPath) }else{ do { try FileManager.default.createDirectory(atPath: allPath, withIntermediateDirectories: true, attributes: nil) //请求 let request = URLRequest(url: URL(string: url)!) let session = URLSession.shared //下载任务 let downloadTask = session.downloadTask(with: request, completionHandler: { (location:URL?, response:URLResponse?, error:Error?) -> Void in guard let locationPath = location?.path else{return} do { if FileManager.default.fileExists(atPath: allPath){ try FileManager.default.removeItem(atPath: allPath) } // 文件移动至document try FileManager.default.copyItem(atPath: locationPath, toPath: allPath) // main DispatchQueue.main.async { complete(allPath) } } catch let error { fail() } }) //使用resume方法启动任务 downloadTask.resume() }catch let error{ fail() } } } }