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 CheckAndCreateMedia();
52 if (mediaPlayer_ == nullptr) {
53 LOGI("mediaPlayer create fail");
54 return;
55 }
56
57 if (customizedVolume != INVALID_VOLUME) {
58 SetCustomizedVolume(customizedVolume);
59 }
60
61 std::string path = GetResPath(TYPE_SOUND);
62 LOGI("sound res path: %{public}s", path.c_str());
63 int ret = mediaPlayer_->SetSource(path);
64 if (ret != 0) {
65 LOGE("PlaySound SetSource fail, errorCode: %{public}d", ret);
66 return;
67 }
68
69 ret = mediaPlayer_->SetParameter(buildMediaFormat());
70 if (ret != 0) {
71 LOGE("PlaySound SetParameter fail, errorCode:%{public}d", ret);
72 return;
73 }
74 mediaPlayer_->SetLooping(false);
75 mediaPlayer_->PrepareAsync();
76 mediaPlayer_->Play();
77 }
78 #endif
79