1

我有一个要求,允许用户在提交到服务器之前修剪音频文件。修剪功能在 iOS 6 中运行良好,而在 iOS 7 中则不行。

这发生在 iOS 7 中,当用户从 iTunes 库中选择一首歌曲并开始修剪时。它显示为修剪。修剪后创建的新文件播放到修剪和休息将是空白的。持续时间也显示原始歌曲的持续时间。并非所有文件都会发生这种情况。它仅发生在某些文件中。我也检查了 exportable 和 hasProtectedContent 。两者都有正确的值(可导出 - 是,hasProtectedContent - 否)。我能知道 iOS 7 中可能出现的问题吗?

我正在粘贴修剪音频文件代码以供参考

  - (void)trimAndExportAudio:(AVAsset *)avAsset withDuration:(NSInteger)durationInSeconds withStartTime:(NSInteger)startTime endTime:(NSInteger)endTime toFileName:(NSString *)filename withTrimCompleteBlock:(TrimCompleteBlock)trimCompleteBlock
{
    if (startTime < 0 || startTime > durationInSeconds || startTime >= endTime)
    {
        CGLog(@"start time = %d endTime %d durationInSeconds %d", startTime, endTime, durationInSeconds);

        trimCompleteBlock(NO, @"Invalid Start Time");
        return;
    }
    if (endTime > durationInSeconds)
    {
        CGLog(@"start time = %d endTime %d durationInSeconds %d", startTime, endTime, durationInSeconds);

        trimCompleteBlock(NO, @"Invalid End Time");
        return;
    }

    // create the export session
    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:avAsset presetName:AVAssetExportPresetAppleM4A];
    if (exportSession == nil)
    {
        trimCompleteBlock(NO, @"Could not create an Export Session.");
        return;
    }

    //export file path
    NSError *removeError = nil;
    NSString *filePath = [[CGUtilities applicationLibraryMyRecordingsDirectory] stringByAppendingPathComponent:filename];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&removeError];
    }
    if (removeError)
    {
        CGLog(@"Error removing existing file = %@", removeError);
    }

    // create trim time range
    CMTime exportStartTime = CMTimeMake(startTime, 1);
    CMTime exportStopTime = CMTimeMake(endTime, 1);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(exportStartTime, exportStopTime);

    // configure export session  output with all our parameters
    exportSession.outputURL = [NSURL fileURLWithPath:filePath];     // output path
    exportSession.outputFileType = AVFileTypeAppleM4A;              // output file type
    exportSession.timeRange = exportTimeRange;                      // trim time range

    //perform the export
    __weak AVAssetExportSession *weakExportSession = exportSession;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (AVAssetExportSessionStatusCompleted == exportSession.status)
        {
            if (![filename isEqualToString:kLibraryTempFileName])
            {
                //created a new recording
            }

            trimCompleteBlock(YES, nil);
        }
        else if (AVAssetExportSessionStatusFailed == exportSession.status)
        {
            // a failure may happen because of an event out of your control
            // for example, an interruption like a phone call comming in
            // make sure and handle this case appropriately
            trimCompleteBlock(NO, weakExportSession.error.description);
        }
        else
        {
            trimCompleteBlock(NO, weakExportSession.error.description);
        }
    }];
}

谢谢

4

1 回答 1

4

我们可以导入 AVFoundation/AVFoundation.h

-(BOOL)trimAudiofile{

    float audioStartTime;//define start time of audio
    float audioEndTime;//define end time of audio
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryCachesDirectory = [paths objectAtIndex:0];
   libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];
    NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/output_%@.mp4", [dateFormatter stringFromDate:[NSDate date]]];
   NSURL *audioFileOutput = [NSURL fileURLWithPath:OutputFilePath];
   NSURL *audioFileInput;//<Path of orignal audio file>

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

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

   AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                        presetName:AVAssetExportPresetAppleM4A];
   if (exportSession == nil)
   {
       return NO;
   }
   CMTime startTime = CMTimeMake((int)(floor(audioStartTime * 100)), 100);
   CMTime stopTime = CMTimeMake((int)(ceil(audioEndTime * 100)), 100);
   CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

   exportSession.outputURL = audioFileOutput;
   exportSession.timeRange = exportTimeRange;
   exportSession.outputFileType = AVFileTypeAppleM4A;

   [exportSession exportAsynchronouslyWithCompletionHandler:^
   {
       if (AVAssetExportSessionStatusCompleted == exportSession.status)
       {
           NSLog(@"Export OK");
       }
       else if (AVAssetExportSessionStatusFailed == exportSession.status)
       {
           NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
       }
   }];
  return YES;
}
于 2014-02-25T12:10:07.013 回答