Files
yingmeng-ios-switf/yinmeng-ios/Modules/Home/PlayVoiceManager.swift

144 lines
5.0 KiB
Swift
Raw Normal View History

2024-03-01 16:54:46 +08:00
//
// PlayVoiceManager.swift
// yinmeng-ios
//
2024-03-04 14:14:33 +08:00
// Created by yinmeng on 2024/2/29.
2024-03-01 16:54:46 +08:00
//
import UIKit
import Alamofire
2024-03-07 18:18:25 +08:00
class PlayVoiceManager: NSObject,AVAudioPlayerDelegate {
static let shared = PlayVoiceManager()
var player:AVAudioPlayer?
2024-03-01 16:54:46 +08:00
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
}
2024-03-07 18:18:25 +08:00
self.player = try? AVAudioPlayer(contentsOf: _url)
2024-03-01 16:54:46 +08:00
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) {
2024-03-07 18:18:25 +08:00
2024-03-01 16:54:46 +08:00
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
2024-03-07 18:18:25 +08:00
2024-03-01 16:54:46 +08:00
}
2024-03-07 18:18:25 +08:00
func downloadVoice(url:String){
2024-03-01 16:54:46 +08:00
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{
2024-03-07 18:18:25 +08:00
try? FileManager.default.createDirectory(atPath: allPath, withIntermediateDirectories: true, attributes: nil)
2024-03-01 16:54:46 +08:00
downloadDataWithUrl(url: url, path: allPath)
}
}
func downloadDataWithUrl(url:String,path:String){
2024-03-07 18:18:25 +08:00
//
let request = URLRequest(url: URL(string: url)!)
let session = URLSession.shared
//
let downloadTask = session.downloadTask(with: request,
completionHandler: { (location:URL?, response:URLResponse?, error:Error?)
2024-03-01 16:54:46 +08:00
-> Void in
2024-03-07 18:27:47 +08:00
guard let locationPath = location?.path else{return}
2024-03-07 18:18:25 +08:00
do {
2024-03-07 18:27:47 +08:00
if FileManager.default.fileExists(atPath: path){
try FileManager.default.removeItem(atPath: path)
}
2024-03-07 18:18:25 +08:00
// 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)")
2024-03-04 14:14:33 +08:00
}
2024-03-07 18:18:25 +08:00
})
2024-03-04 14:14:33 +08:00
2024-03-01 16:54:46 +08:00
//使resume
downloadTask.resume()
}
2024-03-23 16:28:10 +08:00
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()
}
}
}
2024-03-07 18:18:25 +08:00
2024-03-01 16:54:46 +08:00
}
2024-03-07 18:18:25 +08:00