• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 将音频文件保存在后台

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:41:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个应用程序可以在选择音高按钮时更改音频的音高,现在我使用 installTapOnBus 来保存文件,但是在我按下音高按钮后会调用此方法,因此只有部分音频已保存,我想保存整个音频无论何时选择音高按钮,有什么办法

此方法用于播放音频

-(void)playAudio
{
    NSError *err = nil;

    audioEngine = [[AVAudioEngine alloc] init];
    AudioFileplayer = [[AVAudioPlayerNode alloc] init];
    pitch = [[AVAudioUnitTimePitch alloc] init];
    reverb = [[AVAudioUnitReverb alloc] init];
    [audioEngine stop];
    [AudioFileplayer stop];
    [audioEngine reset];

    file = [[AVAudioFile alloc] initForReading:[NSURL URLWithString:[self filePath]] error:&err];


    [audioEngine attachNode:AudioFileplayer];

    [audioEngine attachNode:pitch];
    [audioEngine attachNode:reverb];

    [audioEngine connect:AudioFileplayer to:reverb format:nil];
    [audioEngine connect:reverb to:pitch format:nil];
    [audioEngine connect:pitch to:audioEngine.outputNode format:nil];
    [reverb loadFactoryPreset:AVAudioUnitReverbPresetLargeRoom2];

 [AudioFileplayer scheduleFile:file atTime:nil completionHandler:^{
        AudioFileplayer = nil;
    }];

    [audioEngine prepare];
    [audioEngine startAndReturnError:&err];

    if (err != nil) {
        NSLog(@"An error occured");
    }

    [AudioFileplayer play];



}

以及这种保存音高影响音频的方法

-(void)saveEffectedAudioToFolder
{
NSError *error;
if (audioEngine) {


     pitchEffect.pitch = 1000;

    AVAudioFormat  * commonFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:44100 channels:2 interleaved:NO];

    //
    AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:[NSURL URLWithString:[self filePath1]] settings:commonFormat.settings error:&error];
    //


    if (error) {
        NSLog(@"Error is 1 %@",[error localizedDescription]);


    }
    [pitchEffect  installTapOnBus:0 bufferSize:4096 format:commonFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when)
     {

         if (outputFile.length < file.length)
         {//Let us know when to stop saving the file, otherwise saving infinitely
             NSError *error;

             NSAssert([outputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]);

         }else{
             audioEngine = nil;
             [pitchEffect removeTapOnBus:0];//if we dont remove it, will keep on tapping infinitely

         }

     }


     ];
}

}

任何解决方法都会有帮助



Best Answer-推荐答案


其实我们在Output Audio File 的设置中犯了错误。输出的音频文件处理格式应该和输入文件一样(你放的效果或音高)。

输出文件格式应为 wav 或 caf 格式。此格式仅保存到输出音频文件。

 - (IBAction)save_it_after_changesid)sender
    {

             engine = [[AVAudioEngine alloc] init];
             audio_player_node= [[AVAudioPlayerNode alloc] init];
            [engine attachNode:audio_player_node];
            [self setupEQ];

            AVAudioMixerNode *mixerNode = [engine mainMixerNode];
            [engine connect:audio_player_node to:unitEq format:audioFile.processingFormat];
            [engine connect:unitEq to:mixerNode format:audioFile.processingFormat];

            NSError *error12;
            [engine startAndReturnError:&error12];
            if (!error12)
            {
                  NSLog(@"Engine = %@",engine);
                 [audio_player_node scheduleFile:audioFile atTime:nil completionHandler:nil];
                 NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

                  [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
                  [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
                  [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

                  [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
                  [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
                  [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];


                  NSError *error;
                 outputFile = [[AVAudioFile alloc] initForWriting:[self testFilePathURL] settings:recordSetting error:&error];
                  NSLog(@"outputfile = %@",outputFile);
                  if (error)
                  {
                         NSLog(@"outputFile error = %@",error);
                  }
                  else
                  { //(AVAudioFrameCount)audioFile.length
                        [audio_player_node installTapOnBus:0 bufferSize:8192  format:audioFile.processingFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
                              NSLog(@"Buffer Size = %@",buffer);
                              NSLog(@"when = %lld",when.sampleTime);
                              NSLog(@"outputfile length = %lli",outputFile.length);
                              NSLog(@"input file length = %lld",audioFile.length);
                              if (outputFile.length<audioFile.length)
                              {
                                    NSError *error;
                                    [outputFile writeFromBuffer:buffer error:&error];
                                    if (error)
                                    {
                                          NSLog(@"writebuffererror =%@",error);
                                    }
                              }
                              else
                              {
                                    [audio_player_node removeTapOnBus:0];
                                     NSError *error2;
                                   // player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[self testFilePathURL] error:&error2];
                                    //player2.delegate = self;
                                    NSLog(@"athththt = %@",[self testFilePathURL]);
                                    NSLog(@"error = %@",error2);
                                   // [audio_player_node scheduleFileutputFile atTime:nil completionHandler:nil];
                                    //[audio_player_node play];
                                   // [self toMp3];
                              }

                        }];
                  }
            }
            else
            {
                  NSLog(@"error12 =%@",error12);
            }

    }





- (void)setupEQ
{
      NSLog(@"setupEQ");

      unitEq = [[AVAudioUnitEQ alloc] initWithNumberOfBands:12];
      unitEq.globalGain = 3.0;
      AVAudioUnitEQFilterParameters *filterParameters;
      filterParameters = unitEq.bands[0];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 31;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[1];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 63;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[2];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 125;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[3];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 250;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[4];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 500;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[5];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 1000;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[6];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 2000;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[7];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 4000;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain =-20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[8];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 8000;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain = -20;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[9];
      filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
      filterParameters.frequency = 16000;
      filterParameters.bandwidth = 1.0;
      filterParameters.gain =-20;
      filterParameters.bypass = FALSE;


      filterParameters = unitEq.bands[10];
      filterParameters.filterType = AVAudioUnitEQFilterTypeLowPass;
      filterParameters.frequency = 16857;
      filterParameters.bypass = FALSE;

      filterParameters = unitEq.bands[11];
      filterParameters.filterType = AVAudioUnitEQFilterTypeHighPass;
      filterParameters.frequency = 205;
      filterParameters.bypass = FALSE;
      [engine attachNode:unitEq];
}




- (NSString *)applicationDocumentsDirectory
{
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
      return basePath;
}

//------------------------------------------------------------------------------

- (NSURL *)testFilePathURL
{
      return [NSURL fileURLWithPath:[NSString stringWithFormat"%@/test.caf",
                                     [self applicationDocumentsDirectory]]];
}

保存成功后请播放文件。这个对我有用。看看吧。

请引用下面的链接,我从这里得到更多,Can I use AVAudioEngine to read from a file, process with an audio unit and write to a file, faster than real-time?

引用这个示例项目。这是我们正在寻找的 https://github.com/VladimirKravchenko/AVAudioEngineOfflineRender

关于ios - 将音频文件保存在后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660501/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap