【问题标题】:AVAudioFile from memory?来自内存的 AVAudioFile?
【发布时间】:2016-01-25 08:30:55
【问题描述】:

我在玩 AVAudioFile 和 AVAudioPlayerNode。

我已经实现了一个自定义流协议,它接收音频块并处理所有缓冲部分,但是我无法在不使用文件系统的情况下播放第一个块(我不想在这个时候使用 FS时间)。

这是:当我将数据写入临时文件并使用AVAudioFile 加载它时,它工作正常(然后我初始化AVAudioBuffer 并使用AVAudioFile.readIntoBuffer,最后使用AVAudioPlayerNode.scheduleBuffer)。但我无法直接从第一个块(NSData)加载缓冲区。

我应该实现自定义的 NSURLProtocol 并尝试从自定义的 NSURL 初始化 AVAudioFile 吗?

func play( data: NSData ) {
var audioFile: AVAudioFile
var audioBuffer: AVAudioPCMBuffer
// writing the data
let destPath = NSTemporaryDirectory() + "chunk.mp3"
data.writeToFile( destPath, atomically: true )
// initializing the AVAudioFile
audioFile = AVAudioFile( forReading: NSURL(string: destPath)!)
// initializing the AVAudioPCMBuffer
audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFile.processingFormat,
                               frameCapacity: UInt32(data.length))
audioFile.readIntoBuffer( audioBuffer )
/*
    I didn't put any AVAudioPlayerNode/AVAudioEngine in this code to keep it simple and describe the situation,
    anyway this works fine when everything's properly initialized:
*/
player.scheduleBuffer( audioBuffer, completionHandler: nil )
}

【问题讨论】:

    标签: ios objective-c swift avfoundation avaudioengine


    【解决方案1】:

    来自documentation

    要播放流式音频内容(例如来自网络连接),请将音频文件流服务与音频队列服务结合使用。音频文件流服务从网络比特流中的常见音频文件容器格式解析音频数据包和元数据。您还可以使用它来解析磁盘文件中的数据包和元数据。

    这里的关键点是使用音频文件服务将数据放入缓冲区并使用音频队列服务将其排入队列。网络部分通常使用CFNetwork API 完成,它比NSURLProtocol 低得多。

    Apple 示例代码“AudioFileStreamExample”说明了流式传输音频的客户端和服务器实现。这将是一个有效的起点。

    【讨论】:

    • 谢谢,我去看看!
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多