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            audioPlayer.seek(audioPlayer.duration / 3);
55            // step 2: seek duration/3 -> pause
56            audioPlayer.pause();
57        } else if (playCount == 2) {
58            // step 5: play -> seek duration when loop is true
59            audioPlayer.loop = true;
60            audioPlayer.seek(audioPlayer.duration);
61            // step 6: seek duration -> setVolume + seek duration when loop is false
62            console.info('case state 2 is :' + audioPlayer.state);
63            expect(audioPlayer.state).assertEqual('playing');
64            audioPlayer.loop = false;
65            audioPlayer.setVolume(0.5);
66            audioPlayer.seek(audioPlayer.duration);
67            seekEOS = true;
68            // step 7: wait for finish
69        } else if (playCount == 3) {
70            // step 9: play -> stop
71            audioPlayer.stop();
72        } else {
73            // step 12: play -> pause
74            audioPlayer.pause();
75        }
76    });
77     audioPlayer.on('pause', () => {
78        console.info('case now is paused');
79        expect(audioPlayer.state).assertEqual('paused');
80        pauseCount++;
81        if (pauseCount == 1) {
82            // step 3: pause -> seek 0
83            audioPlayer.seek(0);
84            // step 4: seek 0 -> play
85            audioPlayer.play();
86        } else {
87            // step 13: pause -> stop
88            audioPlayer.stop();
89        }
90    });
91    audioPlayer.on('stop', () => {
92        console.info('case stop success');
93        expect(audioPlayer.state).assertEqual('stopped');
94        stopCount++;
95        if (stopCount == 1) {
96            // step 10: stop -> reset
97            audioPlayer.reset();
98        } else {
99            // step 14: stop -> release
100            expect(volumeChanged).assertEqual(true);
101            audioPlayer.release();
102            done();
103        }
104    });
105    audioPlayer.on('reset', () => {
106        console.info('case reset success');
107        expect(audioPlayer.state).assertEqual('idle');
108        // step 11: reset -> dataLoad
109        if (typeof (src) == 'string') {
110            console.error('case src test');
111            audioPlayer.src = src;
112        } else {
113            console.error('case fdsrc test');
114            audioPlayer.fdSrc = src;
115        }
116    });
117    audioPlayer.on('volumeChange', () => {
118        console.info('case set volume success ');
119        volumeChanged = true;
120    });
121    audioPlayer.on('finish', () => {
122        console.info('case play end');
123        expect(audioPlayer.state).assertEqual('stopped');
124        expect(seekEOS).assertTrue();
125        // step 8: play when stream is end
126        audioPlayer.play();
127    });
128    audioPlayer.on('error', (err) => {
129        console.error(`case error called,errMessage is ${err.message}`);
130        audioPlayer.release();
131        expect().assertFail();
132        done();
133    });
134}