zoukankan      html  css  js  c++  java
  • IOS 音频合成

    - (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition start:(CMTime)start dura:(CMTime)dura offset:(CMTime)offset{
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];  
        
        AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        
        NSError *error = nil;
        BOOL ok = NO;
        
        CMTime startTime = start;
        CMTime trackDuration = dura;
        CMTimeRange tRange = CMTimeRangeMake(startTime, trackDuration);
        
        //Set Volume
        AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
        [trackMix setVolume:0.8f atTime:startTime];
        [audioMixParams addObject:trackMix];
        
        //Insert audio into track  //offset CMTimeMake(0, 44100)
        ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:offset error:&error];
    }
    - (void) exportAudio {
        
        AVMutableComposition *composition = [AVMutableComposition composition];
        audioMixParams = [[NSMutableArray alloc] initWithObjects:nil];
        
        //Add Audio Tracks to Composition
        NSString *path = [[NSBundle mainBundle] pathForResource:@"recordedFile" ofType:@"caf"];
        NSURL *assetURL1 = [NSURL fileURLWithPath:path];
        
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL1 options:nil];  
        CMTime startTime = CMTimeMakeWithSeconds(0, 1);
        CMTime trackDuration = songAsset.duration;
        
        [self setUpAndAddAudioAtPath:assetURL1 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(14*44100, 44100)];
        
        path = [[NSBundle mainBundle] pathForResource:@"1286" ofType:@"mp3"];
        NSURL *assetURL2 = [NSURL fileURLWithPath:path];
        [self setUpAndAddAudioAtPath:assetURL2 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(0, 44100)];
        
        AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
        audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams];
        
        //If you need to query what formats you can export to, here's a way to find out
        NSLog (@"compatible presets for songAsset: %@",
               [AVAssetExportSession exportPresetsCompatibleWithAsset:composition]);
        
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
                                          initWithAsset: composition
                                          presetName: AVAssetExportPresetAppleM4A];
        exporter.audioMix = audioMix;
        exporter.outputFileType = @"com.apple.m4a-audio";
        NSString *fileName = @"someFilename";
        NSString *exportFile = [NSHomeDirectory() stringByAppendingFormat: @"/%@.m4a", fileName];    
        
        // set up export
        if ([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) {
            [[NSFileManager defaultManager] removeItemAtPath:exportFile error:nil];
        }
        NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
        exporter.outputURL = exportURL;
        
        // do the export
        [exporter exportAsynchronouslyWithCompletionHandler:^{
            int exportStatus = exporter.status;
            switch (exportStatus) {
                case AVAssetExportSessionStatusFailed:{
                    NSError *exportError = exporter.error;
                    NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                    break;
                }
                    
                case AVAssetExportSessionStatusCompleted: NSLog (@"AVAssetExportSessionStatusCompleted"); break;
                case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break;
                case AVAssetExportSessionStatusExporting: NSLog (@"AVAssetExportSessionStatusExporting"); break;
                case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break;
                case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break;
                default:  NSLog (@"didn't get export status"); break;
            }
        }];
        
    //    // start up the export progress bar
    //    progressView.hidden = NO;
    //    progressView.progress = 0.0;
    //    [NSTimer scheduledTimerWithTimeInterval:0.1
    //                                     target:self
    //                                   selector:@selector (updateExportProgress:)
    //                                   userInfo:exporter
    //                                    repeats:YES];
        
    }

    转自

  • 相关阅读:
    什么是接口测试?怎样做接口测试?
    python下批量执行多条py文件的方法
    Jmeter运行报错software caused connection abort:recv failed
    性能测试一般容易出现瓶颈点
    性能测试流程规范(较好文档)
    Jmeter代理录制获取登录参数_移动端设置代理
    Http请求与WebSocket请求区别(WebSocket协议简析)
    JSONObject方法提取响应数据中的值
    Jmeter学习资料、控件下载地址大全
    图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
  • 原文地址:https://www.cnblogs.com/gaoxiao228/p/2942847.html
Copyright © 2011-2022 走看看