146 lines
5.7 KiB
Swift
146 lines
5.7 KiB
Swift
//
|
||
// Macro.swift
|
||
// MusicPlayer
|
||
//
|
||
// Created by Mr.Zhou on 2024/3/25.
|
||
//
|
||
import UIKit
|
||
import Foundation
|
||
import AVFoundation
|
||
@_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!
|
||
|
||
///底部安全区域
|
||
let bottomPadding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
|
||
///全局占位图
|
||
let placeholderImage:UIImage = UIImage(named: "Home First'placeholder")!
|
||
///隐私政策网址
|
||
let privacyUrl:URL = .init(string: "https://musicoo.app/privacy")!
|
||
///用户协议网址
|
||
let serviceUrl:URL = .init(string: "https://musicoo.app/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
|
||
}
|
||
}
|
||
///转时分值
|
||
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: "“Musicoo” 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)
|
||
}
|
||
}
|