3

我想在 iOS 中录制一个虚拟乐器,它可以创建缓冲区,以后可以流式传输。但为此我需要创建一个记录缓冲区。我有一台虚拟钢琴,可以在单击按钮时播放 mp3 文件。但是我真的不知道要在内部录制用户播放的曲目。我为此使用了麦克风录音,但它非常嘈杂并且没有提供干净的录音。车库乐队之类的应用程序有这样的功能。所以我不认为这是不可能的。谁能指导我完成这个查询?

我已经尝试过音频引擎,但示例代码由于某种原因不起作用。代码运行,但当我按下播放按钮时没有任何反应。不播放音频。

4

1 回答 1

0

添加

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

到你的项目

并将委托添加AVAudioRecorderDelegate到您的.h文件

@property (nonatomic, strong) AVAudioRecorder *Audiorecorder;

.m文件中(这里self.audioFileNameNSString

这是代码片段:

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];        
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

        //Now that we have our settings we are going to instanciate an instance of our recorder instance.
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd_MM_yyyy_HH_mm_ss"];
        self.audioFileName = [NSString stringWithFormat:@"%@.caf", [formatter stringFromDate:[NSDate date]]];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:self.audioFileName ];
        NSLog(@"%@",path);
        NSURL *recordedTmpFile = [[NSURL alloc] initFileURLWithPath:path];
        NSLog(@"%@",recordedTmpFile);

        //Setup the recorder to use this file and record to it.
        if(self.Audiorecorder != nil)
        {
            [self.Audiorecorder stop];
            self.Audiorecorder = nil;
        }

        NSError * error;
        self.Audiorecorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
        //NSLog(@"Error : %@",error);
        [self.Audiorecorder setDelegate:self];
        [self.Audiorecorder prepareToRecord];       

        //Start the actual Recording
        [self.Audiorecorder peakPowerForChannel:8];
        [self.Audiorecorder updateMeters];
        self.Audiorecorder.meteringEnabled = YES;
        [self.Audiorecorder record];
于 2013-08-16T13:11:47.370 回答