0

在我的应用程序的一个视图中,有一个按钮。按下它应该开始拍摄视频,触发声音文件开始,并在取消隐藏另一个按钮时隐藏自己。第二个按钮应该停止视频录制并保存。这是我用于视频录制的代码,最初可以正常工作:

在 viewDidLoad 中:

finishButton.hidden = TRUE;

session = [[AVCaptureSession alloc] init];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];


NSError *error;

AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self cameraWithPosition:AVCaptureDevicePositionFront] error:&error];


if (videoInput)
{
    [session addInput:videoInput];
}

AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *audioError = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&audioError];

if (audioInput)
{
    [session addInput:audioInput];
}


Float64 TotalSeconds = 35;          //Total seconds
int32_t preferredTimeScale = 30;    //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
movieFileOutput.maxRecordedDuration = maxDuration;

movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;

if ([session canAddOutput:movieFileOutput])
    [session addOutput:movieFileOutput];

[session setSessionPreset:AVCaptureSessionPresetMedium];
if ([session canSetSessionPreset:AVCaptureSessionPreset640x480])        //Check size based configs are supported before setting them
    [session setSessionPreset:AVCaptureSessionPreset640x480];

[self cameraSetOutputProperties];


[session startRunning];

对于按钮:

-(IBAction)start:(id)sender
{
startButton.hidden = TRUE;
finishButton.hidden = FALSE;


//Create temporary URL to record to
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
self.outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
    NSError *error;
    if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
    {
        //Error - handle if required
    }
}
//Start recording
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

最后,在最后一个按钮下:

[movieFileOutput stopRecording];

这是保存视频的代码:

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
  fromConnections:(NSArray *)connections
            error:(NSError *)error
{

NSLog(@"didFinishRecordingToOutputFileAtURL - enter");

BOOL RecordedSuccessfully = YES;
if ([error code] != noErr)
{
    // A problem occurred: Find out if the recording was successful.
    id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
    if (value)
    {
        RecordedSuccessfully = [value boolValue];
    }
}
if (RecordedSuccessfully)
{
    //----- RECORDED SUCESSFULLY -----
    NSLog(@"didFinishRecordingToOutputFileAtURL - success");
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:outputURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)
         {
             if (error)
             {

             }
         }];
    }

}
}

所有这一切都工作得很好。然后我添加了几行,以便在按下开始按钮时播放歌曲文件。

在 viewDidLoad 中:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Song.aiff", [[NSBundle mainBundle] resourcePath]]];

NSError *audioFileError;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&audioFileError];
player.numberOfLoops = 0;
[self.player prepareToPlay];

在开始按钮下:

if (player == nil)
NSLog(@"Audio file could not be played");
else
[player play];

现在,当按下开始按钮时,歌曲播放没有问题,但视频捕获却搞砸了。在添加 AVAudioPlayer 内容之前,当我按下完成按钮时,我会得到“didFinishRecordingToOutputFileAtURL - enter”和“didFinishRecordingToOutputFileAtURL - success”日志,现在我一按下开始按钮就会得到第一个日志,当我按下完成按钮,并且不录制视频。如果我注释掉使歌曲播放的行,那么视频捕获再次正常工作。有什么想法吗?

4

1 回答 1

2
- (void)setupAudioSession 
{
    static BOOL audioSessionSetup = NO;

    if (audioSessionSetup)
    {
        return;   
    }

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;

    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);

    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    audioSessionSetup = YES;
}

- (void)playAudio
{
    [self setupAudioSession];

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"btnClick" ofType:@"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    [fileURL release];

    self.audioPlayer = newPlayer;
    [newPlayer release];

    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    audioPlayer.volume=1.0;
    [audioPlayer play];
}

注意:添加框架:AudioToolbox.framework。

#import <AudioToolbox/AudioServices.h>
于 2012-08-03T04:59:53.067 回答