捕获音频样本以将音频实时推送到服务器

问题描述 投票:0回答:1

我正在使用 LFLiveKit 仅从设备直播视频,并且运行良好。现在我想推送一个音频文件来一起播放。我们使用带有 rtmp 链接的 WOWZA 服务器来进行流式传输和播放。我使用的代码随机播放歌曲 10-15 秒,它还会挂起视频流一段时间。我在会话开始后推送音频。任何解决此问题的帮助将不胜感激。

如果有人想检查音频在库中的编码方式 LFLiveKit - https://github.com/LaiFengiOS/LFLiveKit

lazy var session: LFLiveSession = {
        let audioConfiguration = LFLiveAudioConfiguration.defaultConfiguration(for: .medium)
        audioConfiguration?.numberOfChannels = 1
        let videoConfiguration = LFLiveVideoConfiguration.defaultConfiguration(for: .high3)
        let session = LFLiveSession(audioConfiguration: audioConfiguration, videoConfiguration: videoConfiguration, captureType: .captureMaskVideoInputAudio)
        session?.captureDevicePosition = .back
        session?.delegate = self
        session?.preView = self.videView
        session?.showDebugInfo = true
        return session!
    }()

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if controller.documentPickerMode == .import{
            let firstURL = urls[0] //song path
            let songAsset = AVAsset(url: firstURL)
            loopAmplitudes(audioFileUrl: firstURL)
    }
}

func loopAmplitudes(audioFileUrl: URL) {
        let asset = AVAsset(url: audioFileUrl)
        let reader = try! AVAssetReader(asset: asset)
        let track = asset.tracks(withMediaType: AVMediaType.audio)[0]

        let settings = [
            AVFormatIDKey : kAudioFormatLinearPCM,
            AVNumberOfChannelsKey: 1,
            AVLinearPCMBitDepthKey: 16,
            AVSampleRateKey: track.naturalTimeScale,
            AVLinearPCMIsNonInterleaved: false,
            AVLinearPCMIsFloatKey: false,
            AVLinearPCMIsBigEndianKey: false,
        ] as [String : Any]
        
        let readerOutput = AVAssetReaderTrackOutput(track: track, outputSettings: settings)
        reader.add(readerOutput)
        reader.startReading()

        while let sampleBuffer = readerOutput.copyNextSampleBuffer() {
            
            var audioBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
            var blockBuffer: CMBlockBuffer?
            
            CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, bufferListSizeNeededOut: nil, bufferListOut: &audioBufferList, bufferListSize: MemoryLayout<AudioBufferList>.size, blockBufferAllocator: nil, blockBufferMemoryAllocator: nil, flags: kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, blockBufferOut: &blockBuffer)
            
            let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers))
            
            for audioBuffer in buffers {
                let audio = audioBuffer.mData!.assumingMemoryBound(to: UInt8.self)   //WORKING PARTIALLY
                let newdata = Data(bytes: audio, count: Int(audioBuffer.mDataByteSize))
                session.pushAudio(newdata)
            }
        }
    } 
ios swift iphone
1个回答
0
投票

我在通过 LFliveKit 库进行直播时遇到声音问题。声音似乎跑得太快了。您能帮我设置 LFLiveStream 库来直播视频吗?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.