Showing
1 changed file
with
47 additions
and
1 deletions
| @@ -85,6 +85,7 @@ export class WDPlayerController { | @@ -85,6 +85,7 @@ export class WDPlayerController { | ||
| 85 | */ | 85 | */ |
| 86 | private bindState() { | 86 | private bindState() { |
| 87 | this.avPlayer?.on(Events.STATE_CHANGE, async (state) => { | 87 | this.avPlayer?.on(Events.STATE_CHANGE, async (state) => { |
| 88 | + //console.info(TAG,`cj2024 state = ${state}`) | ||
| 88 | if (this.avPlayer == null) { | 89 | if (this.avPlayer == null) { |
| 89 | return | 90 | return |
| 90 | } | 91 | } |
| @@ -179,6 +180,49 @@ export class WDPlayerController { | @@ -179,6 +180,49 @@ export class WDPlayerController { | ||
| 179 | this.videoWidth = width | 180 | this.videoWidth = width |
| 180 | this.videoHeight = height | 181 | this.videoHeight = height |
| 181 | }) | 182 | }) |
| 183 | + this.avPlayer?.on(Events.AUDIOINTERRUPT, async(interruptEvent: audio.InterruptEvent) => { | ||
| 184 | + //https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/media/audio/audio-playback-concurrency.md#%E5%A4%84%E7%90%86%E9%9F%B3%E9%A2%91%E7%84%A6%E7%82%B9%E4%BA%8B%E4%BB%B6 | ||
| 185 | + if (interruptEvent.forceType === audio.InterruptForceType.INTERRUPT_FORCE) { | ||
| 186 | + switch (interruptEvent.hintType) { | ||
| 187 | + case audio.InterruptHint.INTERRUPT_HINT_RESUME: | ||
| 188 | + await this.play() | ||
| 189 | + break; | ||
| 190 | + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: | ||
| 191 | + // 此分支表示系统已将音频流暂停(临时失去焦点),为保持状态一致,应用需切换至音频暂停状态 | ||
| 192 | + // 临时失去焦点:待其他音频流释放音频焦点后,本音频流会收到resume对应的音频打断事件,到时可自行继续播放 | ||
| 193 | + case audio.InterruptHint.INTERRUPT_HINT_STOP: | ||
| 194 | + // 此分支表示系统已将音频流停止(永久失去焦点),为保持状态一致,应用需切换至音频暂停状态 | ||
| 195 | + // 永久失去焦点:后续不会再收到任何音频打断事件,若想恢复播放,需要用户主动触发。 | ||
| 196 | + // this.avPlayer?.play() | ||
| 197 | + await this.pause() | ||
| 198 | + break; | ||
| 199 | + } | ||
| 200 | + }else if (interruptEvent.forceType === audio.InterruptForceType.INTERRUPT_SHARE) { | ||
| 201 | + // 共享打断类型(INTERRUPT_SHARE):应用可自主选择执行相关操作或忽略音频打断事件 | ||
| 202 | + switch (interruptEvent.hintType) { | ||
| 203 | + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: | ||
| 204 | + case audio.InterruptHint.INTERRUPT_HINT_RESUME: | ||
| 205 | + // 此分支表示临时失去焦点后被暂停的音频流此时可以继续播放,建议应用继续播放,切换至音频播放状态 | ||
| 206 | + // 若应用此时不想继续播放,可以忽略此音频打断事件,不进行处理即可 | ||
| 207 | + // 继续播放,此处主动执行start(),以标识符变量started记录start()的执行结果 | ||
| 208 | + // await audioRenderer.start().then(() => { | ||
| 209 | + // started = true; // start()执行成功 | ||
| 210 | + // }).catch((err: BusinessError) => { | ||
| 211 | + // started = false; // start()执行失败 | ||
| 212 | + // }); | ||
| 213 | + // // 若start()执行成功,则切换至音频播放状态 | ||
| 214 | + // if (started) { | ||
| 215 | + // isPlay = true; // 此句为简化处理,代表应用切换至音频播放状态的若干操作 | ||
| 216 | + // } else { | ||
| 217 | + // // 音频继续播放执行失败 | ||
| 218 | + // } | ||
| 219 | + this.avPlayer?.play() | ||
| 220 | + break; | ||
| 221 | + default: | ||
| 222 | + break; | ||
| 223 | + } | ||
| 224 | + } | ||
| 225 | + }) | ||
| 182 | } | 226 | } |
| 183 | 227 | ||
| 184 | public setXComponentController(controller: XComponentController) { | 228 | public setXComponentController(controller: XComponentController) { |
| @@ -271,10 +315,12 @@ export class WDPlayerController { | @@ -271,10 +315,12 @@ export class WDPlayerController { | ||
| 271 | // if (this.avPlayer == null) { | 315 | // if (this.avPlayer == null) { |
| 272 | // return | 316 | // return |
| 273 | // } | 317 | // } |
| 318 | + //Logger.debug(TAG, "start play") | ||
| 274 | this.avPlayer?.prepare().then(() => { | 319 | this.avPlayer?.prepare().then(() => { |
| 275 | this.avPlayer?.play() | 320 | this.avPlayer?.play() |
| 276 | }, (err: BusinessError) => { | 321 | }, (err: BusinessError) => { |
| 277 | - console.error('Failed to prepare,error message is :' + err.message) | 322 | + console.error(TAG,'Failed to prepare,error message is :' + err.message) |
| 323 | + //this.avPlayer?.stop() | ||
| 278 | }) | 324 | }) |
| 279 | } | 325 | } |
| 280 | 326 |
-
Please register or login to post a comment