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
|
|
|
|
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.init(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) {
|
|
|
|
print(flag)
|
|
|
|
}
|
|
|
|
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
|
|
|
|
print(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
|
|
|
|
|
2024-03-04 14:14:33 +08:00
|
|
|
|
2024-03-01 16:54:46 +08:00
|
|
|
let allPath = path + "/kUserVoice/" + name
|
2024-03-04 14:14:33 +08:00
|
|
|
try? FileManager.default.removeItem(atPath: allPath)
|
2024-03-01 16:54:46 +08:00
|
|
|
if FileManager.default.fileExists(atPath: allPath){
|
|
|
|
beginPlayVoice(url: allPath)
|
|
|
|
}else{
|
2024-03-04 14:14:33 +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){
|
|
|
|
|
|
|
|
//请求
|
|
|
|
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
|
2024-03-04 14:14:33 +08:00
|
|
|
let locationPath = location!.path
|
|
|
|
let getUlr = URL.init(string: path)
|
|
|
|
let locationUrl = URL.init(string: locationPath)
|
|
|
|
|
|
|
|
|
|
|
|
let data = try? NSData.dataWithContentsOfMappedFile(locationPath)
|
|
|
|
if let _data = data as? Data{
|
|
|
|
do{
|
|
|
|
FileManager.default.createFile(atPath: path, contents: _data)
|
|
|
|
self.beginPlayVoice(url:path)
|
|
|
|
}
|
|
|
|
catch{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-01 16:54:46 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
//使用resume方法启动任务
|
|
|
|
downloadTask.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|