代码之家  ›  专栏  ›  技术社区  ›  azamsharp

在iphone编程中录制和播放音频

  •  0
  • azamsharp  · 技术社区  · 14 年前

    我正在尝试从我的iphone模拟器录制音频,然后将其保存到目录中,然后回放。因为我听不到我录制的音频,我甚至不确定它是不是在第一时间录制。这是我的代码:

    -(IBAction) playRecording:sender
    {
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *dir = [paths objectAtIndex:0]; 
    
        NSFileManager *filemanager = [NSFileManager defaultManager]; 
    
        NSArray *files = [filemanager contentsOfDirectoryAtPath:dir error:nil]; 
    
        NSString *file = [files objectAtIndex:0]; 
    
        NSString *path = [dir stringByAppendingPathComponent:file]; 
    
        NSURL *url = [NSURL URLWithString:path]; // url is nil 
    
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    
        player.volume = 0.3; 
    
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    
        [player play]; 
    
    
    }
    
    
    -(IBAction) record:sender 
    {
        if(recorder.recording) 
        {
            [recorder stop]; 
    
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
    
            [recordButton setTitle:@"Record" forState:UIControlStateNormal]; 
    
        }
    
        else 
        {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
    
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
    
            NSString *dir = [paths objectAtIndex:0]; 
            NSString *filename = [[NSString alloc] initWithString:@"myfile.caf"]; 
    
            NSString *path = [dir stringByAppendingPathComponent:filename]; 
    
            NSMutableDictionary *settings = [[NSMutableDictionary alloc] init]; 
    
            [settings setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey]; 
    
            [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    
            [settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey]; 
    
            [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
    
            [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
    
            [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 
    
            [recorder release]; 
    
            recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path]
        settings:settings error:nil];
    
            [recorder prepareToRecord]; 
            recorder.meteringEnabled = YES; 
            [recorder record]; 
    
            [recordButton setTitle:@"Stop" forState:UIControlStateNormal]; 
    
        }
    }
    

    更新1:

    playrecording中的url变量的计算结果始终为零。

    2 回复  |  直到 14 年前
        1
  •  2
  •   azamsharp    14 年前

    问题是nsurl需要一个未提供的有效url。下面是一种修复nsurl stringwithurl喜欢的有效url的路径的方法:

    NSString *path = [dir stringByAppendingPathComponent:file]; 
    
    NSString *fixedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    
    NSURL *url = [NSURL URLWithString:fixedPath]; 
    
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    
    player.volume = 0.3; 
    
    [player play]; 
    
        2
  •  1
  •   Justin user327094    13 年前
    #import "RecorderController.h"
    
    #import <Foundation/Foundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    
    #define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
    
    @implementation RecorderController
    
    @synthesize _recordButton;
    @synthesize _recorder;
    
    - (void)awakeFromNib {
    
      NSError *err = nil;
    
      NSMutableDictionary *recordSetting;
      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];
    
      // create a new dated file
      NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
      NSString *caldate = [now description];
      NSString *recorderFilePath;
      //recorderFilePath = [[NSString stringWithFormat:@"%@/%@.wav", DOCUMENTS_FOLDER, caldate] retain];
        recorderFilePath = [[NSString stringWithFormat:@"%@/SeasonsGreetings.wav", DOCUMENTS_FOLDER, caldate] retain];
    
    
      NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
        NSLog(@"PATH: %@",url);
      err = nil;
      _recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    
      if(!_recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
      }
    
      // prepare to record
      [_recorder setDelegate:self];
      [_recorder prepareToRecord];
    
    }
    
    - (IBAction)recordButtonPressed:(UIButton *)sender {
      NSLog(@"Record button pressed");
    
      [_recorder record];
    }
    
    @end