问题
I have 4 AKPlayer
nodes and each one is connected to some effects and finally they are mixed together.
I want to render offline the output for iOS > 9.0 but I can't figure out how.
edit : I have implemented the render and separated it for iOS >11 While iOS>11 renderToFile seems to do well, but for iOS <11 the rendered file has some lags and jumps forward at some seconds, resulting silent in the end.
here is my render function :
do{
if #available(iOS 11, *) {
let outputFile = try AKAudioFile(forWriting: url, settings: [:])
_ = AudioKit.engine.isRunning
try AudioKit.renderToFile(outputFile, duration: karaPlayer.duration, prerender: {
self.seekTo(time: 0)
})
}else {
let offlineNode = AKOfflineRenderNode(self.mixer)
AudioKit.output = offlineNode
offlineNode.internalRenderEnabled = false
try AudioKit.start()
self.seekTo(time: 0)
try offlineNode.renderToURL(url, duration: self.karaPlayer.duration)
self.karaPlayer.stop()
self.voicePlayer.stop()
offlineNode.internalRenderEnabled = true
}
} catch {
print(error)
print("Couldn't render output file")
}
mixer's inputs are 2 AKPlayer Nodes that read from a ".caf" file.
回答1:
If you want to use iOS11+ you can use AudioKit's render to file:
/// Render output to an AVAudioFile for a duration.
/// - Parameters
/// - audioFile: An file initialized for writing
/// - seconds: Duration to render
/// - prerender: A closure called before rendering starts, use this to start players, set initial parameters, etc...
///
@available(iOS 11.0, macOS 10.13, tvOS 11.0, *)
public func renderToFile(_ audioFile: AVAudioFile, seconds: Double, prerender: (() -> Void)? = nil) throws {
If you need support for iOS9+ use AKOfflineRenderNode's render to URL function:
@available(iOS, obsoleted: 11) @available(tvOS, obsoleted: 11) @available(macOS, obsoleted: 10.13) open class AKOfflineRenderNode: AKNode, AKComponent, AKInput {
public typealias AKAudioUnitType = AKOfflineRenderAudioUnit
public static let ComponentDescription = AudioComponentDescription(effect: "mnrn")
private var internalAU: AKAudioUnitType?
open var internalRenderEnabled: Bool {
get { return internalAU!.internalRenderEnabled }
set { internalAU!.internalRenderEnabled = newValue }
}
open func renderToURL(_ url: URL, seconds: Double, settings: [String: Any]? = nil) throws {
return try internalAU!.render(toFile: url, seconds: seconds, settings: settings)
}
来源:https://stackoverflow.com/questions/50238045/offline-audio-render-with-audiokit-for-ios-11