使用RETrimControl可以轻易地实现一个音频剪辑的界面,而且支持自定义界面外观。
但想要真正实现音频分割的功能,还需要使用AudioTrim,代码如下:
Demo下载:https://github.com/lx271896700/audioTrim
// 音频剪切
- (void)trimTheMusic {
CMTime startTime = CMTimeMake(20, 1);
CMTime stopTime = CMTimeMake(30, 1);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
NSString *targetPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kaibulekou" ofType:@"mp3"];
AudioTrim *audioTrim = [[AudioTrim alloc] init];
[audioTrim trimAudio:filePath toFilePath:targetPath startTime:startTime stopTime:stopTime];
}
// 播放原始音乐
- (void)playTheOriginalMusic {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kaibulekou" ofType:@"mp3"];
self.player = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:filePath]];
[self.player play];
}
//播放裁剪后的音乐
- (void)playTheNewMusic {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
NSString *targetPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"];
NSLog(@"%@",targetPath);
self.player = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:targetPath]];
[self.player play];
}