0

私のアプリのビューの 1 つにボタンがあります。押すと、ビデオの撮影が開始され、サウンドファイルが開始され、別のボタンが表示されている間、ビューから非表示になります。2 番目のボタンは、ビデオ録画を停止して保存することになっています。ビデオ録画用のコードは次のとおりです。最初は問題なく動作しました。

ビューでDidLoad:

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)
             {

             }
         }];
    }

}
}

これらすべてがうまく機能していました。次に、スタート ボタンが押されたときに曲ファイルが再生されるように、数行を追加しました。

ビューでDidLoad:

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 に答える