Question: When I trim audio from start to end time, it trim from different time


When I trim audio from start to end time, it trim from different time

Answers 1
Added at 2017-01-02 07:01

I want to trim audio from start to end thumb but after trim it is trimmed from different point. How can I solve this?

- (BOOL)trimAudio
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString *currentTime1 = [dateFormatter stringFromDate:today];
    currentTime1 = [currentTime1 stringByReplacingOccurrencesOfString:@" PM" withString:@""];
    currentTime1 = [currentTime1 stringByAppendingString:@".m4a"];
    NSFileManager *fileManager;
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(),currentTime1];
    NSURL *movieURL1 = [[NSURL alloc] initFileURLWithPath:outputPath];
    fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:outputPath])
        NSError *error;
        [fileManager removeItemAtPath:outputPath error:&error];
    NSURL *audioFileInput = songURL;
    NSURL *audioFileOutput = movieURL1;

    if (!audioFileInput || !audioFileOutput)
        return NO;

    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
    AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

    NSLog(@"START**,**END %f , %f",startTime,endTime);
    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset

    if (exportSession == nil)
        return NO;

    CMTime startTime_ = CMTimeMake(stickerRangeStartTime, 1);//2.49  87
    CMTime stopTime = CMTimeMake(stickerRangeStopTime, 1);

    NSLog(@"START,END %lld , %lld",startTime_.value,stopTime.value);

    CMTimeRange exportTimeRange = CMTimeRangeMake(startTime_,stopTime);

    exportSession.outputURL = audioFileOutput;
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = exportTimeRange;
    NSLog(@"Audio Exporting.....");
    [exportSession exportAsynchronouslyWithCompletionHandler:^
         if (AVAssetExportSessionStatusCompleted == exportSession.status)
             // It worked!
             [audioPlayer stop];
             [DEFAULT setObject:songNameString forKey:SONG_NAME];
             [DEFAULT setURL:audioFileOutput forKey: MUSIC_URL];
             [DEFAULT synchronize];

             AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:[DEFAULT URLForKey:MUSIC_URL] options:nil];
             CMTime audioDuration = audioAsset.duration;
             float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
             NSLog(@"Duration - %f",audioDurationSeconds);
                 [timerForSlider invalidate];
             dispatch_async(dispatch_get_main_queue(), ^
                                NSLog(@" Go BACK");

                                [self performSelector:@selector(openRecordVideoView) withObject:nil afterDelay:0.4];
                                //[self showFilterView];

           //  [self playAudio:audioFileOutput];
            // [self playAudio:songURL];
             NSLog(@"Audio Exporting.....Completed");

         else if (AVAssetExportSessionStatusFailed == exportSession.status)
             // It failed...

    return YES;

I also tried iOS Audio Trimming But not working.

Answers to

When I trim audio from start to end time, it trim from different time

nr: #1 dodano: 2017-01-02 10:01

I have used RETrimControl pod. (

Below is some lines of code for how to set some variables:

Write below lines of code after you initialize a trim control.

 self.trimControl.leftValue = 0
 self.trimControl.rightValue = duration
 self.trimControl._maxValue = duration
 self.audioStopTime = duration

The method of triming seems okay.

Hope it will help you.

Let me know if you face any issue.

Source Show
◀ Wstecz