207 lines
7.9 KiB
Swift
207 lines
7.9 KiB
Swift
//
|
||
// Macro.swift
|
||
// MusicPlayer
|
||
//
|
||
// Created by Mr.Zhou on 2024/3/25.
|
||
//
|
||
import UIKit
|
||
import Foundation
|
||
import AVFoundation
|
||
import AppTrackingTransparency
|
||
import AdSupport
|
||
@_exported import JXSegmentedView
|
||
@_exported import JXPagingView
|
||
//给JXPagingListContainerView添加extension,表示遵从JXSegmentedViewListContainer的协议
|
||
extension JXPagingListContainerView: JXSegmentedViewListContainer {}
|
||
//MARK: - 常用宏定义
|
||
///屏幕宽
|
||
let screen_Width = UIScreen.main.bounds.width
|
||
///屏幕高
|
||
let screen_Height = UIScreen.main.bounds.height
|
||
///像素比值
|
||
let width = screen_Width / 375
|
||
///状态栏高度
|
||
#if __IPHONE_13_0
|
||
let statusBarHeight:CGFloat = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.size.height
|
||
#else
|
||
let statusBarHeight:CGFloat = UIApplication.shared.statusBarFrame.size.height
|
||
#endif
|
||
///状态栏和导航栏的总高度
|
||
let navAndstatusBarHeight = statusBarHeight + 44
|
||
///判断是否是刘海屏
|
||
let iphoneX = ((statusBarHeight != 20) ? true : false)
|
||
///获取版本号
|
||
let LOCAL_RELEASE_VERSION = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
|
||
///获取程序名字
|
||
let App_Name = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
|
||
///获取手机型号
|
||
let Phone_Model = UIDevice.current.model
|
||
///系统版本号
|
||
let System_Version = UIDevice.current.systemVersion
|
||
///获取当前系统语言
|
||
let Language_first_local = NSLocale.preferredLanguages.first!
|
||
///当前应用版本号
|
||
var app_Version:String{
|
||
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String {
|
||
return version
|
||
}else {
|
||
return "1.0.0"
|
||
}
|
||
}
|
||
///底部安全区域
|
||
let bottomPadding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
|
||
///全局占位图
|
||
let placeholderImage:UIImage = UIImage(named: "Home First'placeholder")!
|
||
///隐私政策网址
|
||
let privacyUrl:URL = .init(string: "https://musiclax.mystrikingly.com/privacy")!
|
||
///用户协议网址
|
||
let serviceUrl:URL = .init(string: "https://musiclax.mystrikingly.com/terms")!
|
||
|
||
//MARK: - 全局变量与方法
|
||
///总事件闭包
|
||
typealias ActionBlock = () -> Void?
|
||
///A面全局模态弹出类型
|
||
var MPSideA_ModalType:MPSideA_PresentModal = .Timer
|
||
///B面全局模态弹出类型
|
||
var MPPositive_ModalType:MPPositive_PresentModal = .PlayerList
|
||
///沙盒文件
|
||
let DocumentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||
///获取沙盒下载路径
|
||
func getDocumentsFileURL(_ videoID: String) -> String? {
|
||
// 获取Documents目录的URL
|
||
let documentsDirectoryURL = DocumentsURL.appendingPathComponent("Downloads")
|
||
// 根据videoId构建文件完整URL
|
||
let fileURL = documentsDirectoryURL.appendingPathComponent("\(videoID).mp4")
|
||
//检索是否存在
|
||
if FileManager.default.fileExists(atPath: fileURL.path) == true {
|
||
//存在
|
||
return fileURL.absoluteString
|
||
}else {
|
||
return nil
|
||
}
|
||
}
|
||
///调用next对单曲数据歌词ID与相关ID补全
|
||
func improveDataforLycirsAndRelated(_ song:MPPositive_SongItemModel, completion:@escaping(((String?,String?)) -> Void)) {
|
||
//单曲补全需要再次调用next接口
|
||
MP_NetWorkManager.shared.requestNextLyricsAndRelated(song){ result in
|
||
completion(result)
|
||
}
|
||
}
|
||
///调用player对资源路径和封面路径补全
|
||
func improveDataforResouceAndCover(_ song:MPPositive_SongItemModel, completion:@escaping((([String],[Int],[String]), [String]?) -> Void)) {
|
||
//单曲补全需要调用player接口
|
||
MP_NetWorkManager.shared.requestAndroidPlayer(song.videoId, playlistId: "") { resourceUrls, coverUrls in
|
||
completion(resourceUrls,coverUrls)
|
||
}
|
||
}
|
||
///转时分值
|
||
func setTimesToMinSeconds(_ time:TimeInterval) -> String {
|
||
//设置分钟
|
||
let min = Int(time / 60)
|
||
let second = Int(time.truncatingRemainder(dividingBy: 60))
|
||
return "\(min < 10 ? "0\(min)":"\(min)"):\(second < 10 ? "0\(second)":"\(second)")"
|
||
}
|
||
///转分值
|
||
func setTimesToMins(_ time:TimeInterval) -> String {
|
||
//设置分钟
|
||
let min = Int(time / 60)
|
||
return "\(min < 10 ? "0\(min)":"\(min)")"
|
||
}
|
||
///获取麦克风权限
|
||
func authorize(observe:UIViewController) -> Bool{
|
||
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
|
||
switch status {
|
||
case .authorized:
|
||
return true
|
||
case .notDetermined:
|
||
// 请求授权
|
||
AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: {(status) in
|
||
DispatchQueue.main.async(execute: {() -> Void in
|
||
_ = authorize(observe: observe)
|
||
})
|
||
})
|
||
default: ()
|
||
DispatchQueue.main.async(execute: { () -> Void in
|
||
let alertController = UIAlertController(title: "Get Microphone Access",message: "“Musiclax” asks you to turn on your microphone to recognize the decibels around you and turns on white noise for you automatically. Please go to the “Settings” page to turn on the microphone permission",preferredStyle: .alert)
|
||
let cancelAction = UIAlertAction(title:"Cancel", style: .cancel, handler:nil)
|
||
let settingsAction = UIAlertAction(title:"Settings", style: .default, handler: {
|
||
(action) -> Void in
|
||
let url = URL(string: UIApplication.openSettingsURLString)
|
||
if let url = url, UIApplication.shared.canOpenURL(url) {
|
||
if #available(iOS 10, *) {
|
||
UIApplication.shared.open(url, options: [:],
|
||
completionHandler: {
|
||
(success) in
|
||
})
|
||
} else {
|
||
UIApplication.shared.openURL(url)
|
||
}
|
||
}
|
||
})
|
||
alertController.addAction(cancelAction)
|
||
alertController.addAction(settingsAction)
|
||
observe.present(alertController, animated: true)
|
||
})
|
||
}
|
||
return false
|
||
}
|
||
///创建一个Label
|
||
func createLabel(_ text:String? = nil, font:UIFont, textColor:UIColor, textAlignment:NSTextAlignment, lines:Int = 1) -> UILabel {
|
||
let label = UILabel()
|
||
label.text = text ?? "text"
|
||
label.font = font
|
||
label.textColor = textColor
|
||
label.textAlignment = textAlignment
|
||
label.numberOfLines = lines
|
||
return label
|
||
}
|
||
///根据播放器状态将按钮的图片进行切换
|
||
func switchPlayTypeBtnIcon(_ btn:UIButton) {
|
||
switch MP_PlayerManager.shared.getPlayType() {
|
||
case .normal://列表播放图案
|
||
btn.setBackgroundImage(UIImage(named: "List_NormolPlay'logo"), for: .normal)
|
||
case .random://随机播放图案
|
||
btn.setBackgroundImage(UIImage(named: "Player_Shuffle'logo"), for: .normal)
|
||
case .single://单曲循环图案
|
||
btn.setBackgroundImage(UIImage(named: "Player_Single'logo"), for: .normal)
|
||
}
|
||
}
|
||
///请求广告授权
|
||
func requestTrackingAuthorization(_ observe:UIViewController) -> Bool {
|
||
if #available(iOS 14, *) {
|
||
// 检查当前的跟踪管理器状态
|
||
let status = ATTrackingManager.trackingAuthorizationStatus
|
||
switch status {
|
||
case .notDetermined:
|
||
// 处理未知授权状态
|
||
print("未知的跟踪状态")
|
||
ATTrackingManager.requestTrackingAuthorization { status in
|
||
let isAuthorized = status == .authorized
|
||
DispatchQueue.main.async {
|
||
_ = requestTrackingAuthorization(observe)
|
||
}
|
||
}
|
||
case .authorized:
|
||
// 用户授予了权限,可以获取 IDFA
|
||
print("用户授权跟踪")
|
||
return true
|
||
case .denied:
|
||
print("用户拒绝跟踪")
|
||
default:()
|
||
print("跟踪状态受限")
|
||
}
|
||
return false
|
||
} else {
|
||
return true
|
||
}
|
||
}
|
||
///获取IDFA
|
||
func getIDFA(_ observe:UIViewController) -> UUID? {
|
||
if requestTrackingAuthorization(observe) {
|
||
let idfa = ASIdentifierManager.shared().advertisingIdentifier
|
||
return idfa
|
||
}else {
|
||
return nil
|
||
}
|
||
}
|