AudioPlayerTestBase.js 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import media from '@ohos.multimedia.media'
import * as mediaTestBase from './MediaTestBase.js';

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