2012-03-14 50 views
9

tôi đã tìm kiếm rất nhiều và không thể tìm thấy bất cứ điều gì có liên quan ... Tôi đang làm việc trên các tập tin âm thanh iOS và đây là những gì tôi muốn làm ...iOS âm thanh Cắt

  1. Ghi âm thanh và Lưu Clip (kiểm tra, tôi đã làm điều này bằng AVAudioRecorder)
  2. Thay đổi sân (kiểm tra, đã làm điều này bằng Dirac)
  3. Cắt :(

tôi có hai dấu hiệu tức là bắt đầu & chấm dứt bù đắp và sử dụng thông tin này Tôi muốn cắt tập tin đã ghi và muốn lưu lại. Tôi không muốn sử dụng "tìm kiếm" vì sau này tôi muốn phát tất cả các tệp được ghi đồng bộ (giống như các đoạn phim flash trong dòng thời gian) và cuối cùng tôi muốn xuất dưới dạng một tệp âm thanh.

+0

cảm ơn mattjgalloway để chỉnh sửa ... –

Trả lời

25

Đây là mã mà tôi đã sử dụng để cắt âm thanh từ tệp đã tồn tại từ trước. Bạn sẽ cần phải thay đổi hằng số liên quan M4A nếu bạn đã lưu hoặc đang lưu sang định dạng khác.

- (BOOL)trimAudio 
{ 
    float vocalStartMarker = <starting time>; 
    float vocalEndMarker = <ending time>; 

    NSURL *audioFileInput = <your pre-existing file>; 
    NSURL *audioFileOutput = <the file you want to create>; 

    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(vocalStartMarker * 100)), 100); 
    CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100); 
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

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

    [exportSession exportAsynchronouslyWithCompletionHandler:^ 
    { 
     if (AVAssetExportSessionStatusCompleted == exportSession.status) 
     { 
      // It worked! 
     } 
     else if (AVAssetExportSessionStatusFailed == exportSession.status) 
     { 
      // It failed... 
     } 
    }]; 

    return YES; 
} 

Ngoài ra còn có Technical Q&A 1730, cung cấp cách tiếp cận chi tiết hơn một chút.

+0

vocalStartMarker và vocalEndMarker nằm trong khoảng [0,1], phải không? –

+1

vocalStartMarker và vocalEndMarker chỉ mất vài giây. Ví dụ, cắt xén giữa giây thứ hai của clip 10 giây, bạn sẽ đặt vocalStartMarker = 4.5 và vocalEndMarker = 5.5. – kyleobrien

+0

Tôi nghĩ chúng ta chỉ sử dụng: CMTime startTime = CMTimeMake (vocalStartMarker, 1); –

2

Dưới đây là mã mẫu cắt tệp âm thanh bắt đầu từ & bù đắp kết thúc và lưu lại. Vui lòng kiểm tra điều này iOS Audio Trimming.

// Path of your source audio file 
NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"abc.mp3"]; 
NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; 

// Path of your destination save audio file 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *libraryCachesDirectory = [paths objectAtIndex:0]; 
    libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; 

NSString *strOutputFilePath = [NSString stringWithFormat:@"%@%@",libraryCachesDirectory,@"/abc.mp4"]; 
NSURL *audioFileOutput = [NSURL fileURLWithPath:strOutputFilePath]; 

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; 
} 
float startTrimTime = 0; 
float endTrimTime = 5; 

CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100); 
CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100); 
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

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

[exportSession exportAsynchronouslyWithCompletionHandler:^ 
{ 
    if (AVAssetExportSessionStatusCompleted == exportSession.status) 
    { 
     NSLog(@"Success!"); 
    } 
    else if (AVAssetExportSessionStatusFailed == exportSession.status) 
    { 
     NSLog(@"failed"); 
    } 
}]; 
2

nhập khẩu sau hai thư viện trong .m

#import "BJRangeSliderWithProgress.h" 
#import <AVFoundation/AVFoundation.h> 

và sau khi dán mà sau mã bạn sẽ có thể cắt một tập tin âm thanh với sự giúp đỡ của hai ngón tay cái.

- (void) viewDidLoad 
{ 
     [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

     mySlider = [[BJRangeSliderWithProgress alloc] initWithFrame:CGRectMake(20, 100, 300, 50)]; 

     [mySlider setDisplayMode:BJRSWPAudioSetTrimMode]; 

     [mySlider addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged]; 

     [mySlider setMinValue:0.0]; 

     NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"]; 

     NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; 

     audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audioFileInput error:nil]; 

     [mySlider setMaxValue:audioPlayer.duration]; 

     [self.view addSubview:mySlider]; 
} 

-(void)valueChanged { 
     NSLog(@"%f %f", mySlider.leftValue, mySlider.rightValue); 
} 


-(IBAction)playTheSong 
{ 
     // Path of your source audio file 
     NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"]; 
     NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; 

     // Path of your destination save audio file 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
     NSString *libraryCachesDirectory = [paths objectAtIndex:0]; 
     //libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; 

     NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.mov"]; 
     NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"]; 
     NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath]; 

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

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


     float startTrimTime = mySlider.leftValue; 
     float endTrimTime = mySlider.rightValue; 

     CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100); 
     CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100); 
     CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

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

     [exportSession exportAsynchronouslyWithCompletionHandler:^ 
     { 
      if (AVAssetExportSessionStatusCompleted == exportSession.status) 
      { 
       NSLog(@"Success!"); 
       NSLog(@" OUtput path is \n %@", requiredOutputPath); 
       NSFileManager * fm = [[NSFileManager alloc] init]; 
       [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil]; 
       //[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL]; 

       NSURL *url=[NSURL fileURLWithPath:requiredOutputPath]; 
       NSError *error; 
       audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; 
       audioPlayer.numberOfLoops=0; 
       [audioPlayer play]; 

      } 
      else if (AVAssetExportSessionStatusFailed == exportSession.status) 
      { 
       NSLog(@"failed with error: %@", exportSession.error.localizedDescription); 
      } 
     }]; 

} 
+0

Bạn có thể làm cho câu trả lời này tốt hơn bằng cách thêm một vài nhận xét về những gì đang diễn ra – arghtype

+0

sẽ có một thanh trượt trên màn hình và sẽ có hai ngón tay cái. với sự giúp đỡ của ngón tay cái, bạn sẽ có thể cắt âm thanh (.m4a). @iwantMyAnswerNow –

Các vấn đề liên quan