IOS开发,实现铃声制作功能。
在IOS端,要设置铃声都是通过库乐队来制作的。
先看一下库乐队中铃声的文件结构。下面是弄的一个示例的文件,文件信息如下:
我们右击文件,点击显示包内容如下:
能看到一个aiff格式的音频。思考一下,当我们想要制作成铃声的音频文件是mp3或者是其他格式的音频是,发现,当隔空分享到库乐队时并没有库乐队选项。那是因为库乐队需要识别的是.band文件。
要是我们将音频转为aiff文件,然后塞入到这个band文件里面的Media。然后将这个band文件分享给库乐队就可以了呢?答案是:是的。下面我们开始操作!
一、实现流程
1、将没有音频文件的band文件导入项目中。也就是删掉aiff音频,剩下保存下来。 如下:
我把删掉音频后的band文件改了名字而已。
2、将bandfolder.band文件复制一份到沙盒中
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dir = [array objectAtIndex:0];
NSString *cacheDirectory = dir;NSString* bandfolder = [cacheDirectory stringByAppendingPathComponent:@"bandFolder"];
if(![[NSFileManager defaultManager] fileExistsAtPath:bandfolder]) {BOOL success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"bandfolder.band" ofType:nil] toPath:bandfolder error:NULL];
}
3、 创建一个分享的存放文件的文件夹
NSString* bandfolderDirectory = [cacheDirectory stringByAppendingPathComponent:@"bandfolderDirectory"];BOOL isDirectory = FALSE;
NSFileManager* fileMgr = [NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:bandfolderDirectory isDirectory:&isDirectory]){[fileMgr createDirectoryAtPath:bandfolderDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
4、拷贝一份bandFolder, 将第二步中沙盒的band文件拷贝多一份。然后把你自己的音频转码为aiff
//先拷贝一个bandFolder
NSURL *fileURL = @"换成你的音频URL";
NSString *fileName=[[filePath lastPathComponent] stringByDeletingPathExtension];NSString *copyAtPath = bandfolder;
NSString *copyToPath = [bandfolderDirectorystringByAppendingPathComponent:[NSString stringWithFormat:@"%@.band",fileName]];
[[NSFileManager defaultManager] copyItemAtPath:copyAtPath toPath:copyToPath error:NULL];//然后要把你自己的音频转码为aiff
ExtAudioConverter* converter = [[ExtAudioConverter alloc] init];
converter.inputFile = filePath;
converter.outputFile = [NSString stringWithFormat:@"%@/Media/ringtone.aiff",copyToPath];
converter.outputFileType = kAudioFileAIFFType;
[converter convert];
ExtAudioConverter类github上面搜一下有,附一个别的大佬的链接
GitHub - Agisight/ExtAudioConverter: iso + simulator
5、最后一步:将band文件分享给库乐队就行了
//弹出分享框并进行分享
NSArray *items = [NSArray arrayWithObjects:[NSURL fileURLWithPath:copyToPath],nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAirDrop];// 分享之后的回调
activityViewController.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {if (completed) {} else if(activityError){}
};
[self presentViewController:activityViewController animated:YES completion:^{}];
二、完整代码
//1.将项目中的bandFolder.band复制一份到沙盒
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dir = [array objectAtIndex:0];
NSString *cacheDirectory = dir;NSString* bandfolder = [cacheDirectory stringByAppendingPathComponent:@"bandFolder"];
if(![[NSFileManager defaultManager] fileExistsAtPath:bandfolder]) {BOOL success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"bandfolder.band" ofType:nil] toPath:bandfolder error:NULL];
}//2.创建一个后续分享band文件存放其的文件夹
NSString* bandfolderDirectory = [cacheDirectory stringByAppendingPathComponent:@"bandfolderDirectory"];BOOL isDirectory = FALSE;
NSFileManager* fileMgr = [NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:bandfolderDirectory isDirectory:&isDirectory]){[fileMgr createDirectoryAtPath:bandfolderDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}//3.拷贝一份band文件,变将你的音频转换为aiff格式。注意:转换的输出路径对应band文件里面的音频路径
//3.1先拷贝一个bandFolder
NSURL *fileURL = @"换成你的音频URL";
NSString *fileName=[[filePath lastPathComponent] stringByDeletingPathExtension];NSString *copyAtPath = bandfolder;
NSString *copyToPath = [bandfolderDirectorystringByAppendingPathComponent:[NSString stringWithFormat:@"%@.band",fileName]];
[[NSFileManager defaultManager] copyItemAtPath:copyAtPath toPath:copyToPath error:NULL];//3.2然后要把你自己的音频转码为aiff
ExtAudioConverter* converter = [[ExtAudioConverter alloc] init];
converter.inputFile = filePath;
converter.outputFile = [NSString stringWithFormat:@"%@/Media/ringtone.aiff",copyToPath];
converter.outputFileType = kAudioFileAIFFType;
[converter convert];//4.最后一步,分享到库乐队
NSArray *items = [NSArray arrayWithObjects:[NSURL fileURLWithPath:copyToPath],nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAirDrop];// 分享之后的回调
activityViewController.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {if (completed) {} else if(activityError){}
};
[self presentViewController:activityViewController animated:YES completion:^{}];
注意点:就是苹果设置铃声最长只能30s,如果你的音频超过30s,在被自动截取为前30s的音频.建议是先自行裁剪为30s,再将其转为aiff格式,塞入到.band包,再传给库乐队