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 */ 15 16// @ts-nocheck 17import mediaLibrary from '@ohos.multimedia.mediaLibrary' 18import media from '@ohos.multimedia.media' 19import Logger from '../model/Logger' 20 21const TAG = 'myMedia:' 22 23export class myMedia { 24 resourceAddress: mediaLibrary.FileAsset = undefined 25 fileName: string = undefined 26 totalDuration: number = 0 27 private playStatus: string 28 private playMode: string 29 private currentNode: string 30 format: string 31 mediaType: number 32 private media: media.AudioPlayer | media.VideoPlayer 33 private surfaceId: string = '' 34 private isLoop: boolean 35 private audioData: Array<mediaLibrary.FileAsset> 36 private videoData: Array<mediaLibrary.FileAsset> 37 private fileIndex: number = 0 38 private mediaDataLen: number 39 private uri:string = undefined 40 getAudioData(audioData) { 41 this.audioData = audioData 42 } 43 44 getVideoData(videoData) { 45 this.videoData = videoData 46 } 47 48 prev(surfaceId) { 49 if (this.fileIndex > 0) { 50 this.fileIndex -= 1 51 } else { 52 this.fileIndex = this.mediaDataLen - 1 53 } 54 if (this.resourceAddress.mediaType == mediaLibrary.MediaType.AUDIO) { 55 this.resourceAddress = this.audioData[this.fileIndex] 56 this.init(this.resourceAddress) 57 } else if (this.resourceAddress.mediaType == mediaLibrary.MediaType.VIDEO) { 58 this.resourceAddress = this.videoData[this.fileIndex] 59 this.init(this.resourceAddress, surfaceId) 60 } 61 } 62 63 next(surfaceId?) { 64 if (this.fileIndex < this.mediaDataLen - 1) { 65 this.fileIndex += 1 66 } else { 67 this.fileIndex = 0 68 } 69 if (this.resourceAddress.mediaType == mediaLibrary.MediaType.AUDIO) { 70 this.resourceAddress = this.audioData[this.fileIndex] 71 this.init(this.resourceAddress) 72 } else if (this.resourceAddress.mediaType == mediaLibrary.MediaType.VIDEO) { 73 this.resourceAddress = this.videoData[this.fileIndex] 74 this.init(this.resourceAddress, surfaceId) 75 } 76 } 77 78 getFileIndex(data) { 79 data.forEach((file, index) => { 80 if (file.id === this.resourceAddress.id) { 81 this.fileIndex = index 82 } 83 }) 84 } 85 86 getResourceAddress() { 87 return this.resourceAddress 88 } 89 90 async httpInit(url, surfaceId?) { 91 this.uri = url 92 if (surfaceId) { 93 this.surfaceId = surfaceId.toString() 94 Logger.info(TAG, `surfaceId success: ${surfaceId}`) 95 } 96 await this.stop() 97 await this.release() 98 this.media = await media.createVideoPlayer() 99 await this.setVideoCallBack(this.media) 100 this.media.url = url 101 await this.media.setDisplaySurface(this.surfaceId).then(() => { 102 Logger.info(TAG, `httpInit success`) 103 }).catch((error) => { 104 Logger.info(TAG, `httpInit setDisplaySurface fali error:${error.message}`) 105 }) 106 await this.media.prepare() 107 this.totalDuration = this.media.duration 108 this.fileName = 'Http' 109 this.media.play() 110 } 111 112 async init(resourceAddress: mediaLibrary.FileAsset, surfaceId?) { 113 Logger.info(TAG, `init state}`) 114 if (surfaceId) { 115 this.surfaceId = surfaceId.toString() 116 Logger.info(TAG, `surfaceId success: ${surfaceId}`) 117 } 118 this.stop() 119 this.release() 120 this.resourceAddress = resourceAddress 121 Logger.info(TAG, `resourceAddress success: ${this.resourceAddress}`) 122 this.fileName = resourceAddress.displayName 123 Logger.info(TAG, `fileName success: ${this.fileName}`) 124 this.totalDuration = resourceAddress.duration 125 Logger.info(TAG, `totalDuration success: ${this.totalDuration}`) 126 this.resourceAddress.open('r').then(async (fd) => { 127 Logger.info(TAG, `fd success: ${fd}`) 128 if (this.resourceAddress.mediaType == mediaLibrary.MediaType.AUDIO) { 129 Logger.info(TAG, `AUDIO success`) 130 this.getFileIndex(this.audioData) 131 this.mediaDataLen = this.audioData.length 132 this.media = media.createAudioPlayer() 133 Logger.info(TAG, `AUDIO success: ${this.media}`) 134 this.setAudioCallBack(this.media) 135 this.media.src = 'fd://' + fd 136 } else if (this.resourceAddress.mediaType == mediaLibrary.MediaType.VIDEO) { 137 Logger.info(TAG, `VIDEO success`) 138 this.getFileIndex(this.videoData) 139 this.mediaDataLen = this.videoData.length 140 this.media = await media.createVideoPlayer() 141 await this.setVideoCallBack(this.media) 142 this.media.url = 'fd://' + fd 143 await this.media.setDisplaySurface(this.surfaceId) 144 await this.media.prepare() 145 Logger.info(TAG, `VIDEO end`) 146 } 147 this.getPlay() 148 this.getPlayMode(this.isLoop) 149 }) 150 } 151 152 setAudioCallBack(audioPlayer) { 153 audioPlayer.on('dataLoad', () => { 154 Logger.info(TAG, `AUDIO case dataLoad called`) 155 Logger.info(TAG, `AUDIO audioPlayer.duration : ${audioPlayer.duration}`) 156 }) 157 audioPlayer.on('pause', () => { 158 Logger.info(TAG, `AUDIO case pause called`) 159 }) 160 audioPlayer.on('play', () => { 161 Logger.info(TAG, `AUDIO case play called`) 162 }) 163 audioPlayer.on('stop', () => { 164 Logger.info(TAG, `AUDIO case stop called`) 165 }) 166 audioPlayer.on('finish', () => { 167 Logger.info(TAG, `AUDIO case finish called`) 168 this.release() 169 if(this.resourceAddress && this.resourceAddress.mediaType){ 170 this.next() 171 } 172 }) 173 audioPlayer.on('error', (err) => { 174 Logger.info(TAG, ` AUDIO audioPlayer error called : ${err}`) 175 }) 176 } 177 178 setVideoCallBack(videoPlayer) { 179 videoPlayer.on('prepare', () => { 180 Logger.info(TAG, 'VIDEO start') 181 Logger.info(TAG, `VIDEO videoPlayer duration `) 182 }) 183 184 videoPlayer.on('playing', () => { 185 Logger.info(TAG, `VIDEO playing finish`) 186 }) 187 videoPlayer.on('paused', () => { 188 Logger.info(TAG, `VIDEO paused finish`) 189 }) 190 videoPlayer.on('stopped', () => { 191 Logger.info(TAG, `VIDEO stopped finish`) 192 }) 193 videoPlayer.on('error', (err) => { 194 Logger.info(TAG, `VIDEO error : ${err}`) 195 }) 196 videoPlayer.on('playbackCompleted', () => { 197 Logger.info(TAG, `VIDEO playbackCompleted finish`) 198 this.release() 199 if(this.resourceAddress && this.resourceAddress.mediaType){ 200 this.next() 201 } 202 }) 203 204 } 205 206 setIsLoopCallBack(boole) { 207 this.isLoop = boole 208 } 209 210 private getPlayStatus() { 211 return this.media.state 212 } 213 214 getPlayMode(boole) { 215 Logger.info(TAG, `getPlayMode state : ${boole}`) 216 if (boole) { 217 this.media.loop = true 218 } else { 219 this.media.loop = false 220 } 221 } 222 223 getPlay() { 224 if (typeof (this.media) != 'undefined') { 225 226 Logger.info(TAG, `getPlay success`) 227 this.media.play() 228 } 229 } 230 231 stop() { 232 if (typeof (this.media) != 'undefined') { 233 this.media.stop() 234 } 235 } 236 237 release() { 238 if (typeof (this.media) != 'undefined') { 239 this.media.release() 240 } 241 } 242 243 reset() { 244 if (typeof (this.media) != 'undefined') { 245 this.media.reset() 246 } 247 } 248 249 getPause() { 250 if (typeof (this.media) != 'undefined') { 251 this.media.pause() 252 } 253 } 254 255 getCurrentTime() { 256 if (typeof (this.media) != 'undefined') { 257 return this.media.currentTime 258 } 259 return 0 260 } 261 262 setSpeed(speed) { 263 if (typeof (this.media) != 'undefined') { 264 this.media.setSpeed(speed, (err) => { 265 if (typeof (err) == 'undefined') { 266 Logger.info(TAG, `setSpeed success`) 267 } else { 268 Logger.info(TAG, `setSpeed fail!!`) 269 } 270 }) 271 } 272 } 273 274 async seek(time) { 275 if (typeof (this.media) != 'undefined') { 276 await this.media.seek(time) 277 } 278 } 279 280 getFormat() { 281 return this.resourceAddress.mimeType 282 } 283}