1/* 2 * Copyright (c) 2021-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 audio from '@ohos.multimedia.audio'; 17import VolumeModel from '../VolumeModel'; 18import Log from '../../../../../../../common/src/main/ets/default/Log'; 19 20const TAG = 'Control-volumeComponent' 21const volumeType = audio.AudioVolumeType.MEDIA; 22 23@Component 24export struct MyVol { 25 @StorageLink('VolumeValue') volumeValue: number = 0; 26 @State volume : any = { 27 minValue: 0, 28 maxValue: 15, 29 value: 7 30 }; 31 32 aboutToAppear() { 33 this.initVolume() 34 Log.showInfo(TAG,'Finished init Volume!'); 35 VolumeModel.init(); 36 VolumeModel.registerVolume(); 37 VolumeModel.unRegisterVolume(); 38 } 39 40 aboutToDisappear () { 41 Log.showInfo(TAG,'aboutToDisappear'); 42 } 43 44 initVolume() { 45 VolumeModel.getMaxVolume(this.volume, volumeType); 46 VolumeModel.getMinVolume(this.volume, volumeType); 47 Log.showInfo(TAG, `initVolume ${this.volume.minValue} ${this.volume.maxValue} `); 48 } 49 50 setVolume(value) { 51 Log.showInfo(TAG, `setVolume = ${value}`); 52 VolumeModel.setVolume(value); 53 } 54 55 56 build() { 57 Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { 58 Column() { 59 Text($r('app.string.volume_control')) 60 .fontSize($r('app.float.control_common_font_size')).alignSelf(ItemAlign.Center) 61 62 } 63 .width('15%') 64 .constraintSize({ maxWidth: $r("app.float.font_constraint_maxSize") }) 65 .padding({ left: $r('app.float.slider_text_padding_left') }) 66 67 Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 68 Slider({ 69 value: this.volumeValue, 70 min: this.volume.minValue, 71 max: this.volume.maxValue, 72 step: 1, 73 style: SliderStyle.InSet 74 }) 75 .width('100%') 76 .blockColor(Color.Blue) 77 .trackColor(Color.Grey) 78 .selectedColor(Color.Blue) 79 .onChange((value: number) => { 80 this.volume.value = value 81 this.setVolume(this.volume) 82 }) 83 } 84 .width('85%') 85 } 86 .height('100%') 87 .width('100%') 88 .backgroundColor($r('app.color.volume_bg_color')) 89 .borderRadius($r('app.float.volume_border_radius')) 90 } 91}