2

最初にレコーダーを起動する必要があります。これは、カメラからのビデオも記録する必要がある間にオーディオを記録します。

オーディオ録音用コード

NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

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

    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];

    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
    [recorder record];

ビデオ録画用のコード

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [picker dismissViewControllerAnimated:YES completion:^
     {


         if(_isVideo)
         {
             NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
             _imageData = [NSData dataWithContentsOfURL:videoURL];
              [picker dismissViewControllerAnimated:YES completion:nil];
             [ self saveVideo];


         }
     }];




}



-(void)saveVideo
{




        if(_isVideo)
        {

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSDateFormatter *formatter;
            NSString        *dateString;
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
            dateString = [formatter stringFromDate:[NSDate date]];
            NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@/%@.mp4",_folderName,@"Videos",dateString]];
            NSError * error = nil;
            NSString *pathVideoThumb = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@/%@",_folderName,@"VideoThumb",dateString]];
            [_imageData writeToFile:path options:NSDataWritingAtomic error:&error];
            if (error != nil)
            {
                NSLog(@"Error: %@", error);
                return;
            }
                [self generateThumbImage:pathVideoThumb dataPath:path];
            dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];

                _isVideo=NO;
                videoCount++;
                [videoLabel setText:[NSString stringWithFormat:@"%d %@",videoCount,@"VIDEOS"]];
                [videoView setBackgroundColor:[UIColor clearColor]];

              });
            });




  }


}

しかし、録画したビデオを再生すると、音声が含まれていません。録音されたオーディオは正常に動作しています。オーディオ セッションに問題があることは理解しています。オーディオ セッションを使用してこれを処理する方法を教えてください。

ありがとう

4

0 に答える 0