替换 RemoteAudioHandler 中 prepareaudio 的 stub 为完整链路:权限检查 →
录音浮层 → 转码 → 七牛上传 → 反向 callback getaudiourl + 子游戏额外
recordSuccess。mediaTypeAudio 仍为 stub(Phase 3.F 处理)。
## 完整链路
1. H5 调 prepareaudio
2. `AVAudioSession.requestRecordPermission`
- 拒绝:弹 Alert("需要访问您的麦克风..."),**不触发 cb**
(对齐 daoqi NewRootVC.m:314-319)
3. 同意 → `RecordingPresenter.start(on: hostVC, fileName:)`
- 启动失败:log + return,**不触发 cb**
- 启动成功:立即触发 `cb("Response from prepareaudio")`,对齐 daoqi
NewRootVC.m:322 `[recorderVC beginRecordByFileName:..]` 之后 cb 行为
4. 用户松手 / 60s 自动停 → `processCompletedRecording` 后台 Task
- 用 AVAudioPlayer 读 wav `duration` → `round` 取整秒(对齐 daoqi
NewRootVC.m:1891-1893)
- 生成 amr 文件名:
- 大厅 `yyyyMMddHHmmss%08X.amr`(无下划线,NewRootVC.m:1908)
- 子游戏 `yyyyMMddHHmmss_%08X.amr`(有下划线,gameController.m:2269)
- `AMRCodec.wavToAmr`
- `QiniuUploader.upload`(key = 本地文件名,对齐 daoqi 上传命名)
5. 反向 callback(MainActor):
- `getaudiourl({audiourl: String, time: String})`
⚠️ time 是字符串(%ld 整秒),不是 number — 对齐 daoqi
NewRootVC.m:1923 / gameController.m:2305
- **仅子游戏**额外 `recordSuccess({fileUrl, fileName, fileKey})`
对齐 daoqi gameController.m:2310;大厅 NewRootVC 无此 callHandler
## register 签名扩展
新增参数:
- `hostVC: @escaping @MainActor @Sendable () -> UIViewController?`
—— 每次录音重新求值,避免捕获过时引用
- `isSubGame: Bool` —— 决定是否触发 recordSuccess
WebContainerViewController(大厅)传 `isSubGame: false`;
SubGameViewController(子游戏)传 `isSubGame: true`。
## QiniuUploader 改动
key 生成方式:UUID → 本地文件名(lastPathComponent)。让调用方完全控制
key 命名,对齐 daoqi 录音文件命名规则。H5 通过 audiourl 转发给对端,对端
按文件名规律假设,必须严格对齐。
## 错误处理
启动失败 / 转码失败 / 上传失败:log + return,不触发反向 callback。
对齐 daoqi 同款 silent return 行为(mediaTypeAudio 下载失败、转码失败
也是同样 silent return 模式)。
## 验证
BuildProject 通过 0 错误。
107 lines
4.4 KiB
Swift
107 lines
4.4 KiB
Swift
//
|
||
// QiniuUploader.swift
|
||
// ylgamehall
|
||
//
|
||
// 七牛上传 actor wrapper。包 `QNUploadManager` 为 async/await。
|
||
//
|
||
// ⚠️ canImport(QiniuSDK) 守卫:在 Xcode 把 https://github.com/qiniu/objc-sdk
|
||
// 添加到 Swift Package Dependencies 之前,本文件编译为 no-op,upload 永远抛
|
||
// QiniuUploadError.sdkNotLinked。后续 handler 可降级到 stub 路径。
|
||
//
|
||
// 上传完成后返回 UploadedFile,字段映射给 H5 `getaudiourl` / `recordSuccess`。
|
||
//
|
||
|
||
import Foundation
|
||
|
||
#if canImport(QiniuSDK)
|
||
import QiniuSDK
|
||
#endif
|
||
|
||
public struct UploadedFile: Sendable {
|
||
public let fileUrl: String // 公网访问 URL(http://{domain}/{key})
|
||
public let fileName: String // 原始本地文件名
|
||
public let fileKey: String // 七牛 key(path-on-cdn)
|
||
public let timeSec: Int // 录音时长(秒)
|
||
}
|
||
|
||
public enum QiniuUploadError: Error, Sendable {
|
||
case sdkNotLinked
|
||
case fileNotFound(String)
|
||
case uploadFailed(any Error)
|
||
case responseInvalid
|
||
}
|
||
|
||
public actor QiniuUploader {
|
||
|
||
public static let shared = QiniuUploader()
|
||
|
||
public init() {}
|
||
|
||
/// 上传文件。
|
||
/// - Parameters:
|
||
/// - localFile: 本地文件 URL(amr 录音文件)
|
||
/// - timeSec: 录音时长,回传给 H5 `getaudiourl` 用
|
||
/// - Returns: UploadedFile(含公网 URL)
|
||
public func upload(_ localFile: URL, timeSec: Int) async throws -> UploadedFile {
|
||
#if canImport(QiniuSDK)
|
||
let fm = FileManager.default
|
||
guard fm.fileExists(atPath: localFile.path) else {
|
||
throw QiniuUploadError.fileNotFound(localFile.path)
|
||
}
|
||
|
||
// 七牛 key 直接用本地文件名(调用方负责命名)。对齐 daoqi 录音文件命名规则:
|
||
// - 大厅:yyyyMMddHHmmss%08X.amr(NewRootVC.m:1908,无下划线)
|
||
// - 子游戏:yyyyMMddHHmmss_%08X.amr(gameController.m:2269,有下划线)
|
||
// H5 通过 audiourl 转发给对端,对端按文件名规律假设——必须严格对齐。
|
||
let key = QiniuConfig.recordingDirectory + localFile.lastPathComponent
|
||
|
||
// 客户端自签 token(参 QiniuTokenSigner / msext QiniuManager 同款算法)
|
||
let token = QiniuTokenSigner.uploadToken(key: key)
|
||
|
||
// 七牛 SDK 8.x:`QNUploadManager.init()` 被标 `kQNDeprecated`,必须走
|
||
// `initWithConfiguration:`。用 defaultConfigurationV2(v2 已替代旧 defaultConfiguration)。
|
||
// initWithConfiguration: ObjC 端 instancetype 桥到 Swift 仍为可选;
|
||
// 实际几乎不会失败(内部仅初始化无 IO),失败按 responseInvalid 抛出。
|
||
let cfg = QNConfiguration.defaultConfigurationV2()
|
||
guard let mgr = QNUploadManager(configuration: cfg) else {
|
||
throw QiniuUploadError.responseInvalid
|
||
}
|
||
let fileName = localFile.lastPathComponent
|
||
|
||
return try await withCheckedThrowingContinuation { (cont: CheckedContinuation<UploadedFile, Error>) in
|
||
mgr.putFile(
|
||
localFile.path,
|
||
key: key,
|
||
token: token,
|
||
complete: { info, savedKey, _ in
|
||
if let info, info.isOK, let savedKey {
|
||
let url = QiniuConfig.publicURL(forKey: savedKey)
|
||
cont.resume(returning: UploadedFile(
|
||
fileUrl: url,
|
||
fileName: fileName,
|
||
fileKey: savedKey,
|
||
timeSec: timeSec
|
||
))
|
||
} else if let error = info?.error {
|
||
cont.resume(throwing: QiniuUploadError.uploadFailed(error))
|
||
} else {
|
||
cont.resume(throwing: QiniuUploadError.responseInvalid)
|
||
}
|
||
},
|
||
option: nil
|
||
)
|
||
}
|
||
#else
|
||
throw QiniuUploadError.sdkNotLinked
|
||
#endif
|
||
}
|
||
|
||
/// 取消所有进行中任务(接 BackGameDataHandler 清理钩子用)。
|
||
/// QNUploadManager 没有「取消全部」API,单任务用 QNUploadOption.cancellationSignal 实现;
|
||
/// 当前实现以 task 粒度由 Swift Task.cancel 传递(withCheckedThrowingContinuation
|
||
/// 已在 cancellation 时 throw CancellationError),无需额外状态。
|
||
public func cancelInFlight() {
|
||
// no-op(依赖 Task.cancel 传播);如未来需要硬 cancel,改为持有 QNUploadOption 并触发 cancellationSignal
|
||
}
|
||
}
|