gpt4 book ai didi

iOS StreamingKit 如何录制/保存音频?

转载 作者:行者123 更新时间:2023-11-29 11:54:42 26 4
gpt4 key购买 nike

我正在使用 https://github.com/tumtumtum/StreamingKit

用于流式传输实时 url。工作得很好。我想在我的应用程序中添加录音/保存音频功能。有谁知道这个图书馆是否可以做到这一点?如果没有,是否有其他选择?请注意,我需要录制实时流音频,而不是本地文件/静态 url。

该页面显示您可以在播放之前拦截 PCM 数据:

[audioPlayer appendFrameFilterWithName:@"MyCustomFilter" block:^(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UInt32 frameCount, void* frames)
{
...
}];

但是,我不确定如何将其转换为实际的录音/mp3 文件,甚至无法从中截取实际数据?

最佳答案

您可以这样做,尽管 StreamingKit 似乎对它提供给您的样本的格式有点保密。采样率是多少? float 还是整数?我想你可以从样本量中猜出。此示例假定 16 位整数。

NSURL *dstUrl = [[NSURL fileURLWithPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] ] URLByAppendingPathComponent:@"output.m4a"];

NSLog(@"write to %@", dstUrl);

__block AVAudioFile *audioFile = nil;

[audioPlayer appendFrameFilterWithName:@"MyCustomFilter" block:^(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UInt32 frameCount, void* frames)
{
NSError *error;

// what's the sample rate? StreamingKit doesn't seem to tell us
double sampleRate = 44100;

if (!audioFile) {
NSDictionary *settings =
@{
AVFormatIDKey : @(kAudioFormatMPEG4AAC),
AVSampleRateKey : @(sampleRate),
AVNumberOfChannelsKey : @(channelsPerFrame),
};

// need commonFormat?
audioFile = [[AVAudioFile alloc] initForWriting:dstUrl settings:settings commonFormat:AVAudioPCMFormatInt16 interleaved:YES error:&error];
if (!audioFile) {
// error
}
}

AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:sampleRate channels:channelsPerFrame interleaved:YES];
AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:frameCount];

buffer.frameLength = frameCount;
memmove(buffer.int16ChannelData[0], frames, frameCount*bytesPerFrame);

if (![audioFile writeFromBuffer:buffer error:&error]) {
NSLog(@"write error: %@", error);
}
}];

[self.audioPlayer performSelector:@selector(removeFrameFilterWithName:) withObject:@"MyCustomFilter" afterDelay:10];

关于iOS StreamingKit 如何录制/保存音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com