1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 emitter from '@ohos.events.emitter'; 17import Logger from '../utils/Logger'; 18import { AudioInfo } from '../appsampled/data/SearchResult'; 19import AVPlayerModel from '../model/AVPlayerModel' 20import Constant from '../utils/Constant'; 21 22const TAG: string = '[SearchPlayMusicComponent]'; 23 24@Component 25export default struct SearchPlayMusicComponent { 26 private audioInfo?: AudioInfo; 27 private avPlayerModel: AVPlayerModel = new AVPlayerModel(getContext(this)); 28 @State isCollect: boolean = false; 29 @State isPlay: boolean = false; 30 @State isInit: boolean = false; 31 32 aboutToAppear() { 33 // 监听暂停事件,当有其他音乐播放时当前播放 34 emitter.on({ eventId: Constant.EVENT_PAUSED_AUDIO }, data => { 35 Logger.info(TAG, `emitter on data = ${JSON.stringify(data)}`) 36 if (data) { 37 // 拿出传过来的ID 38 let audioId: number = data.data?.audioId; 39 Logger.info(TAG, `emitter on data audioId= ${JSON.stringify(audioId)}`) 40 // 不与当前ID相同则暂停,规避自身也会暂停的问题 41 if (audioId && audioId !== this.audioInfo?.audioId) { 42 Logger.info(TAG, `emitter on data this.isPlay= ${JSON.stringify(this.isPlay)}`) 43 if (this.isPlay) { 44 this.avPlayerModel.paused(); 45 this.isPlay = false; 46 } 47 } 48 } 49 }); 50 } 51 52 build() { 53 Row() { 54 // 音乐头像 55 Stack() { 56 Image($r('app.media.app_icon')) 57 .width(74) 58 .height(74) 59 .objectFit(ImageFit.Contain) 60 .borderRadius(10) 61 Image(this.isPlay ? $r('app.media.app_icon') : $r('app.media.app_icon')) 62 .width(26) 63 .height(26) 64 .objectFit(ImageFit.Contain) 65 } 66 .id(`musicID_${this.audioInfo?.audioId}`) 67 .width(74) 68 .height(74) 69 .margin({ right: 12 }) 70 .alignContent(Alignment.Center) 71 .onClick(e => { 72 // 播放的音乐点击则暂停 73 if (this.isPlay) { 74 this.avPlayerModel.paused(); 75 } else { 76 // 播放当前音乐时发送事件暂停其他音乐播放事件 77 emitter.emit({ eventId: Constant.EVENT_PAUSED_AUDIO }, { 78 data: { 79 'audioId': this.audioInfo?.audioId 80 } 81 }); 82 // 第一次点击播放先初始化音乐 83 if (!this.isInit) { 84 this.avPlayerModel.avPlayerFdSrcDemo(this.audioInfo?.audio); 85 this.isInit = !this.isInit; 86 } else { 87 // 初始化过的直接播放 88 this.avPlayerModel.play(); 89 } 90 } 91 this.isPlay = !this.isPlay; 92 }) 93 94 // 音乐信息 95 Column({ space: 2 }) { 96 Row({ space: 2 }) { 97 Image($r('app.media.app_icon')) 98 .width(18) 99 .height(18) 100 .objectFit(ImageFit.Contain) 101 .borderRadius(10) 102 Text(this.audioInfo?.audioName) 103 .height(18) 104 .fontColor($r('app.color.COLOR_FFFFFF')) 105 .fontSize(16) 106 .fontFamily($r('app.string.Font_family_medium')) 107 .textAlign(TextAlign.Start) 108 } 109 .height(30) 110 111 Text(this.audioInfo?.audioAuthorName) 112 .height(18) 113 .fontColor($r('app.color.COLOR_CCFFFFFF')) 114 .fontSize(14) 115 .fontFamily($r('app.string.Font_family_regular')) 116 .textAlign(TextAlign.Start) 117 118 Row({ space: 5 }) { 119 Text(this.audioInfo?.audioTime) 120 .height(18) 121 .fontColor($r('app.color.COLOR_CCFFFFFF')) 122 .fontSize(14) 123 .fontFamily($r('app.string.Font_family_regular')) 124 .textAlign(TextAlign.Start) 125 Text() 126 .width(2) 127 .height(2) 128 .backgroundColor($r('app.color.COLOR_CCFFFFFF')) 129 .borderRadius(1) 130 Text(this.audioInfo?.audioNum) 131 .height(18) 132 .fontColor($r('app.color.COLOR_CCFFFFFF')) 133 .fontSize(14) 134 .fontFamily($r('app.string.Font_family_regular')) 135 .textAlign(TextAlign.Center) 136 } 137 .height(20) 138 } 139 .height(70) 140 .alignItems(HorizontalAlign.Start) 141 142 Blank() 143 144 Column() { 145 Image(this.isCollect ? $r('app.media.app_icon') : $r('app.media.app_icon')) 146 .width(20) 147 .height(20) 148 .objectFit(ImageFit.Contain) 149 .margin({ right: 12 }) 150 } 151 .width(40) 152 .height(40) 153 .justifyContent(FlexAlign.Center) 154 .alignItems(HorizontalAlign.Center) 155 .onClick(e => { 156 this.isCollect = !this.isCollect; 157 }) 158 } 159 .height(90) 160 .width('100%') 161 } 162}