Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player cache issue #476

Open
zjfMVC opened this issue May 11, 2024 · 1 comment
Open

Player cache issue #476

zjfMVC opened this issue May 11, 2024 · 1 comment

Comments

@zjfMVC
Copy link

zjfMVC commented May 11, 2024

Hello, the project I am currently developing uses this excellent open-source library. I have a question to know if this library supports local caching. For example, can I replay the previously played audio after playing first hand music and then disconnecting from the network?

@iDevelopper
Copy link

No, but you can record in a file, which I do like this:

            audioPlayer.addFrameFilter(withName: "RecordFilter", afterFilterWithName: "CustomEQFilter") { (channelsPerFrame, bytesPerFrame, frameCount, frames) in
                
                let sampleRate: Double = 44100

                if audioFile == nil {
                    let settings = [
                        AVFormatIDKey: self.formatIDKey, // kAudioFormatLinearPCM
                        AVSampleRateKey: sampleRate,
                        AVNumberOfChannelsKey: channelsPerFrame,
                    ] as [String : Any]
                    
                    do {
                        audioFile = try AVAudioFile(forWriting: outputUrl, settings: settings, commonFormat: .pcmFormatInt16, interleaved: true)
                    } catch {
                        let error = error as NSError
                        print("Error: \(error.localizedDescription)")
                    }
                }
                
                let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: sampleRate, channels: channelsPerFrame, interleaved: true)
                let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: frameCount)
                
                buffer?.frameLength = frameCount
                memmove(buffer?.int16ChannelData?[0], frames, Int(frameCount * bytesPerFrame))

                do {
                    if let buffer = buffer {
                        try audioFile?.write(from: buffer)
                    }
                } catch let error as NSError {
                    print("Error: \(error)")
                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants