tumtumtum / StreamingKit

A fast and extensible gapless AudioPlayer/AudioStreamer for OSX and iOS (iPhone, iPad)
Other
2.42k stars 523 forks source link

Player cache issue #476

Open zjfMVC opened 3 months ago

zjfMVC commented 3 months ago

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 commented 3 months ago

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)")
                }
            }