我将创建应用程序,我将在其中录制带音频的视频然后播放。
为此,我在我的代码中使用 AVCaptureManager。
在这里,如果视频大约为 10 秒,则音频和视频都在播放,并且工作正常。
但是如果我录制的视频超过 10 秒,则只有视频无声播放(没有声音播放)。
这是我的代码。
- (void)startRecording
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/GeneralInstruction"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSDate *time = [NSDate date];
NSDateFormatter* df = [NSDateFormatter new];
[df setDateFormat:@"ddMMyyyyhhmmss"];
NSString *timeString = [df stringFromDate:time];
NSString *myUniqueNames = [NSString stringWithFormat:@"%@", timeString];
NSString *myUniqueName = [NSString stringWithFormat:@"file://%@/%@.mp4",dataPath, myUniqueNames];
[self.fileOutput startRecordingToOutputFileURL:[NSURL URLWithString:myUniqueName] recordingDelegate:self];
}
之后,当我完成录制时,它来了......
#pragma mark - AVCaptureFileOutputRecordingDelegate
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didStartRecordingToOutputFileAtURL:(NSURL *)fileURL
fromConnections:(NSArray *)connections
{
_isRecording = YES;
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections error:(NSError *)error
{// [self saveRecordedFile:outputFileURL];
_isRecording = NO;
if ([self.delegate respondsToSelector:@selector(didFinishRecordingToOutputFileAtURL:error:)]) {
[self.delegate didFinishRecordingToOutputFileAtURL:outputFileURL error:error];
}
}