使用 iOS 修剪音频 [英] Trim audio with iOS

查看:29
本文介绍了使用 iOS 修剪音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一项功能,让用户可以修剪他以前录制的音频文件 (.caf).录音部分已经可以使用,但是我如何添加类似于 Voicememos 应用程序中的修剪功能.苹果使用的音频修剪器有 api 吗?任何帮助都会很棒...

I want to implement a feature that lets the user trim an audio file (.caf) which he perviously recorded. The recording part already works, but how can i add a trimming feature similar to the one in the Voicememos app. Is there an api for the audio trimmer apple uses? Any help would be great...

推荐答案

如何使用 AVFoundation?将音频文件导入 AVAsset(组合等),然后您可以将其导出 - 设置首选时间 + 持续时间 - 到一个文件.

How about using the AVFoundation? Import the audio file into an AVAsset (composition etc), then you can export it - setting preferred time + duration - to a file.

我前段时间写了一个库存函数,可以将资产导出到文件,您还可以指定混音.如下所示,它导出了所有文件,但您可以向 exporter.timeRange 添加一个 NSTimeRange,然后就可以了.我还没有测试过,但应该可以工作(?).另一种选择是在创建 AVAsset + 轨道时调整时间范围.当然,出口商只处理 m4a (AAC).抱歉,如果这不是您想要的.

I wrote a stock function awhile ago that exports an asset to a file, you can also specify an audiomix. As below it exports all of the file, but you could add a NSTimeRange to exporter.timeRange and there you go. I haven't tested that though, but should work(?). Another alternative could be to adjust time ranges when creating the AVAsset + tracks. Of course the exporter only handles m4a (AAC). Sorry if this wasn't what you wanted.

-(void)exportAsset:(AVAsset*)asset toFile:(NSString*)filename overwrite:(BOOL)overwrite withMix:(AVAudioMix*)mix {
//NSArray* availablePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];

AVAssetExportSession* exporter = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];

if (exporter == nil) {
    DLog(@"Failed creating exporter!");
    return;
}

DLog(@"Created exporter! %@", exporter);

// Set output file type
DLog(@"Supported file types: %@", exporter.supportedFileTypes);
for (NSString* filetype in exporter.supportedFileTypes) {
    if ([filetype isEqualToString:AVFileTypeAppleM4A]) {
        exporter.outputFileType = AVFileTypeAppleM4A;
        break;
    }
}
if (exporter.outputFileType == nil) {
    DLog(@"Needed output file type not found? (%@)", AVFileTypeAppleM4A);
    return;
}

// Set outputURL
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* parentDir = [NSString stringWithFormat:@"%@/", [paths objectAtIndex:0]];

NSString* outPath = [NSString stringWithFormat:@"%@%@", parentDir, filename];

NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:outPath]) {
    DLog(@"%@ already exists!", outPath);
    if (!overwrite) {
        DLog(@"Not overwriting, uh oh!");
        return;
    }
    else {
        // Overwrite
        DLog(@"Overwrite! (delete first)");
        NSError* error = nil;
        if (![manager removeItemAtPath:outPath error:&error]) {
            DLog(@"Failed removing %@, error: %@", outPath, error.description);
            return;
        }
        else {
            DLog(@"Removed %@", outPath);
        }
    }
}

NSURL* const outUrl = [NSURL fileURLWithPath:outPath];
exporter.outputURL = outUrl;
// Specify a time range in case only part of file should be exported
//exporter.timeRange =

if (mix != nil)
    exporter.audioMix = mix; // important

DLog(@"Starting export! (%@)", exporter.outputURL);
[exporter exportAsynchronouslyWithCompletionHandler:^(void) {
    // Export ended for some reason. Check in status
    NSString* message;
    switch (exporter.status) {
        case AVAssetExportSessionStatusFailed:
            message = [NSString stringWithFormat:@"Export failed. Error: %@", exporter.error.description];
            DLog(@"%@", message);
            [self showAlert:message];
            break;
        case AVAssetExportSessionStatusCompleted: {
            /*if (playfileWhenExportFinished) {
             DLog(@"playfileWhenExportFinished!");
             [self playfileAfterExport:exporter.outputURL];
             playfileWhenExportFinished = NO;
             }*/
            message = [NSString stringWithFormat:@"Export completed: %@", filename];
            DLog(@"%@", message);
            [self showAlert:message];
            break;
        }
        case AVAssetExportSessionStatusCancelled:
            message = [NSString stringWithFormat:@"Export cancelled!"];
            DLog(@"%@", message);
            [self showAlert:message];
            break;
        default:
            DLog(@"Export unhandled status: %d", exporter.status);
            break;
    }       
}];
}

这篇关于使用 iOS 修剪音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆