1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import media from '@ohos.multimedia.media' 16import Logger from '../model/Logger' 17 18const TAG: string = 'VideoPlayerUtils' 19 20export default class VideoPlayerUtils { 21 private videoPlayer = undefined 22 private playPath: string = '' 23 private surfaceID: string = '' 24 private finishCallBack: () => void = undefined 25 26 async initVideoPlayer(playSrc, surfaceID) { 27 await this.release() 28 this.playPath = playSrc 29 this.surfaceID = surfaceID 30 this.videoPlayer = await media.createVideoPlayer() 31 Logger.info(TAG, 'createVideoPlayer') 32 this.videoPlayer.url = this.playPath 33 this.videoPlayer.on('playbackCompleted', () => { 34 Logger.info(TAG, 'play finish') 35 this.seek(0) 36 if (this.finishCallBack) { 37 this.finishCallBack() 38 } 39 }); 40 await this.videoPlayer.setDisplaySurface(this.surfaceID) 41 Logger.info(TAG, 'setDisplaySurface') 42 await this.videoPlayer.prepare() 43 await this.videoPlayer.play() 44 Logger.info(TAG, 'start play') 45 } 46 47 async play() { 48 Logger.info(TAG, 'play') 49 if (typeof (this.videoPlayer) != 'undefined') { 50 await this.videoPlayer.play() 51 } 52 } 53 54 async seek(time) { 55 Logger.info(TAG, 'seek') 56 if (typeof (this.videoPlayer) != 'undefined') { 57 await this.videoPlayer.seek(time) 58 } 59 } 60 61 getCurrentTime() { 62 if (typeof (this.videoPlayer) != 'undefined') { 63 return this.videoPlayer.currentTime 64 } 65 return 0 66 } 67 68 async pause() { 69 Logger.info(TAG, 'pause') 70 if (typeof (this.videoPlayer) != 'undefined') { 71 await this.videoPlayer.pause() 72 } 73 } 74 75 async stop() { 76 Logger.info(TAG, 'stop') 77 if (typeof (this.videoPlayer) != 'undefined') { 78 await this.videoPlayer.stop() 79 } 80 } 81 82 async reset(playSrc) { 83 if (typeof (this.videoPlayer) != 'undefined') { 84 this.playPath = playSrc 85 await this.videoPlayer.reset() 86 this.videoPlayer.url = this.playPath 87 await this.videoPlayer.prepare() 88 await this.videoPlayer.play() 89 } 90 } 91 92 async release() { 93 if (typeof (this.videoPlayer) != 'undefined') { 94 await this.videoPlayer.release() 95 Logger.info(TAG, 'release success') 96 } 97 } 98 99 setFinishCallBack(callback) { 100 this.finishCallBack = callback 101 } 102}