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 #include <parameters.h>
21 #include <util.h>
22
23 using namespace OHOS;
BootSoundPlayer(const PlayerParams & params)24 BootSoundPlayer::BootSoundPlayer(const PlayerParams& params)
25 {
26 resPath_ = params.resPath;
27 screenId_ = params.screenId;
28 isSoundEnabled_ = params.soundEnabled;
29 }
30
31 #ifdef PLAYER_FRAMEWORK_ENABLE
Play()32 void BootSoundPlayer::Play()
33 {
34 LOGI("PlaySound start");
35 if (!isSoundEnabled_) {
36 LOGI("sound disabled on screen: " BPUBU64 "", screenId_);
37 return;
38 }
39 bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
40 if (!bootSoundEnabled) {
41 LOGI("boot animation sound disabled");
42 return;
43 }
44
45 int customizedVolume = system::GetIntParameter(BOOT_SOUND, INVALID_VOLUME, MIN_VOLUME, MAX_VOLUME);
46 if (customizedVolume == MIN_VOLUME) {
47 LOGI("boot animation sound set volume 0");
48 return;
49 }
50
51 int64_t startTime = GetSystemCurrentTime();
52 int64_t endTime = startTime;
53 while ((endTime - startTime) < MAX_WAIT_MEDIA_CREATE_TIME
54 && (mediaPlayer_ = Media::PlayerFactory::CreatePlayer()) == nullptr) {
55 endTime = GetSystemCurrentTime();
56 usleep(SLEEP_TIME_US);
57 LOGI("mediaPlayer is nullptr, try create again");
58 }
59
60 if (mediaPlayer_ == nullptr) {
61 LOGI("mediaPlayer create fail");
62 return;
63 }
64
65 if (customizedVolume != INVALID_VOLUME) {
66 SetCustomizedVolume(customizedVolume);
67 }
68
69 std::string path = GetResPath(TYPE_SOUND);
70 LOGI("sound res path: %{public}s", path.c_str());
71 int ret = mediaPlayer_->SetSource(path);
72 if (ret != 0) {
73 LOGE("PlaySound SetSource fail, errorCode: %{public}d", ret);
74 return;
75 }
76
77 ret = mediaPlayer_->SetParameter(buildMediaFormat());
78 if (ret != 0) {
79 LOGE("PlaySound SetParameter fail, errorCode:%{public}d", ret);
80 return;
81 }
82 mediaPlayer_->SetLooping(false);
83 mediaPlayer_->PrepareAsync();
84 mediaPlayer_->Play();
85 }
86 #endif
87