音频不保存在长篇视频Ios上

时间:2015-11-04 09:33:44

标签: ios iphone xcode camera avfoundation

嘿,我正在使用创建简单的视频录制应用程序,我正在录制和保存不同名称的相册中的视频。 但我面临的问题是,当我们保存20秒的小视频时,它的工作正常。 但如果我们尝试保存60秒视频,则录制视频但不录制音频 请查看我的代码,并在我出错的地方纠正我

/**
 * @SWG\Swagger(
 *     schemes={"http","https"},
 *     host="api.host.com",
 *     basePath="/",
 *     @SWG\Info(
 *         version="1.0.0",
 *         title="This is my website cool API",
 *         description="Api description...",
 *         termsOfService="",
 *         @SWG\Contact(
 *             email="contact@mysite.com"
 *         ),
 *         @SWG\License(
 *             name="Private License",
 *             url="URL to the license"
 *         )
 *     ),
 *     @SWG\ExternalDocumentation(
 *         description="Find out more about my website",
 *         url="http..."
 *     )
 * )
 */

class SwaggerController extends...

{

- (void)viewDidLoad {
[super viewDidLoad];

videoCount = 0 ;


 [self switchCameraTapped:nil];

// Alloc Player



//---------------------------------
//----- SETUP CAPTURE SESSION -----
//---------------------------------
NSLog(@"Setting up capture session");
CaptureSession = [[AVCaptureSession alloc] init];

//----- ADD INPUTS -----
NSLog(@"Adding video input");

//ADD VIDEO INPUT
AVCaptureDevice *VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (VideoDevice)
{
    NSError *error;
    VideoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
    if (!error)
    {
        if ([CaptureSession canAddInput:VideoInputDevice])
            [CaptureSession addInput:VideoInputDevice];
        else
            NSLog(@"Couldn't add video input");
    }
    else
    {
        NSLog(@"Couldn't create video input");
    }
}
else
{
    NSLog(@"Couldn't create video capture device");
}



//ADD AUDIO INPUT
NSLog(@"Adding audio input");
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput)
{
    [CaptureSession addInput:audioInput];
}


//----- ADD OUTPUTS -----

//ADD VIDEO PREVIEW LAYER
NSLog(@"Adding video preview layer");
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:CaptureSession] ];

// PreviewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;      //<<SET ORIENTATION.  You can deliberatly set this wrong to flip the image and may actually need to set it wrong to get the right image

[[self PreviewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];


//ADD MOVIE FILE OUTPUT
NSLog(@"Adding movie file output");
MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

Float64 TotalSeconds = 60;          //Total seconds
int32_t preferredTimeScale = 30;    //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);   //<<SET MAX DURATION
MovieFileOutput.maxRecordedDuration = maxDuration;

MovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;                        //<<SET MIN FREE SPACE IN BYTES FOR RECORDING TO CONTINUE ON A VOLUME

if ([CaptureSession canAddOutput:MovieFileOutput])
    [CaptureSession addOutput:MovieFileOutput];

//SET THE CONNECTION PROPERTIES (output properties)
// [self CameraSetOutputProperties];            //(We call a method as it also has to be done after changing camera)



//----- SET THE IMAGE QUALITY / RESOLUTION -----
//Options:
//  AVCaptureSessionPresetHigh - Highest recording quality (varies per device)
//  AVCaptureSessionPresetMedium - Suitable for WiFi sharing (actual values may change)
//  AVCaptureSessionPresetLow - Suitable for 3G sharing (actual values may change)
//  AVCaptureSessionPreset640x480 - 640x480 VGA (check its supported before setting it)
//  AVCaptureSessionPreset1280x720 - 1280x720 720p HD (check its supported before setting it)
//  AVCaptureSessionPresetPhoto - Full photo resolution (not supported for video output)
NSLog(@"Setting image quality");
[CaptureSession setSessionPreset:AVCaptureSessionPresetMedium];
if ([CaptureSession canSetSessionPreset:AVCaptureSessionPreset640x480])     //Check size based configs are supported before setting them
    [CaptureSession setSessionPreset:AVCaptureSessionPreset640x480];



//----- DISPLAY THE PREVIEW LAYER -----
//Display it full screen under out view controller existing controls
NSLog(@"Display the preview layer");
CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:layerRect];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                      CGRectGetMidY(layerRect))];
//[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
//We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):

UIView *CameraView = [[UIView alloc] init] ;
[[self view] addSubview:CameraView];
[self.view sendSubviewToBack:CameraView];

[[CameraView layer] addSublayer:PreviewLayer];

//----- START THE CAPTURE SESSION RUNNING -----
[CaptureSession startRunning];
// Do any additional setup after loading the view.

[self performSelector:@selector(StartFunctionality) withObject:nil afterDelay:valSequenceDelay];
// [self orientationChanged];

AVCaptureConnection *videoConnection = nil;

for ( AVCaptureConnection *connection in [MovieFileOutput connections] )
{
    NSLog(@"%@", connection);
    for ( AVCaptureInputPort *port in [connection inputPorts] )
    {
        NSLog(@"%@", port);
        if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
        }
    }
}

if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
    [videoConnection setVideoOrientation:[self deviceOrientationDidChange]];
}

NSLog(@"didFinishRecordingToOutputFileAtURL - success");
NSString *albumName=strSessionName;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:albumName
                         resultBlock:^(ALAssetsGroup *group) {
                             NSLog(@"added album:%@", albumName);

                         }
                        failureBlock:^(NSError *error) {
                            NSLog(@"error adding album");
                        }];


}



#pragma mark- DID FINISH RECORDING TO OUTPUT FILE AT URL
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
  fromConnections:(NSArray *)connections
            error:(NSError *)error

}

0 个答案:

没有答案