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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - super1250

技术支持网址 (URL) 填写的地址 通过Homebrew安装CocoaPods iOS游戏内购(记录官网链接) iOS的证书创建 Apple开发者账号注册 __swift_FORCE_LOAD_$_swiftCompatibility50 Validation failed (409) 在 Pod 库中同时使用 Swift 和 Objective-C - super1250 拒审原因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

//

//            }

//        }

    }

}