import UIKit
import Foundation
class PublicFunction: NSObject {
//MARK:GCD實(shí)現(xiàn)定時(shí)器
///
/// - Parameters:
/// - timeInterval: 間隔時(shí)間
/// - handler: 事件
/// - needRepeat: 是否重復(fù)
static func dispatchTimer(timeInterval: Double, handler: @escaping (DispatchSourceTimer?) -> Void, needRepeat: Bool) {
let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
timer.schedule(deadline: .now(), repeating: timeInterval)
timer.setEventHandler {
DispatchQueue.main.async {
if needRepeat {
handler(timer)
} else {
timer.cancel()
handler(nil)
}
}
}
timer.resume()
}
}
import UIKit
import Foundation
class PublicFunction: NSObject {
//MARK:document文檔目錄
static func documentPath() -> String {
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
return documentPath
}
//MARK:文件路徑
static func downloadDirectory(path:String) -> String {
let downloadFile = PublicFunction.documentPath() + "/\(path)/"
let fileManager = FileManager.default
if fileManager.fileExists(atPath: downloadFile) == false {
do {
try fileManager.createDirectory(atPath: downloadFile, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
}
return downloadFile
}
}
import UIKit
import AVFoundation
class LocalAudioPlayerTool: NSObject,AVAudioPlayerDelegate {
//單列
static let sharedAudioPlayer = LocalAudioPlayerTool()
//播放器
var audioPlayer:AVAudioPlayer = AVAudioPlayer()
//播放完畢
var completePlayingBlock:funcBlock?
//MARK:初始化播放器
func playAudioWithPath(audioPath:String) {
let session = AVAudioSession.sharedInstance()
//在音頻播放前扰路,首先創(chuàng)建一個(gè)異常捕捉語(yǔ)句
do {
//啟動(dòng)音頻會(huì)話管理尤溜,此時(shí)會(huì)阻斷后臺(tái)音樂(lè)播放
try session.setActive(true)
//設(shè)置音頻播放類別,表示該應(yīng)用僅支持音頻播放
try session.setCategory(AVAudioSession.Category.playback)
//將字符串路徑轉(zhuǎn)化為網(wǎng)址路徑
let soundUrl = URL(fileURLWithPath: audioPath)
try audioPlayer = AVAudioPlayer(contentsOf:soundUrl)
//為音頻播放做好準(zhǔn)備
audioPlayer.prepareToPlay()
//設(shè)置音量
audioPlayer.volume = 1.0
//是否循環(huán)播放
//audioPlayer.numberOfLoops = -1
audioPlayer.delegate = self
} catch {
print(error)
}
}
//MARK:開(kāi)始播放
func playAudio() {
if audioPlayer.duration > 0 {
audioPlayer.play()
}
}
//MARK:暫停播放
func pauseAudio() {
if audioPlayer.duration > 0 {
audioPlayer.pause()
}
}
//MARK:停止播放
func stopAudio() {
if audioPlayer.duration > 0 {
audioPlayer.stop()
}
completePlayingBlock?()
}
//MARK:播放時(shí)長(zhǎng)
func currentTime() -> (TimeInterval) {
return audioPlayer.currentTime
}
//MARK:音頻時(shí)長(zhǎng)
func duration() -> (TimeInterval) {
return audioPlayer.duration
}
//MARK:修改播放進(jìn)度
func playWithPercent(percent:Double) {
if audioPlayer.duration > 0 {
audioPlayer.currentTime = audioPlayer.duration * percent
}
}
//MARK:修改音量大小
func updateVolume(volume:Float) {
audioPlayer.volume = volume
}
//MARK:播放進(jìn)度
func percent() -> (Float) {
var percent:Float = 0
if audioPlayer.duration > 0 {
percent = Float(audioPlayer.currentTime/audioPlayer.duration)
}
return percent
}
//MARK:播放完畢
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
self.stopAudio()
}
}
//單列初始化
let savePath = PublicFunction.downloadDirectory(path: "download") + "111.mp3"
LocalAudioPlayerTool.sharedAudioPlayer.playAudioWithPath(audioPath: savePath)
//開(kāi)始播放
LocalAudioPlayerTool.sharedAudioPlayer.playAudio()
//暫停播放
LocalAudioPlayerTool.sharedAudioPlayer.pauseAudio()
//拖動(dòng)修改進(jìn)度
LocalAudioPlayerTool.sharedAudioPlayer.playWithPercent(percent: Double(sender.value/100))
//MARK:更新播放進(jìn)度
func updateAudioProgress() {
PublicFunction.dispatchTimer(timeInterval: 1, handler: { _ in
print("播放進(jìn)度---\(LocalAudioPlayerTool.sharedAudioPlayer.percent())")
}
}