iOS音乐的截取-创新互联

最近接触一个音频的项目,之前接触过,不过都是用系统自带的去实现,没有仔细研究

创新互联公司科技有限公司专业互联网基础服务商,为您提供成都电信服务器托管高防物理服务器租用,成都IDC机房托管,成都主机托管等互联网服务。

在网上找到了一个demo,挺好用,反正效果是实现,非常不错。具体链接忘记了。

#pragma mark - 音视频剪辑,如果是视频把下面的类型换为AVFileTypeAppleM4V

// assetURL 本地路径地址

//startTime 开始时间

//endTime 结束时间

//outputPath 裁剪完成后的地址

+ (void)cutAudioVideoResourcePath:(NSURL *)assetURL startTime:(CGFloat)startTime endTime:(CGFloat)endTime complition:(void (^)(NSURL *outputPath,BOOL isSucceed)) completionHandle{

  //  素材

  AVAsset *asset = [AVAsset assetWithURL:assetURL];

  //  导出素材

  AVAssetExportSession *exporter = [[AVAssetExportSessionalloc]initWithAsset:assetpresetName:AVAssetExportPresetAppleM4A];

  //剪辑(设置导出的时间段)

  CMTime start =CMTimeMakeWithSeconds(startTime, asset.duration.timescale);

  CMTime duration = CMTimeMakeWithSeconds(endTime - startTime,asset.duration.timescale);

  exporter.timeRange = CMTimeRangeMake(start, duration);

//  输出路径

  NSURL *outputPath = [self exporterPath];

  exporter.outputURL = [self exporterPath];

//  输出格式

  exporter.outputFileType =AVFileTypeAppleM4A;

  exporter.shouldOptimizeForNetworkUse=YES;

//  合成后的回调

  [exporterexportAsynchronouslyWithCompletionHandler:^{

    switch ([exporter status]) {

      caseAVAssetExportSessionStatusFailed: {

        NSLog(@"合成失败:%@",[[exporter error] description]);

        completionHandle(outputPath,NO);

      } break;

      caseAVAssetExportSessionStatusCancelled: {

        completionHandle(outputPath,NO);

      } break;

      caseAVAssetExportSessionStatusCompleted: {

        completionHandle(outputPath,YES);

      } break;

      default: {

        completionHandle(outputPath,NO);

      } break;

    }

  }];

}

#pragma mark - 输出路径

+ (NSURL *)exporterPath {

  NSInteger nowInter = (long)[[NSDatedate]timeIntervalSince1970];

  NSString *fileName = [NSString stringWithFormat:@"output%ld.mp4",(long)nowInter];

  NSString *documentsDirectory =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObject;

  NSString *outputFilePath =[documentsDirectory stringByAppendingPathComponent:fileName];

  if([[NSFileManagerdefaultManager]fileExistsAtPath:outputFilePath]){

    [[NSFileManagerdefaultManager]removeItemAtPath:outputFilePatherror:nil];

  }

  return [NSURL fileURLWithPath:outputFilePath];

}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:iOS音乐的截取-创新互联
文章源于:http://pwwzsj.com/article/dpgddo.html