Advertisement
markos-lacerda

Record and Play Audio Files iOS

Jan 13th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Note: Add AVFoundation framework to your project
  2.  
  3. #pragma mark - .h file
  4.  
  5. #import <AVFoundation/AVFoundation.h>
  6.  
  7. // Implement this delegate : AVAudioRecorderDelegate
  8.  
  9. AVAudioRecorder *recorder;
  10.  
  11. #pragma mark - .m file
  12.  
  13. // Configure audio recorder
  14. -(void)configureAudioRecorder {
  15.     NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  16.     NSString *documentPath_ = [searchPaths objectAtIndex: 0];
  17.    
  18.     // Don't change the file extension
  19.     NSString *soundFilePath = [documentPath_ stringByAppendingPathComponent:@"your_file_name_here.m4a"];
  20.    
  21.     NSURL *outputFileURL = [NSURL fileURLWithPath:soundFilePath];
  22.    
  23.     // Setup audio session
  24.     AVAudioSession *session = [AVAudioSession sharedInstance];
  25.     [session setCategory:AVAudioSessionCategoryRecord error:nil];
  26.     [session setActive:YES error:nil];
  27.    
  28.     [session requestRecordPermission:^(BOOL granted) {
  29.         if (granted) {
  30.             // Define the recorder setting
  31.             NSDictionary *recordSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
  32.                                              AVEncoderAudioQualityKey: @(AVAudioQualityLow),
  33.                                              AVNumberOfChannelsKey: @1,
  34.                                              AVSampleRateKey: @22050.0f};
  35.            
  36.             // Initiate and prepare the recorder
  37.             NSError *error;
  38.             recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSettings error:&error];
  39.            
  40.             if (!error) {
  41.                 recorder.delegate = self;
  42.                 [recorder prepareToRecord];
  43.                
  44.                 // Start recording
  45.                 [recorder record];
  46.                
  47.                 NSLog(@"Record Start");
  48.             } else {
  49.                 NSLog(@"%@", error.description);
  50.             }
  51.         } else {
  52.             NSLog(@"Permission to record audio denied");
  53.         }
  54.     }];
  55. }
  56.  
  57. // Call "configureAudioRecorder" in your record button
  58.  
  59. // In your "pause/stop" button call
  60. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  61. [audioSession setActive:NO error:nil];
  62.        
  63. [recorder stop];
  64.  
  65. // Implement the delegate method
  66. -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag {
  67.    // if flag param equals to YES your file is saved successfull in : avrecorder.url.absoluteString
  68. }
  69.  
  70. // ===================================================================================================== //
  71.  
  72. // To Play File Saved after Recording
  73. AVAudioSession *session = [AVAudioSession sharedInstance];
  74. [session setCategory:AVAudioSessionCategoryPlayback error:nil];
  75. [session setActive:YES error:nil];
  76.        
  77. NSURL *filePath = [NSURL URLWithString:@"path to your saved file. ex: avrecorder.url.absoluteString"];
  78.        
  79. AVPlayer *avPlayer = [AVPlayer playerWithURL:filePath];
  80.        
  81. AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
  82. controller.player = avPlayer;
  83.        
  84. [self presentViewController:controller animated:YES completion:^{
  85.     [avPlayer play];
  86. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement