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 worker from '@ohos.worker'; 17import media from '@ohos.multimedia.media'; 18import Log from "@ohos.hilog"; 19 20/** 21 * dailpad key tone worker 22 */ 23const parentPort = worker.parentPort; 24const TAG = "ContactLog"; 25const DOMAIN = 0x0900; 26let audioPlayer = undefined; 27let mAudioPath = ''; 28 29parentPort.onmessage = function (e) { 30 let data = e.data; 31 switch (data.type) { 32 case "audio": 33 if (audioPlayer == undefined) { 34 audioPlayer = media.createAudioPlayer(); 35 audioPlayer.on('dataLoad', () => { 36 audioPlayer.play(); 37 }); 38 } 39 let fileName = data.data; 40 console.log(`fileName is ${JSON.stringify(fileName)}`) 41 if (mAudioPath == '') { 42 audioPlayer.fdSrc = fileName; 43 } else if (mAudioPath == fileName) { 44 audioPlayer.seek(0); 45 audioPlayer.play(); 46 } else { 47 audioPlayer.reset(); 48 audioPlayer.fdSrc = fileName; 49 } 50 mAudioPath = fileName; 51 break; 52 case "over": 53 audioPlayer.release(); 54 audioPlayer = undefined; 55 parentPort.close(); 56 break; 57 default: 58 Log.error(DOMAIN, TAG, 'the case is not in this category'); 59 break; 60 } 61}