正如在主题中一样,我想分析输出信号的缓冲区。我已使用此函数( InstallTapOnBus )来获取麦克风信号,但不适用于输出。有人知道怎么做吗?
let bus = 0
let node = engine.outputNode
node.installTap(onBus: bus, bufferSize: AVAudioFrameCount(BUFFER_SIZE), format: node.outputFormat(forBus: bus), block: { (buffer : AVAudioPCMBuffer ,time : AVAudioTime) in
...
})
try! engine.start()
}
Run Code Online (Sandbox Code Playgroud)
它给我提供了一个错误:“所需条件为假:_isInput”
我有一副带麦克风输入的蓝牙耳机。不使用麦克风,但使用时,输入和输出都强制为 8000kHz。
我的 AVAudioEngine 实例以 8000kHz 模式连接到耳机,除非我进入系统设置并指定我不想使用耳机进行输入(每次连接耳机时都必须执行此操作)。
我注意到其他应用程序可以以预期的 44100kHz 播放,没有任何问题。我的 AVAudioEngine 图中没有输入节点。
如何使 AVAudioEngine 更喜欢以合理的采样率进行连接?
我用来AVAudioEngine播放音频文件和AVPlayer播放AVPlayerViewController视频。
和控制中心通知已启用UIApplication.shared.beginReceivingRemoteControlEvents()
AVPlayerViewController如果没有播放视频文件(如果不存在实例),控制中心可以完美地处理音频文件(播放/暂停/下一个/上一个/滑块)
但是一旦我播放视频文件然后关闭AVPlayerViewController并播放音频文件,控制中心中的所有控件都会被禁用。
刷新 nowPlayingInfo 不起作用 - 它只更新缩略图和音频标题
解雇 AVPlayerViewController 后
control-center avaudioengine avplayerviewcontroller swift3 ios10
假设我有一个持续时间为 10 秒的 AVAudioFile。我想将该文件加载到 AVAudioPCMBuffer 中,但我只想加载特定秒数/毫秒后或特定 AVAudioFramePosition 之后出现的音频帧。
它看起来不像 AVAudioFile 的readIntoBuffer方法给我那种精度,所以我假设我必须在 AVAudioBuffer 级别或更低级别工作?
我使用 AVAudioMixerNode 来更改音频格式。这篇文章对我帮助很大。下面的代码给了我我想要的数据。但我在电话扬声器中听到了自己的声音。我该如何预防?
func startAudioEngine()
{
engine = AVAudioEngine()
guard let engine = engine, let input = engine.inputNode else {
// @TODO: error out
return
}
let downMixer = AVAudioMixerNode()
//I think you the engine's I/O nodes are already attached to itself by default, so we attach only the downMixer here:
engine.attach(downMixer)
//You can tap the downMixer to intercept the audio and do something with it:
downMixer.installTap(onBus: 0, bufferSize: 2048, format: downMixer.outputFormat(forBus: 0), block: //originally 1024
{ (buffer: …Run Code Online (Sandbox Code Playgroud) 我想使用 iOS 在 iOS 上流式传输音频AVAudioEngine。目前我不知道该怎么做。
我从网络获取 RTP 数据并想要播放此音频数据AVAudioEngine。
我正在使用iOSNetwork.Framework接收网络数据。然后,我对语音数据进行解码,现在想使用 来播放它AVAudioEngine。
这是我的接收代码:
connection.receiveMessage { (data, context, isComplete, error) in
if isComplete {
// decode the raw network data with Audio codec G711/G722
let decodedData = AudioDecoder.decode(enc: data, frames: 160)
// create PCMBuffer for audio data for playback
let format = AVAudioFormat(standardFormatWithSampleRate: 8000, channels: 1)
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: 160)!
buffer.frameLength = buffer.frameCapacity
// TODO: now I have to copy the decodedData …Run Code Online (Sandbox Code Playgroud) 我已经配置了AVAudioSinkNodeAttach to ,AVAudioEngine如下inputNode所示:
let sinkNode = AVAudioSinkNode() { (timestamp, frames, audioBufferList) -> OSStatus in
print("SINK: \(timestamp.pointee.mHostTime) - \(frames) - \(audioBufferList.pointee.mNumberBuffers)")
return noErr
}
audioEngine.attach(sinkNode)
audioEngine.connect(audioEngine.inputNode, to: sinkNode, format: nil)
audioEngine.prepare()
do {
try audioEngine.start()
print("AudioEngine started.")
} catch {
print("AudioEngine did not start!")
}
Run Code Online (Sandbox Code Playgroud)
我已经单独将其配置为使用“内置麦克风”设备(我确信它确实使用了)。
如果我将麦克风的采样率设置为 44100(使用 Apple 在所有 Mac 上提供的“音频 MIDI 设置”应用程序),一切都会按预期工作:
AudioEngine started.
SINK: 692312319180567 - 512 - 2
SINK: 692312348024104 - 512 - 2
SINK: 692312359634082 - 512 - 2
SINK: …Run Code Online (Sandbox Code Playgroud) 我正在接收 Int16 形式的 16 位/48 kHz 立体声 PCM 样本流,并且我尝试使用 AVAudioEngine 播放它们,但是我根本听不到任何声音。我认为这要么与我设置播放器的方式有关,要么与我将数据推入缓冲区的方式有关。
我已经阅读了很多有关使用音频队列服务的替代解决方案的内容,但是我能找到的所有示例代码要么是 Objective-C 要么仅限 iOS。
如果我遇到任何类型的帧大小问题或其他问题,我难道不应该至少能听到扬声器中发出的垃圾声吗?
这是我的代码:
import Foundation
import AVFoundation
class VoicePlayer {
var engine: AVAudioEngine
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 48000.0, channels: 2, interleaved: true)!
let playerNode: AVAudioPlayerNode!
var audioSession: AVCaptureSession = AVCaptureSession()
init() {
self.audioSession = AVCaptureSession()
self.engine = AVAudioEngine()
self.playerNode = AVAudioPlayerNode()
self.engine.attach(self.playerNode)
//engine.connect(self.playerNode, to: engine.mainMixerNode, format:AVAudioFormat.init(standardFormatWithSampleRate: 48000, channels: 2))
/* If I set my custom format here, AVFoundation complains about the format not being …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AVAudioEngine播放AVAudioFile。该代码主要是从Apple Developer在线视频中获取和改编的,但是没有回放。已经花了一些时间浏览这些论坛,但是似乎没有任何启示。
我有两种方法。第一个调用标准打开文件对话框,打开音频文件,分配一个AVAudioBuffer对象(稍后将使用)并用音频数据填充它。第二个设置AVAudioEngine和AVAudioPlayerNode对象,连接所有内容并播放文件。下面列出了这两种方法。
- (IBAction)getSoundFileAudioData:(id)sender {
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
openPanel.title = @"Choose a .caf file";
openPanel.showsResizeIndicator = YES;
openPanel.showsHiddenFiles = NO;
openPanel.canChooseDirectories = NO;
openPanel.canCreateDirectories = YES;
openPanel.allowsMultipleSelection = NO;
openPanel.allowedFileTypes = @[@"caf"];
[openPanel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
NSURL* theAudioFileURL = [[openPanel URLs] objectAtIndex:0];
// Open the document.
theAudioFile = [[AVAudioFile alloc] initForReading:theAudioFileURL error:nil];
AVAudioFormat *format = theAudioFile.processingFormat;
AVAudioFrameCount capacity = (AVAudioFrameCount)theAudioFile.length;
theAudioBuffer = [[AVAudioPCMBuffer alloc]
initWithPCMFormat:format frameCapacity:capacity];
NSError *error;
if (![theAudioFile readIntoBuffer:theAudioBuffer error:&error]) { …Run Code Online (Sandbox Code Playgroud) 我将从我制作的一个简单的“操场”视图控制器类开始,该类演示了我的问题:
class AudioEnginePlaygroundViewController: UIViewController {
private var audioEngine: AVAudioEngine!
private var micTapped = false
override func viewDidLoad() {
super.viewDidLoad()
configureAudioSession()
audioEngine = AVAudioEngine()
}
@IBAction func toggleMicTap(_ sender: Any) {
guard let mic = audioEngine.inputNode else {
return
}
if micTapped {
mic.removeTap(onBus: 0)
micTapped = false
return
}
stopAudioPlayback()
let micFormat = mic.inputFormat(forBus: 0)
print("installing tap: \(micFormat.sampleRate) -- \(micFormat.channelCount)")
mic.installTap(onBus: 0, bufferSize: 2048, format: micFormat) { (buffer, when) in
print("in tap completion")
let sampleData = UnsafeBufferPointer(start: buffer.floatChannelData![0], count: Int(buffer.frameLength)) …Run Code Online (Sandbox Code Playgroud) avaudioengine ×10
ios ×6
swift ×4
audio ×3
avfoundation ×3
avaudiofile ×2
bluetooth ×1
core-audio ×1
ios10 ×1
macos ×1
objective-c ×1
pcm ×1
playback ×1
streaming ×1
swift3 ×1