很抱歉 之前的贴子我很早之前弄了就没有用简书了,自己也在后2年中先后跑去搞了安卓和java后台,2020年下半年接到任务又重新用我不熟悉的swift开发新的iOS项目,把很多有用的放在了csdn上面,很久没有更新了,感觉自己学的广,心里更感觉到了无力,最后还是决定好好重新研究iOS 和最基础的计算机知识,我也会在以后的时间中,填写自己心的体会与书友们分享,回归重点,语音播报是我很早之前弄过的,发帖在这些,给那些需要的人。
在这位老铁的博客https://www.jianshu.com/p/e075125603ee里进行了试验和尝试,
自己也进行了修改和改善,这里我说一下改善的方案
这里拆分成3部分,head:支付渠道的开头信息
content:包含具体的整数金额
footer:包含小数点后面的金额+"元"
一。改善音频文件生成方法,这里也是采用百度语音合成,使用python进行语音合成,前面的head,content部分和footer 合成的方法都一样,我这里采用了百度语音sdk 更简单一点,这里就说一下比较小数点后面的,这块相对处理比较费劲。
import os
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipSpeech(APP_ID,API_KEY,SECRET_KEY)
def functionNum(num):
if(num == 0):
return "零"
elif(num == 1):
return "一"
elif (num == 2):
return "二"
elif (num == 3):
return "三"
elif (num == 4):
return "四"
elif (num == 5):
return "五"
elif (num == 6):
return "六"
elif (num == 7):
return "七"
elif (num == 8):
return "八"
elif (num == 9):
return "九"
for i in range(0, 100):
# text = "收到"
# sound = ""
# if (i == 0):
# text = text +"支付宝"
# sound = "alipay"
# elif(i==1):
# text = text + "微信"
# sound = "wxpay"
print(os.getcwd())
filePath = '/Users/"你的用户名"/Desktop/yuyin/'
text = "点"+functionNum(int(i/10))+str(i%10)+"元"
result = client.synthesis(text,'zh',1,None)
sound = "point"+str(i).zfill(2)
if not isinstance(result,dict):
with open(filePath+sound+'.mp3','wb') as f:
f.write(result)
print(sound)
二.生成好的文件放入resource中,在pushservice 通过[UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3",string]]调用废话不多说直接上我改进后的干货
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSError* err = nil;
[[BDSSpeechSynthesizer sharedInstance] speakSentence:self.bestAttemptContent.body withError:&err];
if (err != nil) {
NSLog(@"error");
}
NSMutableArray *array = [NSMutableArray arrayWithArray:[self stringToArray:self.bestAttemptContent.body]] ;
//为空不做任何操作
if(array == nil){
self.contentHandler(self.bestAttemptContent);
return
}
for (NSString *string in array) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self registerNotificationWithString:string completeHandler:^{
//延迟大于1秒感觉更好一点
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_semaphore_signal(semaphore);
});
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
self.contentHandler(self.bestAttemptContent);
}
//这里处理有一点不同 其他和博主基本一样
- (NSArray *)stringToArray:(NSString *)string {
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:50];
NSString *headStr = @"";
if([string containsString:@"支付宝"]){
headStr = @"alipay";
}else if([string containsString:@"微信"]){
headStr = @"wxpay";
}else{
return nil;
}
[mutableArray addObject:headStr];
NSArray *speraStr = [string componentsSeparatedByString:@"【"];
NSString *priceStr;
if (speraStr.count > 1) {
priceStr = [speraStr[1] componentsSeparatedByString:@"】"][0];
}else{
return nil;
}
NSArray *showStr = [priceStr componentsSeparatedByString:@"."];
if (showStr.count >1) {
NSString *contentStr = showStr[0];
NSString *footerStr = [NSString stringWithFormat:@"point%@",showStr[1]];
[mutableArray addObject:contentStr];
[mutableArray addObject:footerStr];
}else{
return nil;
}
return mutableArray;
}
- (void)registerNotificationWithString:(NSString *)string completeHandler:(dispatch_block_t)complete {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = @"";
content.subtitle = @"";
content.body = @"";
content.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3",string]];
content.categoryIdentifier = [NSString stringWithFormat:@"categoryIndentifier%@",string];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"categoryIndentifier%@",string] content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error == nil) {
if (complete) {
complete();
}
}
}];
}
}];
}
这里我的推送语音内容是这样组织的 xxxapp收到微信(支付宝 )【xxx.xx】元
本人亲测效果还行吧,目前也只能这样搞了
这里是我之前csdn的地址 https://blog.csdn.net/javaskyhr/article/details/88844145
最后想说的,绕了一圈自己发现很多的不足,希望在2021年能坚持,认真的学习总结,打好基础
1.数据结构
2.算法
3.设计模式
4.iOS深入研究
5.java我会向着额外技能进行扩展培养
2021年我们一起加油