• 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
16import userFileManager from '@ohos.filemanagement.userFileManager';
17import media from '@ohos.multimedia.media';
18import Logger from '../model/Logger';
19
20const TAG = 'myMedia:';
21
22export class myMedia {
23  public resourceAddress: userFileManager.FileAsset = undefined;
24  public fileName: string = undefined;
25  public totalDuration: number = 0;
26  public format: string;
27  public mediaType: number;
28  private playStatus: string;
29  private playMode: string;
30  private currentNode: string;
31  private media: media.AVPlayer;
32  private surfaceId: string = '';
33  private isLoop: boolean;
34  private audioData: userFileManager.FileAsset[];
35  private videoData: userFileManager.FileAsset[];
36  private fileIndex: number = 0;
37  private mediaDataLen: number;
38  private uri: string = undefined;
39
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.fileType == userFileManager.FileType.AUDIO) {
55      this.resourceAddress = this.audioData[this.fileIndex];
56      this.init(this.resourceAddress);
57    } else if (this.resourceAddress.fileType == userFileManager.FileType.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.fileType == userFileManager.FileType.AUDIO) {
70      this.resourceAddress = this.audioData[this.fileIndex];
71      this.init(this.resourceAddress);
72    } else if (this.resourceAddress.fileType == userFileManager.FileType.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.uri === this.resourceAddress.uri) {
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    this.stop();
97    this.release();
98    this.media = await media.createAVPlayer();
99    this.setVideoCallBack(this.media);
100    this.media.url = url;
101    this.media.surfaceId = this.surfaceId;
102    Logger.info(TAG, 'httpInit success');
103    await this.media.prepare();
104    this.totalDuration = this.media.duration;
105    this.fileName = 'Http';
106    this.media.play();
107  }
108
109  async init(resourceAddress: userFileManager.FileAsset, surfaceId?) {
110    Logger.info(TAG, 'init state');
111    if (surfaceId) {
112      this.surfaceId = surfaceId.toString();
113      Logger.info(TAG, `surfaceId success: ${surfaceId}`);
114    }
115    this.stop();
116    this.release();
117    this.resourceAddress = resourceAddress;
118    Logger.info(TAG, `resourceAddress success: ${this.resourceAddress}`);
119    this.fileName = resourceAddress.displayName;
120    Logger.info(TAG, `fileName success: ${this.fileName}`);
121    this.totalDuration = resourceAddress.get('duration') as number;
122    Logger.info(TAG, `totalDuration success: ${this.totalDuration}`);
123    this.resourceAddress.open('r').then(async (fd) => {
124      Logger.info(TAG, `fd success: ${fd}`);
125      this.media = await media.createAVPlayer();
126      this.media.url = 'fd://' + fd;
127      if (this.resourceAddress.fileType == userFileManager.FileType.AUDIO) {
128        Logger.info(TAG, 'AUDIO success');
129        this.getFileIndex(this.audioData);
130        this.mediaDataLen = this.audioData.length;
131        Logger.info(TAG, `AUDIO success: ${this.media}`);
132        this.setAudioCallBack(this.media);
133      } else if (this.resourceAddress.fileType == userFileManager.FileType.VIDEO) {
134        Logger.info(TAG, 'VIDEO success');
135        this.getFileIndex(this.videoData);
136        this.mediaDataLen = this.videoData.length;
137        this.setVideoCallBack(this.media);
138        this.media.surfaceId = this.surfaceId;
139        await this.media.prepare();
140        Logger.info(TAG, 'VIDEO end');
141      }
142      this.getPlay();
143      this.getPlayMode(this.isLoop);
144    });
145  }
146
147  setAudioCallBack(audioPlayer) {
148    audioPlayer.on('dataLoad', () => {
149      Logger.info(TAG, 'AUDIO case dataLoad called');
150      Logger.info(TAG, `AUDIO audioPlayer.duration : ${audioPlayer.duration}`);
151    });
152    audioPlayer.on('pause', () => {
153      Logger.info(TAG, 'AUDIO case pause called');
154    });
155    audioPlayer.on('play', () => {
156      Logger.info(TAG, 'AUDIO case play called');
157    });
158    audioPlayer.on('stop', () => {
159      Logger.info(TAG, 'AUDIO case stop called');
160    });
161    audioPlayer.on('finish', () => {
162      Logger.info(TAG, 'AUDIO case finish called');
163      this.release();
164      if (this.resourceAddress && this.resourceAddress.fileType) {
165        this.next();
166      }
167    });
168    audioPlayer.on('error', (err) => {
169      Logger.info(TAG, ` AUDIO audioPlayer error called : ${err}`);
170    });
171  }
172
173  setVideoCallBack(videoPlayer) {
174    videoPlayer.on('prepare', () => {
175      Logger.info(TAG, 'VIDEO start');
176      Logger.info(TAG, 'VIDEO videoPlayer duration');
177    });
178
179    videoPlayer.on('playing', () => {
180      Logger.info(TAG, 'VIDEO playing finish');
181    });
182    videoPlayer.on('paused', () => {
183      Logger.info(TAG, 'VIDEO paused finish');
184    });
185    videoPlayer.on('stopped', () => {
186      Logger.info(TAG, 'VIDEO stopped finish');
187    });
188    videoPlayer.on('error', (err) => {
189      Logger.info(TAG, `VIDEO error : ${err}`);
190    });
191    videoPlayer.on('playbackCompleted', () => {
192      Logger.info(TAG, 'VIDEO playbackCompleted finish');
193      this.release();
194      if (this.resourceAddress && this.resourceAddress.fileType) {
195        this.next();
196      }
197    });
198  }
199
200  setIsLoopCallBack(boole) {
201    this.isLoop = boole;
202  }
203
204  private getPlayStatus() {
205    return this.media.state;
206  }
207
208  getPlayMode(boole) {
209    Logger.info(TAG, `getPlayMode state : ${boole}`);
210    if (boole) {
211      this.media.loop = true;
212    } else {
213      this.media.loop = false;
214    }
215  }
216
217  getPlay() {
218    if (typeof (this.media) != 'undefined') {
219      Logger.info(TAG, 'getPlay success');
220      this.media.play();
221    }
222  }
223
224  stop() {
225    if (typeof (this.media) != 'undefined') {
226      this.media.stop();
227    }
228  }
229
230  release() {
231    if (typeof (this.media) != 'undefined') {
232      this.media.release();
233    }
234  }
235
236  reset() {
237    if (typeof (this.media) != 'undefined') {
238      this.media.reset();
239    }
240  }
241
242  getPause() {
243    if (typeof (this.media) != 'undefined') {
244      this.media.pause();
245    }
246  }
247
248  getCurrentTime() {
249    if (typeof (this.media) != 'undefined') {
250      return this.media.currentTime;
251    }
252    return 0;
253  }
254
255  setSpeed(speed) {
256    if (typeof (this.media) != 'undefined') {
257      this.media.setSpeed(speed);
258      Logger.info(TAG, 'setSpeed success');
259    }
260  }
261
262  async seek(time) {
263    if (typeof (this.media) != 'undefined') {
264      await this.media.seek(time);
265    }
266  }
267}