美文网首页
iOS接入阿里云盘获取视频播放url遇到json解析错误Inva

iOS接入阿里云盘获取视频播放url遇到json解析错误Inva

作者: 生命不止运动不息 | 来源:发表于2024-07-22 17:25 被阅读0次

错误如下

dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "video_preview_play_info", intValue: nil), CodingKeys(stringValue: "live_transcoding_task_list", intValue: nil), _JSONKey(stringValue: "Index 2", intValue: 2), CodingKeys(stringValue: "url", intValue: nil)], debugDescription: "Invalid URL string.", underlyingError: nil))

调用代码

let playInfo = try await ali_client
                            .authorize()
                            .send(
                                AliyunpanScope.Video.GetVideoPreviewPlayInfo(
                                    .init(
                                        drive_id: file.drive_id,
                                        file_id: file.file_id)))

错误原因

返回的json数据中url字段的值是空。

解决办法

修改Aliyunpan的AliyunpanFile.swift的源码,具体为
在LiveTranscodingTask结构体中自定义解码方法,捕获url的解析
即添加如下解析代码

public init(from decoder: Decoder) throws {
                let container: KeyedDecodingContainer<AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys> = try decoder.container(keyedBy: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.self)
                self.template_id = try container.decode(String.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.template_id)
                self.template_name = try container.decodeIfPresent(String.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.template_name)
                self.template_width = try container.decodeIfPresent(Int.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.template_width)
                self.template_height = try container.decodeIfPresent(Int.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.template_height)
                self.keep_original_resolution = try container.decodeIfPresent(Bool.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.keep_original_resolution)
                self.stage = try container.decodeIfPresent(String.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.stage)
                self.status = try container.decode(AliyunpanFile.VideoPreviewPlayInfo.Status.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.status)
                
                do {
                    self.url = try container.decodeIfPresent(URL.self, forKey: AliyunpanFile.VideoPreviewPlayInfo.LiveTranscodingTask.CodingKeys.url)
                } catch {
                    print("视频url解析错误 wkun");
                    self.url = nil
                }
            }
重点是这里

相关文章

网友评论

      本文标题:iOS接入阿里云盘获取视频播放url遇到json解析错误Inva

      本文链接:https://www.haomeiwen.com/subject/elxyhjtx.html