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

推荐订阅源

Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Full Disclosure
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
Hacker News: Ask HN
Hacker News: Ask HN
G
Google Developers Blog
H
Heimdal Security Blog
O
OpenAI News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
IT之家
IT之家
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Know Your Adversary
Know Your Adversary
博客园 - 聂微东
The Cloudflare Blog
C
Check Point Blog
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Tor Project blog
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
小众软件
小众软件
Cloudbric
Cloudbric
A
Arctic Wolf

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

//

//            }

//        }

    }

}