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

推荐订阅源

L
LINUX DO - 热门话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Privacy International News Feed
D
Docker
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Jina AI
Jina AI
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
WordPress大学
WordPress大学
Project Zero
Project Zero
小众软件
小众软件
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
Lohrmann on Cybersecurity
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
Cloudbric
Cloudbric
罗磊的独立博客
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
IT之家
IT之家
A
Arctic Wolf
The Hacker News
The Hacker News
T
Tailwind CSS Blog
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
腾讯CDC
S
SegmentFault 最新的问题
P
Palo Alto Networks Blog
NISL@THU
NISL@THU

博客园 - 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

//

//            }

//        }

    }

}