From ae06401011c06d71eaa8e6bd6851ae52236f5b6d Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Tue, 5 Nov 2024 17:01:39 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E7=9B=B4=E6=92=AD=E8=A7=86=E9=A2=91=E6=92=AD=E6=94=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../components/videoplayer/VideoPlayer.ets | 29 +++++++++++++------ .../videoplayer/VideoPlayerApiImpl.ets | 5 +++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index 399f0f008..eedf4d2a0 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -91,15 +91,23 @@ export class VideoPlayer { ); await this.bindState(); - if (this.iUrl != null) { - this.avPlayer.url = this.getIUri(); - } else { - this.avPlayer.fdSrc = this.url!; - } - if (this.headers != null) { + /// 同时设置avPlayer.url和avPlayer.setMediaSource会导致直播视频无法正确播放 + /// 目前无法确定其他设置了header的视频能不能正常使用,暂时先这样使用 + if (!this.headers) { + if (this.iUrl != null) { + this.avPlayer.url = this.getIUri(); + } else { + this.avPlayer.fdSrc = this.url!; + } + } else { let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.iUrl, this.headers); - let playbackStrategy: media.PlaybackStrategy = {preferredWidth: 1, preferredHeight: 2, preferredBufferDuration: 20, preferredHdr: false}; + let playbackStrategy: media.PlaybackStrategy = { + preferredWidth: 1, + preferredHeight: 2, + preferredBufferDuration: 20, + preferredHdr: false + }; // 设置媒体来源和播放策略 await this.avPlayer.setMediaSource(mediaSource, playbackStrategy); } @@ -168,7 +176,9 @@ export class VideoPlayer { this.url = globalVideoList[this.index].src; } } - this.avPlayer.reset(); + /// 网络直播视频采用分片操作方式,这个地方使用reset会导致无法正常播放 + /// 不确定其他场景影响,暂时使用,原生上也不需要在这个地方设置reset + // this.avPlayer.reset(); break; case AvplayerStatus.RELEASED: this.avPlayer.release(); @@ -194,7 +204,8 @@ export class VideoPlayer { Log.e(TAG, "avPlayer Events.ERROR: " + JSON.stringify(err)); // 播放直播视频时,设置 loop 会报错,而 loop 一定会设置(video_player.dart 中初始化之后会 _applyLooping),所以屏蔽掉该报错 // message: Unsupport Capability: The stream is live stream, not support loop - if(err.code == 801) { + /// 网络直播会有许多关于5400102和5400103的报错,需要跳过,不然直播视频无法正常播放 + if(err.code == 801 || err.code == 5400102 || err.code == 5400103) { return; } this.avPlayer?.reset(); diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayerApiImpl.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayerApiImpl.ets index 30ec98380..65608177e 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayerApiImpl.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayerApiImpl.ets @@ -81,8 +81,11 @@ export class VideoPlayerApiImpl { let flutterRenderer = this.flutterState.getTextureRegistry(); let uri: string = arg.getUri(); let asset: string = arg.getAsset(); - let header: Record = {}; + let header: Record | null = null; arg.getHttpHeaders().forEach((value, key) => { + if (!header) { + header = {} + } header[key.toString()] = value.toString(); }) let textureId: number = flutterRenderer.getTextureId(); -- Gitee From 7d5c74e4cdfd1030fcc66321dc17832c90ecf0ce Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Wed, 6 Nov 2024 16:39:43 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=83=A8=E5=88=86reset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../ohos/src/main/ets/components/videoplayer/VideoPlayer.ets | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index eedf4d2a0..e41cbd9b5 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -176,9 +176,6 @@ export class VideoPlayer { this.url = globalVideoList[this.index].src; } } - /// 网络直播视频采用分片操作方式,这个地方使用reset会导致无法正常播放 - /// 不确定其他场景影响,暂时使用,原生上也不需要在这个地方设置reset - // this.avPlayer.reset(); break; case AvplayerStatus.RELEASED: this.avPlayer.release(); -- Gitee From fa11ea1b8e8a05eb88f0a5794722ed19c766225d Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Thu, 7 Nov 2024 19:38:32 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=EF=BC=8C=E5=88=A0=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../components/videoplayer/VideoPlayer.ets | 57 ++++++++----------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index e41cbd9b5..09bbff9b8 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -47,8 +47,8 @@ export class VideoPlayer { private status: number = -1; private loop: boolean = false; private index: number = 0; - private url?: resourceManager.RawFileDescriptor | null = {} as resourceManager.RawFileDescriptor; - private iUrl: string | null = null; + private rawFile?: resourceManager.RawFileDescriptor | null = {} as resourceManager.RawFileDescriptor; + private url: string | null = null; private surfaceId: string = ''; private seekTime: number = PlayConstants.PROGRESS_SEEK_TIME; private positionX: number = PlayConstants.POSITION_X; @@ -60,12 +60,12 @@ export class VideoPlayer { private fd: number | null = null; private headers: Record | null; - constructor(playerModel: PlayerModel, textureEntry: SurfaceTextureEntry, url: resourceManager.RawFileDescriptor | null, iUrl: string | null, eventChannel: EventChannel, AudioFocus: Boolean, headers: Record | null) { + constructor(playerModel: PlayerModel, textureEntry: SurfaceTextureEntry, rawFile: resourceManager.RawFileDescriptor | null, url: string | null, eventChannel: EventChannel, AudioFocus: Boolean, headers: Record | null) { this.playerModel = playerModel; this.textureEntry = textureEntry; this.surfaceId = textureEntry.getSurfaceId().toString(); + this.rawFile = rawFile; this.url = url; - this.iUrl = iUrl; this.eventChannel = eventChannel; this.headers = headers; if (AudioFocus == true) { @@ -92,24 +92,17 @@ export class VideoPlayer { await this.bindState(); - /// 同时设置avPlayer.url和avPlayer.setMediaSource会导致直播视频无法正确播放 - /// 目前无法确定其他设置了header的视频能不能正常使用,暂时先这样使用 + /// 同时使用avPlayer.url和avPlayer.setMediaSource会导致直播视频无法正确播放 if (!this.headers) { - if (this.iUrl != null) { + if (this.url) { this.avPlayer.url = this.getIUri(); } else { - this.avPlayer.fdSrc = this.url!; + this.avPlayer.fdSrc = this.rawFile!; } } else { - let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.iUrl, this.headers); - let playbackStrategy: media.PlaybackStrategy = { - preferredWidth: 1, - preferredHeight: 2, - preferredBufferDuration: 20, - preferredHdr: false - }; + let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.url, this.headers); // 设置媒体来源和播放策略 - await this.avPlayer.setMediaSource(mediaSource, playbackStrategy); + await this.avPlayer.setMediaSource(mediaSource); } } } @@ -134,10 +127,10 @@ export class VideoPlayer { switch (avplayerStatus) { case AvplayerStatus.IDLE: this.resetProgress(); - if (this.iUrl) { + if (this.url) { this.avPlayer.url = this.getIUri(); } else { - this.avPlayer.fdSrc = this.url!; + this.avPlayer.fdSrc = this.rawFile!; } break; case AvplayerStatus.INITIALIZED: @@ -168,12 +161,11 @@ export class VideoPlayer { if (!this.loop) { let curIndex = this.index + PlayConstants.PLAYER_NEXT; let globalVideoList = GlobalContext.getContext().getObject('globalVideoList') as VideoItem[]; - this.index = (curIndex === globalVideoList.length) ? - PlayConstants.PLAYER_FIRST : curIndex; - if (this.iUrl) { - this.iUrl = globalVideoList[this.index].iSrc; + this.index = (curIndex === globalVideoList.length) ? PlayConstants.PLAYER_FIRST : curIndex; + if (this.url) { + this.url = globalVideoList[this.index].iSrc; } else { - this.url = globalVideoList[this.index].src; + this.rawFile = globalVideoList[this.index].src; } } break; @@ -198,13 +190,14 @@ export class VideoPlayer { }); this.avPlayer.on(Events.ERROR, (err: BusinessError) => { - Log.e(TAG, "avPlayer Events.ERROR: " + JSON.stringify(err)); // 播放直播视频时,设置 loop 会报错,而 loop 一定会设置(video_player.dart 中初始化之后会 _applyLooping),所以屏蔽掉该报错 // message: Unsupport Capability: The stream is live stream, not support loop - /// 网络直播会有许多关于5400102和5400103的报错,需要跳过,不然直播视频无法正常播放 + /// 规避部分错误导致的reset,如:5400102 当前状态机不支持此操作;5400103 出现IO错误 if(err.code == 801 || err.code == 5400102 || err.code == 5400103) { + Log.e(TAG, "AvPlayer Avoid Error Reporting: " + JSON.stringify(err)); return; } + Log.e(TAG, "avPlayer Events.ERROR: " + JSON.stringify(err)); this.avPlayer?.reset(); this.sendError(err); }) @@ -305,10 +298,10 @@ export class VideoPlayer { let curIndex = this.index - PlayConstants.CONTROL_NEXT; this.index = (curIndex === -PlayConstants.CONTROL_NEXT) ? (globalVideoList.length - PlayConstants.CONTROL_NEXT) : curIndex; - if (this.iUrl) { - this.iUrl = globalVideoList[this.index].iSrc; + if (this.url) { + this.url = globalVideoList[this.index].iSrc; } else { - this.url = globalVideoList[this.index].src; + this.rawFile = globalVideoList[this.index].src; } this.avPlayer.reset(); } @@ -328,10 +321,10 @@ export class VideoPlayer { let curIndex = this.index + PlayConstants.CONTROL_NEXT; this.index = (curIndex === globalVideoList.length) ? PlayConstants.CONTROL_FIRST : curIndex; - if (this.iUrl) { - this.iUrl = globalVideoList[this.index].iSrc; + if (this.url) { + this.url = globalVideoList[this.index].iSrc; } else { - this.url = globalVideoList[this.index].src; + this.rawFile = globalVideoList[this.index].src; } this.avPlayer.reset(); } @@ -591,7 +584,7 @@ export class VideoPlayer { } getIUri(): string { - let iUrl = this.iUrl; + let iUrl = this.url; const ohosFilePrefix = 'file://'; if (iUrl != null && iUrl.startsWith(ohosFilePrefix)) { this.fd = fs.openSync(iUrl, fs.OpenMode.READ_ONLY).fd; -- Gitee From 3bb93fdc81de24989430bafb22dede3e83ef7857 Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Fri, 8 Nov 2024 14:15:48 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0media.PlaybackStrategy?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E7=BD=91=E7=BB=9C=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E8=83=BD=E6=AD=A3=E5=B8=B8=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../src/main/ets/components/videoplayer/VideoPlayer.ets | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index 09bbff9b8..639d4b571 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -101,8 +101,15 @@ export class VideoPlayer { } } else { let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.url, this.headers); + /// 网络直播视频必须设置之后才能正常缓存 + let playbackStrategy: media.PlaybackStrategy = { + preferredWidth: 1, + preferredHeight: 2, + preferredBufferDuration: 20, + preferredHdr: false + }; // 设置媒体来源和播放策略 - await this.avPlayer.setMediaSource(mediaSource); + await this.avPlayer.setMediaSource(mediaSource, playbackStrategy); } } } -- Gitee From 7697e2522d31a6a961fae99a43b55970e488783e Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Fri, 8 Nov 2024 14:22:47 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8F=AF=E9=80=89?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../src/main/ets/components/videoplayer/VideoPlayer.ets | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index 639d4b571..cff219db1 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -102,12 +102,7 @@ export class VideoPlayer { } else { let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.url, this.headers); /// 网络直播视频必须设置之后才能正常缓存 - let playbackStrategy: media.PlaybackStrategy = { - preferredWidth: 1, - preferredHeight: 2, - preferredBufferDuration: 20, - preferredHdr: false - }; + let playbackStrategy: media.PlaybackStrategy = {}; // 设置媒体来源和播放策略 await this.avPlayer.setMediaSource(mediaSource, playbackStrategy); } -- Gitee From c4437bda3645baf8448cf62217bea822214034a4 Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Fri, 8 Nov 2024 16:23:01 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- .../src/main/ets/components/videoplayer/VideoPlayer.ets | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets index cff219db1..55472f2c9 100644 --- a/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/packages/video_player/video_player_ohos/ohos/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -39,6 +39,11 @@ const FORMAT_SS: String = 'ss'; const FORMAT_DASH: String = 'dash'; const FORMAT_HLS: String = 'hls'; const FORMAT_OTHER: String = 'other'; + +const OPERATE_ERROR: number = 801; +const AVPLAYER_STATE_ERROR: number = 5400102; +const AVPLAYER_IO_ERROR: number = 5400103; + const TAG = 'VideoPlayer' export class VideoPlayer { private avPlayer: media.AVPlayer | null = null; @@ -195,7 +200,7 @@ export class VideoPlayer { // 播放直播视频时,设置 loop 会报错,而 loop 一定会设置(video_player.dart 中初始化之后会 _applyLooping),所以屏蔽掉该报错 // message: Unsupport Capability: The stream is live stream, not support loop /// 规避部分错误导致的reset,如:5400102 当前状态机不支持此操作;5400103 出现IO错误 - if(err.code == 801 || err.code == 5400102 || err.code == 5400103) { + if(err.code == OPERATE_ERROR || err.code == AVPLAYER_STATE_ERROR || err.code == AVPLAYER_IO_ERROR) { Log.e(TAG, "AvPlayer Avoid Error Reporting: " + JSON.stringify(err)); return; } -- Gitee