• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import media from '@ohos.multimedia.media'
17import * as mediaTestBase from './MediaTestBase.js';
18
19export function playAudioSource(src, duration, playTime, checkSeekTime, done) {
20    console.info(`case media source url: ${src}`)
21    let volumeChanged = false;
22    let playCount = 0;
23    let pauseCount = 0;
24    let stopCount = 0;
25    let seekCount = 0;
26    let seekEOS = false;
27    let audioPlayer = media.createAudioPlayer();
28    if (audioPlayer == null) {
29        console.error('case createAudioPlayer failed');
30        expect().assertFail();
31        done();
32    }
33    if (typeof (src) == 'string') {
34        console.error('case src test');
35        audioPlayer.src = src;
36    } else {
37        console.error('case fdsrc test');
38        audioPlayer.fdSrc = src;
39    }
40    audioPlayer.on('dataLoad', () => {
41        console.info('case set source success');
42        expect(audioPlayer.state).assertEqual('idle');
43        expect(audioPlayer.currentTime).assertEqual(0);
44        expect(Math.abs(audioPlayer.duration - duration)).assertLess(500);
45        // step 0: dataLoad -> play
46        audioPlayer.play();
47    });
48    audioPlayer.on('play', () => {
49        console.info('case start to play');
50        expect(audioPlayer.state).assertEqual('playing');
51        playCount++;
52        if (playCount == 1) {
53            // step 1: play -> seek duration/3
54            mediaTestBase.msleep(playTime);
55            audioPlayer.seek(audioPlayer.duration / 3);
56            // step 2: seek duration/3 -> pause
57            mediaTestBase.msleep(playTime);
58            audioPlayer.pause();
59        } else if (playCount == 2) {
60            // step 5: play -> seek duration when loop is true
61            audioPlayer.loop = true;
62            audioPlayer.seek(audioPlayer.duration);
63            // step 6: seek duration -> setVolume + seek duration when loop is false
64            mediaTestBase.msleep(playTime);
65            console.info('case state 2 is :' + audioPlayer.state);
66            expect(audioPlayer.state).assertEqual('playing');
67            audioPlayer.loop = false;
68            audioPlayer.setVolume(0.5);
69            audioPlayer.seek(audioPlayer.duration);
70            seekEOS = true;
71            // step 7: wait for finish
72            mediaTestBase.msleep(playTime);
73        } else if (playCount == 3) {
74            // step 9: play -> stop
75            audioPlayer.stop();
76        } else {
77            // step 12: play -> pause
78            audioPlayer.pause();
79        }
80    });
81     audioPlayer.on('pause', () => {
82        console.info('case now is paused');
83        expect(audioPlayer.state).assertEqual('paused');
84        pauseCount++;
85        if (pauseCount == 1) {
86            // step 3: pause -> seek 0
87            audioPlayer.seek(0);
88            // step 4: seek 0 -> play
89            audioPlayer.play();
90        } else {
91            // step 13: pause -> stop
92            audioPlayer.stop();
93        }
94    });
95    audioPlayer.on('stop', () => {
96        console.info('case stop success');
97        expect(audioPlayer.state).assertEqual('stopped');
98        stopCount++;
99        if (stopCount == 1) {
100            // step 10: stop -> reset
101            audioPlayer.reset();
102        } else {
103            // step 14: stop -> release
104            expect(volumeChanged).assertEqual(true);
105            audioPlayer.release();
106            done();
107        }
108    });
109    audioPlayer.on('reset', () => {
110        console.info('case reset success');
111        expect(audioPlayer.state).assertEqual('idle');
112        // step 11: reset -> dataLoad
113        if (typeof (src) == 'string') {
114            console.error('case src test');
115            audioPlayer.src = src;
116        } else {
117            console.error('case fdsrc test');
118            audioPlayer.fdSrc = src;
119        }
120    });
121    audioPlayer.on('timeUpdate', (seekDoneTime) => {
122        seekCount++;
123        if (seekDoneTime == null) {
124            console.info(`case seek filed`);
125            audioPlayer.release();
126            expect().assertFail();
127            done();
128            return;
129        }
130        console.info('case timeUpdate success, and timeUpdate is ' + seekDoneTime);
131    });
132    audioPlayer.on('volumeChange', () => {
133        console.info('case set volume success ');
134        volumeChanged = true;
135    });
136    audioPlayer.on('finish', () => {
137        console.info('case play end');
138        expect(audioPlayer.state).assertEqual('stopped');
139        expect(seekEOS).assertTrue();
140        // step 8: play when stream is end
141        audioPlayer.play();
142    });
143    audioPlayer.on('error', (err) => {
144        console.error(`case error called,errMessage is ${err.message}`);
145        audioPlayer.release();
146        expect().assertFail();
147        done();
148    });
149}