iOS开发笔记-138: swift-iOS15后,推送商家收款语音播报

先添加UNNotificationServiceExtension。File – new – targrt – UNNotificationServiceExtension

项目和UNNotificationServiceExtension,都要添加appGroups,GroupIdentifier要一致,一般是”group.自己项目的BundleIdentifier”,不一致会有问题。
添加appGroups的缘由是:共享一个group后,可以合成播报语音,然后存到group中,之后在UNNotificationServiceExtension中可以读取到合成后的语音文件。

UNNotificationServiceExtension的Minimum Deployment最低版本号要改一下,默认是最高iOS系统版本。会出现低版本真机调试不可用的情况

网上的实现方式有3种,我用的是第一种。
关于音频的拼接,如果用Data数据流的方式拼接,会出现只有第一段音频声音的问题,所有还是要用AVAssetExportSession来拼接。
1种是修改系统的通知声音,可以实现效果,但是由于音量不能调节,声音太小。如果对音量没有要求的,可以用这个方案,而且体验更好。

//自定义通知声音
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        let keyS = bestAttemptContent?.userInfo["key"] as? String
        if keyS == "sound" {
            let valueS = bestAttemptContent?.userInfo["value"] as? String//金额
            let formatter = NumberFormatter()
            formatter.locale = Locale(identifier: "zh-CN")
            formatter.allowsFloats = true
            let strF: CGFloat = formatter.number(from: valueS ?? "0") as? CGFloat ?? 0.0
            formatter.numberStyle = . spellOut
            let resultS: String = formatter.string(from: NSNumber (value: strF)) ?? ""
            let resultSA: Array<String> = resultS.map { String($0) }
            var finalA: Array<String> = ["钱响", "哥小兔到账"]
            finalA.append(contentsOf: resultSA)
            finalA.append("元")
            mergeAVAsset(with: finalA) { (name,path)  in
                if let bestAttemptContent = self.bestAttemptContent {
                    let temp3 = UNNotificationSoundName(rawValue: name ?? "")
                    let temp4 = UNNotificationSound.init(named: temp3)
                    bestAttemptContent.sound = temp4
                    contentHandler(bestAttemptContent)
                }
            }
        } else {
            if let bestAttemptContent = self.bestAttemptContent {
                contentHandler(bestAttemptContent)
            }
        }
    }

第2种是:AVAudioPlayer播放语音,瑕疵点就是,推送横幅要在语音播报结束后出现,不然就会和AVAudioPlayer语音播报冲突。(功能实现了,但是打包上传有问题,这个当打包上传到Appstore上就会出现错误了,提示说UNNotificationServiceExtension中的UIBackgroundModes的Audio这个字段是非法的,如果是企业分发模式可以添加。要上架审核的话,是无法添加的)所以这个方法只适合企业分发用,上架的话暂时还是用上面的第1方法。

UNNotificationServiceExtension 要添加一下权限,否则AVAudioPlayer不能在UNNotificationServiceExtension中播放

iOS开发笔记-138: swift-iOS15后,推送商家收款语音播报

import UserNotifications
import MediaPlayer

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    var audioPlayer: AVAudioPlayer?
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        let keyS = bestAttemptContent?.userInfo["type"] as? Int
        if keyS == 2 {
            let valueS = bestAttemptContent?.userInfo["amount"] as? String//金额
            let formatter = NumberFormatter()
            formatter.locale = Locale(identifier: "zh-CN")
            formatter.allowsFloats = true
            let strF: CGFloat = formatter.number(from: valueS ?? "0") as? CGFloat ?? 0.0
            formatter.numberStyle = . spellOut
            let resultS: String = formatter.string(from: NSNumber (value: strF)) ?? ""
            let resultSA: Array<String> = resultS.map { String($0) }
            var finalA: Array<String> = ["钱响", "哥小兔到账"]
            finalA.append(contentsOf: resultSA)
            finalA.append("元")
            mergeAVAsset(with: finalA) { (name,path)  in
//                NSLog("合并后的音频路径: (path)")
                let audioSession = AVAudioSession.sharedInstance()
                do {//配置音频,可以在后台、静音情况下播放
                    try audioSession.setCategory(.playback, mode:.default, options: [.mixWithOthers])
                    try audioSession.setActive(true)
                } catch {
                    print("Error configuring audio session: (error)")
                }
                self.audioPlayer = try? AVAudioPlayer(contentsOf: path ?? URL(fileURLWithPath: ""))
                self.audioPlayer?.delegate = self
                self.audioPlayer?.play()
            }
        } else {
            if let bestAttemptContent = self.bestAttemptContent {
                // Modify the notification content here...
//                    bestAttemptContent.title = "(bestAttemptContent.title) [modified]"
                contentHandler(bestAttemptContent)
            }
        }
    }
     
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
    //合成要播报的语音
    func mergeAVAsset(with sourcePathArr: [String], rate: Double = 1.0, completed: @escaping (_ soundName: String?,_ soundsFileURL: URL?) -> Void) {
        // 创建音频轨道, 并获取多个音频素材的轨道
        let composition = AVMutableComposition()
        // 音频插入的开始时间, 用于记录每次添加音频文件的开始时间
        var beginTime = CMTime.zero
        for audioFilePath in sourcePathArr {
            // 获取音频素材
            let audioFileURL: String = Bundle.main.path(forResource: audioFilePath, ofType: "mp3") ?? ""
            guard let audioAsset = AVURLAsset(url: URL(fileURLWithPath: audioFileURL)) as AVAsset? else { continue }
            // 音频轨道
            let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
            // 获取音频素材轨道
            guard let audioAssetTrack = audioAsset.tracks(withMediaType: .audio).first else { continue }
            // 音频合并 - 插入音轨文件
            try? audioTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: audioAsset.duration), of: audioAssetTrack, at: beginTime)
            // 记录尾部时间
            beginTime = CMTimeAdd(beginTime, audioAsset.duration)
        }
        let finalTimeValue = composition.duration.value * Int64(10 * rate)
        let finalTimeScale = composition.duration.timescale * 10
        composition.scaleTimeRange(.init(start: .zero, end: composition.duration), toDuration: .init(value: finalTimeValue, timescale: finalTimeScale))
        
        let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.自己项目的BundleIdentifier")
        //建立文件夹
        let soundsURL = groupURL?.appendingPathComponent("Library/", isDirectory: true)
        let soundsURL2 = groupURL?.appendingPathComponent("Library/Sounds/", isDirectory: true)
        do {
            //建立文件夹
            if !FileManager.default.fileExists(atPath: (soundsURL?.path ?? "")) {
                try FileManager.default.createDirectory(atPath: (soundsURL?.path ?? ""), withIntermediateDirectories: true, attributes: nil)
            }
            //建立文件夹
            if !FileManager.default.fileExists(atPath: (soundsURL2?.path ?? "")) {
                try FileManager.default.createDirectory(atPath: (soundsURL2?.path ?? ""), withIntermediateDirectories: true, attributes: nil)
            }
        } catch {
            print("Error 建立文件夹: (error)")
        }
        // 新建文件名,如果存在就删除旧的
        let soundName = "sound.m4a"
        let outPutFilePath = "Library/Sounds/" + soundName
        let soundsFileURL = groupURL?.appendingPathComponent(outPutFilePath, isDirectory: false)
        
        if FileManager.default.fileExists(atPath: (soundsFileURL?.path ?? "")) {
            do {
                try FileManager.default.removeItem(atPath:(soundsFileURL?.path ?? ""))
            } catch {
                print("Error 新建文件名,如果存在就删除旧的: (error)")
            }
        }
        // 导出合并后的音频文件
        guard let session = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) else { completed(nil, nil); return }
        // 音频文件输出
        session.outputURL = soundsFileURL
        session.outputFileType = AVFileType.m4a // 与上述的`preset`相对应
        session.shouldOptimizeForNetworkUse = true // 优化网络
        //            session.audioMix = videoAudioMixTools
        session.exportAsynchronously {
            if session.status == .completed {
//                print("合并成功 ----(soundsFileURL)")
                completed(soundName, soundsFileURL)
            } else {
                // 其他情况, 具体请看这里`AVAssetExportSessionStatus`.
//                print("合并失败 ----(session.status.rawValue)")
//                print("合并失败 ----(session.error)")
                completed(nil, nil)
            }
        }
    }
}
extension NotificationService: AVAudioPlayerDelegate {
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        //播放结束,执行contentHandler,结束这个通知
        if (contentHandler != nil) && (bestAttemptContent != nil) {
            bestAttemptContent?.sound = .none//由于有语音播报了,这里把系统通知的声音关了
            contentHandler!(bestAttemptContent!)
        }
    }
}

像 哥小兔到账.mp3、 元.mp3、 万.mp3 这些语音文件,可以去ai语音合成,类似的有:百度、科大讯飞、标贝等

语音生成工具:标贝悦读、百度、科大讯飞

第三种
经过查找,大致率支付宝、微信使用的使用voip模式,通过查找微信与支付宝的ipa,ipa中配置的文件都UIBackgroundModes(后台模式)包含voip。
所以大致率支付宝与微信都使用的是Voip PushKit实现的收款的语音播报功能
之后也会调查下Voip PushKit实现的收款的语音播报功能,PushKit和极光推送还不太一样。持续关注中。。。

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容