BFMusicManager.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // BFMusicManager.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by Harry on 2022/3/4.
  6. //
  7. import Foundation
  8. import BFMediaKit
  9. import BFCommonKit
  10. import BFNetRequestKit
  11. import Alamofire
  12. class BFMusicManager {
  13. public class func downloadMusic(model: PQVoiceModel, complete:(@escaping () -> Void)) {
  14. // BFNetRequestAdaptor.postRequestData(url: "", parames: [:], commonParams: commonParams(), encoding: JSONEncoding.default, isJsonEncodingNormal: true, timein)
  15. if let path = model.musicPath, let url = URL(string: path) {
  16. let request = URLRequest(url: url)
  17. let session = URLSession.shared
  18. let downloadTask = session.downloadTask(with: request,
  19. completionHandler: { (location:URL?, response:URLResponse?, error:Error?)
  20. -> Void in
  21. print("location:\(location)")
  22. if let locationPath = location?.path {
  23. BFMusicManager.moveLocation(locationPath: locationPath, model:model)
  24. }
  25. complete()
  26. })
  27. downloadTask.resume()
  28. }
  29. }
  30. fileprivate class func moveLocation(locationPath: String, model: PQVoiceModel) {
  31. let fileManager = FileManager.default
  32. let fileName = (model.musicPath! as NSString).lastPathComponent
  33. let documnets = bgMusicDirectory + fileName + ".mp3"
  34. createDirectory(path: bgMusicDirectory)
  35. do {
  36. try fileManager.moveItem(atPath: locationPath, toPath: documnets)
  37. model.wavFilePath = documnets
  38. } catch {
  39. BFLog(1, message: "移动失败, \(error)")
  40. }
  41. print("new location:\(documnets)")
  42. }
  43. }