【问题标题】:iOS StreamingKit how to record/save the audio?iOS StreamingKit 如何录制/保存音频?
【发布时间】:2016-09-21 22:15:23
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: ios iphone audio streaming avplayer


    【解决方案1】:

    你可以做这样的事情,尽管 StreamingKit 似乎对它提供给你的样本格式有点保密。采样率是多少?浮点数还是整数?我想你可以从样本量中猜到。此示例假定 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];
    

    【讨论】:

    • 谢谢,明天早上试试,然后报告!查看streamingkit的源代码,我看到两个可能定义采样率的地方:initialize中的.mSampleRate = 44100.00,createMixerUnit中的Float64 graphSampleRate = 44100.0;
    • 附注我不认为 iOS 公开了 mp3 编码器。如果 StreamingKit 允许,您最好直接保存 mp3 数据包。
    • 你是个天才先生!!!非常感谢,效果很好!如果你有什么地方我可以捐赠一点啤酒钱,请分享!节省了我很多时间!目前m4a格式已经足够好了,我只是需要某种方式来录制音频,格式并不重要。
    • 44100 采样率也很完美。如果我使用其他任何东西,它会在执行ExtAudioFileWrite时写入错误代码-66567
    • 哈,我应该在我的博客上添加一个捐赠按钮。附言什么采样率? AAC 不支持任意采样率,但它确实支持您想使用的大多数采样率。您在 AVAudioFormat 和设置字典中都设置它,对吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2016-03-25
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多