iOS Audio Trimming

前端 未结 4 1917
既然无缘
既然无缘 2020-11-28 05:08

I searched a lot and couldn\'t find anything relevant... I am working on iOS audio files and here is what I want to do...

  1. Record Audio and Save Clip (Checked,
4条回答
  •  爱一瞬间的悲伤
    2020-11-28 05:54

    import following two libraries in .m

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

    and after that paste following code you will be able to trim an audio file with the help of two thumbs.

    - (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);
               }
           }];
    
    }
    

提交回复
热议问题