0

AVFoundation を使用してビデオ ファイルを記録し、カメラ ロールに保存しています。しかし同時に、単純なテキスト フィールドにこれらのビデオの長さを表示したい.. アイデアはありますか?

//私は2つのメソッドとして分離しましたが、カムを停止するためにまだ0秒を得ています

- (IBAction)startCamera:(id)sender {

if (!recording)
{
    //----- START RECORDING -----
    NSLog(@"START RECORDING");
    recording = YES;


 //Create temporary URL to record to
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:outputPath])
    {
    }
    //Start recording
    [MovieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];     

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
    CMTime videoDuration = videoAsset.duration;
    float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

    NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];  //we can vary the date string. Ex: "mm:ss"
    NSString* result = [dateFormatter stringFromDate:date];
    NSLog(@"Duration 1 of this after starting : %2@",result);

//カメラメソッド停止

-(void)stopCamera:(id)sender {

//----- STOP RECORDING -----
    NSLog(@"STOP RECORDING");
    recording = NO;
    [MovieFileOutput stopRecording];

//Create temporary URL to record to
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
    CMTime videoDuration = videoAsset.duration;
    float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

    NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];  //we can vary the date string. Ex: "mm:ss"
    NSString* result = [dateFormatter stringFromDate:date];
    NSLog(@"Duration 2 of this after stopping : %2@",result);
}
}
4

2 に答える 2

0

録音を開始するときにタイマーを開始します...

- (IBAction)startCamera:(id)sender {

if (!recording)
{
      //----- START RECORDING -----
      NSLog(@"START RECORDING");
      recording = YES;

 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                              target:self
                                            selector:@selector(getCountInSec:)
                                            userInfo:nil
                                             repeats:YES];


     //Create temporary URL to record to

else {
  //----- STOP RECORDING -----
NSLog(@"STOP RECORDING");
recording = NO;

if ([_timer isValid]) {
    [_timer invalidate];
}
_timer = nil;
[MovieFileOutput stopRecording];

  }
 }

//ここにタイマーメソッドがあります..

-(void)getCountInSec
 {   
  // set a global variable of int type . every time increment by one & manage as you need (Sec ,min or hours). 

     yourlbl.text = [NSString stringWithFormate@"%ld",yourVarable];

}

お役に立てば幸いです。

于 2015-10-26T09:41:42.483 に答える
0
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"HH:mm:ss"];  //you can vary the date string. Ex: "mm:ss"
NSString* result = [dateFormatter stringFromDate:date];

プロジェクトで CoreMedia と AVFoundation フレームワークをリンクする必要があります

于 2015-10-26T09:36:21.110 に答える