1 /*
2  * Copyright (c) 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 #include "boot_sound_player.h"
16 
17 #include "boot_animation_utils.h"
18 #include "log.h"
19 #include <media_errors.h>
20 
21 using namespace OHOS;
BootSoundPlayer(const PlayerParams & params)22 BootSoundPlayer::BootSoundPlayer(const PlayerParams& params)
23 {
24     resPath_ = params.resPath;
25     screenId_ = params.screenId;
26     isSoundEnabled_ = params.soundEnabled;
27 }
28 
29 #ifdef PLAYER_FRAMEWORK_ENABLE
Play()30 void BootSoundPlayer::Play()
31 {
32     LOGI("PlaySound start");
33     if (!isSoundEnabled_) {
34         LOGI("sound disabled on screen: " BPUBU64 "", screenId_);
35         return;
36     }
37     bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
38     if (!bootSoundEnabled) {
39         LOGI("boot animation sound disabled");
40         return;
41     }
42 
43     int waitMediaCreateTime = 0;
44     while ((mediaPlayer_ = Media::PlayerFactory::CreatePlayer()) == nullptr
45         && waitMediaCreateTime < MAX_WAIT_MEDIA_CREATE_TIME) {
46         LOGI("mediaPlayer is nullptr, try create again");
47         usleep(SLEEP_TIME_US);
48         waitMediaCreateTime += SLEEP_TIME_US;
49     }
50 
51     if (mediaPlayer_ == nullptr) {
52         LOGI("mediaPlayer create fail");
53         return;
54     }
55 
56     std::string path = GetResPath(TYPE_SOUND);
57     LOGI("sound res path: %{public}s", path.c_str());
58     mediaPlayer_->SetSource(path);
59     mediaPlayer_->SetLooping(false);
60     mediaPlayer_->PrepareAsync();
61     mediaPlayer_->Play();
62 }
63 #endif
64