惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
量子位
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AWS News Blog
AWS News Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Proofpoint News Feed
V
V2EX
Martin Fowler
Martin Fowler
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
SecWiki News
SecWiki News
罗磊的独立博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog

博客园 - super1250

技术支持网址 (URL) 填写的地址 通过Homebrew安装CocoaPods iOS游戏内购(记录官网链接) iOS的证书创建 Apple开发者账号注册 __swift_FORCE_LOAD_$_swiftCompatibility50 Validation failed (409) 在 Pod 库中同时使用 Swift 和 Objective-C 拒审原因2.3.4 git生成ssh秘钥 project.pbxproj文件配置详解 iOS内购支付 打包framwork 上架总结 Guideline 3.2.1 清理Cocoapods缓存mac flutter插件 lanh libarclite_iphonesimulator.a模拟器跑不起的问题SDK does not contain 'libarclite' at the path
录音功能iosSDK
super1250 · 2024-12-04 · via 博客园 - super1250

//

//  Fast.swift

//  FastSwiftKit

//

//  Created by XX on 2023/9/7.

//

import AVFoundation

public class FastAudioManager {

    public static let shared = FastAudioManager()

    // MARK: 音频录制

    private var audioRecorder: AVAudioRecorder?

    private var audioSession: AVAudioSession = AVAudioSession.sharedInstance()

    private var maxtime: Int?

    var audioFilesURL:URL?

    /// 开始录音

    /// - Returns: URL

    public func startRecording(_ serverHost:String?,_ quality:String?,_ maxtime:Int?) -> URL?{

        let audioSession = AVAudioSession.sharedInstance()

        do {

            try audioSession.setCategory(.playAndRecord, mode: .default, options: [])

            try audioSession.setActive(true, options: [])

            let settings = [

                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),

                AVSampleRateKey: Int(quality ?? "0"),

                AVNumberOfChannelsKey: 1,

                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue

            ]

            let time = Date().timeIntervalSince1970

            audioFilesURL = documentDirectory.appendingPathComponent("\(Int(time)).m4a")

            audioRecorder = try AVAudioRecorder(url: audioFilesURL!, settings: settings)

            audioRecorder?.record()

            self.maxtime = maxtime

            Timer.scheduledTimer(withTimeInterval: 0, repeats: true) {[weak self] t in

                if Int(self?.audioRecorder?.currentTime ?? 0.0) >= Int(maxtime ?? 0){

                    print("录音时间到期")

                    self?.audioRecorder?.stop()

                    self?.audioRecorder = nil

                    t.invalidate()

                }

            }

            return audioFilesURL

        } catch {

            print("Failed to start recording")

            return nil

        }

    }

    public func getAudioRecorderCurrentTime() -> TimeInterval? {

        return audioRecorder?.currentTime

    }

    /// 停止录音

    public func stopRecording() {

        audioRecorder?.stop()

        audioRecorder = nil

    }

    // MARK: 播放音频

    var audioPlayer: AVAudioPlayer?

    /// 播放音频

    /// - Parameter audioURL: URL

    public func playAudio(audioURL: URL) {

        do {

            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])

            try AVAudioSession.sharedInstance().setActive(true, options: [])

            if ((audioPlayer?.isPlaying) != nil) == true{

                audioPlayer?.stop()

            }

            audioPlayer = try AVAudioPlayer(contentsOf: audioURL)

            audioPlayer?.play()

        } catch {

            print("Failed to play audio file")

        }

    }

    /// 暂停

    public func pauseAudio() {

        audioPlayer?.pause()

    }

    /// 恢复

    public func resumeAudio() {

        audioPlayer?.play()

    }

    //

    public func getCurrentTime() -> TimeInterval? {

        return audioPlayer?.currentTime

    }

    public func getDuration() -> TimeInterval? {

        return audioPlayer?.duration

    }

}

extension FastAudioManager {

    /// 下载音频并播放 、、、、、、、

    /// - Parameter url: url网络地址

    func downloadAndPlay(form url:String){

        // 下载音频

//        FastNetWorking().download(url) { [weak self] fileUrl  in

//            self?.playAudio(audioURL: fileUrl)

//        } fai: { str in

//

//        }

    }

    func recordAndUplaod(to url:String){

//        if let fileUrl = self.startRecording(100){

//            // 上传音频

//            FastNetWorking().upload(url, fileUrl, nil) { str in

//

//            } fai: { fai in

//

//            }

//        }

    }

}