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