最近项目中,需要将音频文件进行合并,在开发中,遇到了一些坑,在这里将这些问题整理记录一下。
- 将若干.mp3文件合并,可以MP3文件转成NSData,然后再进行拼接处理,这样就可以合成一段完整的MP3文件。
#import "ViewController.h"
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
#define MUSIC [kCachesPath stringByAppendingPathComponent:@"Music"]
#define COMBINPATH [MUSIC stringByAppendingPathComponent:@"combine.mp3"]
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:MUSIC]) {
[fileManager createDirectoryAtPath:MUSIC withIntermediateDirectories:YES attributes:nil error:nil];
}
//音频文件路径
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"];
NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"mp3"];
//音频数据
NSData *sound1Data = [[NSData alloc] initWithContentsOfFile: path1];
NSData *sound2Data = [[NSData alloc] initWithContentsOfFile: path2];
NSData *sound