• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (C) 2023 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 common from '@ohos.app.ability.common'
17import fs from '@ohos.file.fs'
18import audio from '@ohos.multimedia.audio'
19import router from '@ohos.router'
20import { BusinessError } from '@ohos.base'
21
22export class ModeType {
23  id: number;
24  name: Resource;
25
26  constructor(id: number, name: Resource) {
27    this.id = id;
28    this.name = name;
29  }
30}
31
32class fileDescriptorClass {
33  fd: number;
34  offset: number;
35  length: number;
36  constructor(fd: number, offset: number, length: number) {
37    this.fd = fd;
38    this.offset = offset;
39    this.length = length;
40  }
41}
42
43
44@Component
45struct ModeItem_1 {
46  private mode: ModeType = new ModeType(-1, $r('app.string.CONTENT_TYPE_UNKNOWN'));
47  @Consume contentTypeIndex?: number;
48
49  @Builder renderModeItem(fontColor: string, bgColor: string, value: Resource) {
50    Flex() {
51      Text(value)
52        .fontSize(16)
53        .fontWeight(400)
54        .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
55        .fontColor(fontColor);
56    }
57    .height(44)
58    .width(148)
59    .backgroundColor(bgColor)
60    .borderRadius(12)
61    .padding({ left: 14, top: 14, bottom: 14 });
62
63    if (this.mode && this.mode.id !== 5) {
64      Flex() {
65        Flex() {
66        }.height(1).width('100%')
67        .backgroundColor('#F3F3F3');
68      }
69      .padding({ left: 12, right: 12 });
70    }
71  }
72
73  build() {
74    Flex({ direction: FlexDirection.Column }) {
75      if (this.contentTypeIndex == this.mode.id) {
76        this.renderModeItem('#007DFF', 'rgba(0,125,255,0.20)', this.mode.name);
77      } else {
78        this.renderModeItem('rgba(0,0,0,0.9)', '', this.mode.name);
79      }
80    }.height(48).width(156);
81  }
82}
83
84@Component
85struct ModeItem_2 {
86  private mode : ModeType = new ModeType(-1, $r('app.string.CONTENT_TYPE_UNKNOWN'));
87  @Consume streamUsageIndex?: number;
88
89  @Builder renderModeItem(fontColor: string, bgColor: string, value: Resource) {
90    Flex() {
91      Text(value)
92        .fontSize(16)
93        .fontColor(fontColor)
94        .fontWeight(400)
95        .fontFamily($r('sys.string.ohos_id_text_font_family_medium'));
96    }
97    .height(44)
98    .width(148)
99    .backgroundColor(bgColor)
100    .borderRadius(12)
101    .padding({ left: 14, top: 14, bottom: 14 });
102
103    if (this.mode && this.mode.id !== 14) {
104      Flex() {
105        Flex() {
106        }.height(1).width('100%').backgroundColor('#F3F3F3');
107      }
108      .padding({ left: 12, right: 12 });
109    }
110  }
111
112  build() {
113    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
114      if (this.streamUsageIndex == this.mode.id) {
115        this.renderModeItem('#007DFF', 'rgba(0,125,255,0.20)', this.mode.name);
116      } else {
117        this.renderModeItem('rgba(0,0,0,0.9)', '', this.mode.name);
118      }
119    }.height(48).width(156);
120  }
121}
122
123@Component
124struct ModeItem_3 {
125  private mode : ModeType = new ModeType(-1, $r('app.string.CONTENT_TYPE_UNKNOWN'));
126  @Consume audioEffectModeSettingIndex?: number;
127
128  @Builder renderModeItem(fontColor: string, bgColor: string, value: Resource) {
129    Flex() {
130      Text(value)
131        .fontSize(16)
132        .fontWeight(400)
133        .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
134        .fontColor(fontColor);
135    }
136    .height(44)
137    .width(148)
138    .backgroundColor(bgColor)
139    .borderRadius(12)
140    .padding({ left: 14, top: 14, bottom: 14 });
141
142    if (this.mode && this.mode.id !== 1) {
143      Flex() {
144        Flex() {
145        }.height(1).width('100%')
146        .backgroundColor('#F3F3F3');
147      }
148      .padding({ left: 12, right: 12 });
149    }
150  }
151
152  build() {
153    Flex({ direction: FlexDirection.Column }) {
154      if (this.audioEffectModeSettingIndex == this.mode.id) {
155        this.renderModeItem('#007DFF', 'rgba(0,125,255,0.20)', this.mode.name);
156      } else {
157        this.renderModeItem('rgba(0,0,0,0.9)', '', this.mode.name);
158      }
159    }.height(48).width(156);
160  }
161}
162
163@Entry
164@Component
165struct RealtimeAudioEffectQuery {
166  scroller: Scroller = new Scroller();
167  private audioSource = 'test1.wav';
168  private appContext?: common.Context;
169  private fileDescriptor = new fileDescriptorClass(0,0,0);
170  private audioPlayState: Array<string> = ['notPlaying', 'onPlaying'];
171  private stateImg: Array<Resource> = [$r('app.media.ic_play'), $r('app.media.ic_pause')];
172  private audioRenderer?: audio.AudioRenderer;
173  private contentTypeOptions : ModeType[] = [
174    new ModeType(0, $r('app.string.CONTENT_TYPE_UNKNOWN')),
175    new ModeType(1, $r('app.string.CONTENT_TYPE_SPEECH')),
176    new ModeType(2, $r('app.string.CONTENT_TYPE_MUSIC')),
177    new ModeType(3, $r('app.string.CONTENT_TYPE_MOVIE')),
178    new ModeType(4, $r('app.string.CONTENT_TYPE_SONIFICATION')),
179    new ModeType(5, $r('app.string.CONTENT_TYPE_RINGTONE'))
180  ];
181
182  private streamUsageOptions : ModeType[]= [
183    new ModeType(0, $r('app.string.STREAM_USAGE_UNKNOWN')),
184    new ModeType(1, $r('app.string.STREAM_USAGE_MEDIA')),
185    new ModeType(2, $r('app.string.STREAM_USAGE_MUSIC')),
186    new ModeType(3, $r('app.string.STREAM_USAGE_VOICE_COMMUNICATION')),
187    new ModeType(4, $r('app.string.STREAM_USAGE_VOICE_ASSISTANT')),
188    new ModeType(5, $r('app.string.STREAM_USAGE_ALARM')),
189    new ModeType(6, $r('app.string.STREAM_USAGE_VOICE_MESSAGE')),
190    new ModeType(7, $r('app.string.STREAM_USAGE_NOTIFICATION_RINGTONE')),
191    new ModeType(8, $r('app.string.STREAM_USAGE_RINGTONE')),
192    new ModeType(9, $r('app.string.STREAM_USAGE_NOTIFICATION')),
193    new ModeType(10, $r('app.string.STREAM_USAGE_ACCESSIBILITY')),
194    new ModeType(11, $r('app.string.STREAM_USAGE_MOVIE')),
195    new ModeType(12, $r('app.string.STREAM_USAGE_GAME')),
196    new ModeType(13, $r('app.string.STREAM_USAGE_AUDIOBOOK')),
197    new ModeType(14, $r('app.string.STREAM_USAGE_NAVIGATION'))
198  ];
199
200  private audioEffectModeSettingOptions : ModeType[] = [
201    new ModeType(0, $r('app.string.EFFECT_NONE')),
202    new ModeType(1, $r('app.string.EFFECT_DEFAULT'))
203  ];
204
205  @State showSelector_1: boolean = false;
206  @State showSelector_2: boolean = false;
207  @State showSelector_3: boolean = false;
208  @State queryResult: Resource = $r('app.string.BLANK');
209  @State queryButtonState: boolean = false;
210  @State stateTextIndex: number = 0;
211  @State stateImgIndex: number = 0;
212  @State optionsButtonState: boolean = true;
213  @State currentAudioEffect: Resource = $r('app.string.EFFECT_DEFAULT');
214  @State selectColor: Color = Color.Black;
215  @Provide audioEffectModeSettingIndex: number = 1;
216  @Provide contentTypeIndex: number = 0;
217  @Provide streamUsageIndex: number = 0;
218
219  async getStageFileDescriptor(fileName: string): Promise<fileDescriptorClass> {
220    let fileDescriptor = new fileDescriptorClass(0,0,0);
221    this.appContext = getContext();
222    let mgr = this.appContext.resourceManager;
223    await mgr.getRawFd(fileName).then(value => {
224      fileDescriptor = new fileDescriptorClass(value.fd, value.offset, value.length)
225      console.log('case getRawFileDescriptor success filefd: ' + value.fd)
226    }).catch((error: string) => {
227      console.log('case getRawFileDescriptor err: ' + error)
228    });
229    return fileDescriptor;
230  }
231
232  async aboutToAppear(): Promise<void> {
233    this.appContext = getContext();
234  }
235
236  onBackPress(): void {
237    if (this.audioRenderer !== undefined) {
238      try {
239        this.audioRenderer.stop();
240        this.audioRenderer.release();
241        console.info("release:SUCCEED");
242      } catch (err) {
243        let error = err as BusinessError;
244        console.error(`audioRenderer release ,Error: ${JSON.stringify(error)}`);
245        return;
246      }
247    }
248  }
249
250  async onPageHide(): Promise<void> {
251    if (this.audioRenderer !== undefined) {
252      if (this.audioRenderer.state === audio.AudioState.STATE_RUNNING) {
253        await this.audioRenderer.pause();
254        this.stateImgIndex = 0;
255        this.stateTextIndex = 0;
256        console.info('The audio renderer has been paused.');
257        this.optionsButtonState = true;
258      }
259    }
260  }
261
262  mapContentIndexToType(index: number): number | undefined {
263    // map the index of options to the content type in @ohos.multimedia.audio
264    let index2Content = new Map([
265      [0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5]
266    ]);
267    console.info("get content type:" + index2Content.get(index));
268    return index2Content.get(index);
269  }
270
271  mapStreamIndexToUsage(index: number): number | undefined {
272    // map the index of options to the stream usage in @ohos.multimedia.audio
273    let index2Usage = new Map([
274      [0, 0], [1, 1], [2, 1], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6],
275      [8, 6], [9, 7], [10, 8], [11, 10], [12, 11], [13, 12], [14, 13]
276    ]);
277    console.info("get stream usage:" + index2Usage.get(index));
278    return index2Usage.get(index);
279  }
280
281  async createAudioEffectAndPlay(): Promise<void> {
282    let audioRendererOptions : audio.AudioRendererOptions =
283      {
284        streamInfo: {
285          samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
286          channels: audio.AudioChannel.CHANNEL_2,
287          sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
288          encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
289        },
290        rendererInfo: {
291          content: this.mapContentIndexToType(this.contentTypeIndex) as audio.ContentType,
292          usage: this.mapStreamIndexToUsage(this.streamUsageIndex) as audio.StreamUsage,
293          rendererFlags: 0
294        }
295      };
296    try {
297      this.audioRenderer = await audio.createAudioRenderer(audioRendererOptions);
298      console.info("createAudioRenderer:SUCCEED");
299    } catch (err) {
300      let error = err as BusinessError;
301      console.error(`audioRenderer create ,Error: ${JSON.stringify(error)}`);
302      return;
303    }
304
305    try {
306      await this.audioRenderer.setAudioEffectMode(this.audioEffectModeSettingIndex);
307      console.info(" audioRenderer.setAudioEffectMode:SUCCEED");
308    } catch (err) {
309      let error = err as BusinessError;
310      console.error(`audioRenderer setAudioEffectMode ,Error: ${JSON.stringify(error)}`);
311      return;
312    }
313
314    try {
315      await this.audioRenderer.start();
316      console.info(" audioRenderer.start:SUCCEED");
317    } catch (err) {
318      let error = err as BusinessError;
319      console.error(`audioRenderer start ,Error: ${JSON.stringify(error)}`);
320      return;
321    }
322
323    let bufferSize : number = 0;
324    try {
325      bufferSize = await this.audioRenderer.getBufferSize();
326      console.info(" get Buffer Size:SUCCEED");
327    } catch (err) {
328      let error = err as BusinessError;
329      console.error(`get Buffer Size ,Error: ${JSON.stringify(error)}`);
330      return;
331    }
332    console.info("buffer size:" + bufferSize);
333    await this.getStageFileDescriptor(this.audioSource).then((res) => {
334      this.fileDescriptor = res;
335    })
336
337    let buf = new ArrayBuffer(bufferSize);
338    let startOffset : number = this.fileDescriptor.offset;
339    let cur = startOffset + bufferSize;
340    while (true) {
341      // when render released,state is changed to STATE_RELEASED
342      if (!this.audioRenderer) {
343        break;
344      }
345
346      if (this.audioRenderer.state === audio.AudioState.STATE_RELEASED) {
347        break;
348      }
349      while (cur <= startOffset + this.fileDescriptor.length - bufferSize) {
350        class options {
351          offset: number = 0;
352          length: number = 0;
353        }
354        let readOptions: options = {
355          offset: cur,
356          length: bufferSize
357        }
358        await fs.read(this.fileDescriptor.fd, buf, readOptions);
359        try {
360          await this.audioRenderer.write(buf);
361        } catch (err) {
362          let error = err as BusinessError;
363          console.error(`audioRenderer write ,Error: ${JSON.stringify(error)}`);
364          return;
365        }
366
367        cur += bufferSize;
368      }
369      cur = startOffset + bufferSize;
370    }
371  }
372
373  async playOrPause(): Promise<void> {
374    if (this.stateImgIndex === 0) {
375      if (this.audioRenderer === undefined || this.audioRenderer.state === audio.AudioState.STATE_RELEASED) {
376        this.createAudioEffectAndPlay();
377        this.stateImgIndex = 1;
378        this.stateTextIndex = 1;
379        this.optionsButtonState = false;
380        this.selectColor = Color.Gray;
381        this.queryButtonState = true;
382      } else {
383        let previousARInfo : audio.AudioRendererInfo = await this.audioRenderer.getRendererInfo();
384        let previousContent = previousARInfo.content;
385        let previousUsage = previousARInfo.usage;
386        if (this.mapContentIndexToType(this.contentTypeIndex) != previousContent
387          || this.mapStreamIndexToUsage(this.streamUsageIndex) != previousUsage) {
388          try {
389            await this.audioRenderer.stop();
390            await this.audioRenderer.release();
391          } catch (err) {
392            let error = err as BusinessError;
393            console.error(`audioRenderer release ,Error: ${JSON.stringify(error)}`);
394            return;
395          }
396          this.stateImgIndex = 1;
397          this.stateTextIndex = 1;
398          this.optionsButtonState = false;
399          this.selectColor = Color.Gray;
400          this.queryButtonState = true;
401          this.createAudioEffectAndPlay();
402        } else {
403          await this.audioRenderer.start();
404          this.stateImgIndex = 1;
405          this.stateTextIndex = 1;
406          this.optionsButtonState = false;
407          this.selectColor = Color.Gray;
408          this.queryButtonState = true;
409        }
410      }
411    } else {
412      console.info('Try to pause');
413      if (this.audioRenderer === undefined ){
414        return;
415      }
416      try {
417        await this.audioRenderer.pause();
418      } catch (err) {
419        let error = err as BusinessError;
420        console.error(`audioRenderer pause ,Error: ${JSON.stringify(error)}`);
421        return;
422      }
423      this.stateImgIndex = 0;
424      this.stateTextIndex = 0;
425      console.info('The audio renderer has been paused.');
426      this.optionsButtonState = true;
427      this.selectColor = Color.Black;
428    }
429  }
430
431  build() {
432    Column() {
433      Column() {
434        Row() {
435          Navigation() {
436            NavRouter() {
437              NavDestination() {
438              }
439            }.onStateChange(async (isActivated: boolean) => {
440              console.info("hello");
441              if (this.audioRenderer !== undefined) {
442                try {
443                  await this.audioRenderer.stop();
444                  await this.audioRenderer.release();
445                } catch (err) {
446                  let error = err as BusinessError;
447                  console.error(`audioRenderer release ,Error: ${JSON.stringify(error)}`);
448                  return;
449                }
450              }
451              await router.pushUrl({ url: 'pages/Index' });
452            })
453          }
454          .id('back_btn_realtime')
455          .height(56)
456          .width(384)
457          .hideBackButton(false)
458          .titleMode(NavigationTitleMode.Mini)
459          .title($r('app.string.EffectManager'))
460          .mode(NavigationMode.Stack)
461          .backgroundColor('#F1F3F5');
462        }
463
464        Row() {
465          Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
466            Row() {
467              Text(this.contentTypeOptions[this.contentTypeIndex].name)
468                .textAlign(TextAlign.Center)
469                .textOverflow({ overflow: TextOverflow.None })
470                .fontColor(this.selectColor)
471                .fontSize(16)
472                .fontWeight(500)
473                .fontFamily($r('sys.string.ohos_id_text_font_family_medium'));
474              if (this.showSelector_1) {
475                Image($r('app.media.ic_arrow_up_big')).height(7).width(10)
476                  .fillColor(this.selectColor)
477                  .margin({ left: 8 });
478              } else {
479                Image($r('app.media.ic_arrow_down_big')).height(7).width(10)
480                  .fillColor(this.selectColor)
481                  .margin({ left: 8 });
482              }
483            }
484          }
485          .height(40)
486          .width(140)
487          .backgroundColor('#F1F3F5')
488          .borderRadius(16)
489          .margin({ left: 37, right: 6 })
490          .padding({
491            left: 16,
492            right: 16
493          })
494          .enabled(this.optionsButtonState)
495          .id('select_content_realtime')
496          .onClick(() => {
497            this.showSelector_1 = !this.showSelector_1
498            this.showSelector_2 = false
499            this.showSelector_3 = false
500          });
501
502          if (this.showSelector_1) {
503            Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) {
504              ForEach(this.contentTypeOptions, (item:ModeType) => {
505                Flex() {
506                  ModeItem_1({ mode: item });
507                }.width(156)
508                .onClick(() => {
509                  if (this.contentTypeIndex !== item.id) {
510                    this.contentTypeIndex = item.id;
511                    console.info('this.contentTypeIndex===' + this.contentTypeIndex);
512                    this.queryResult = $r('app.string.BLANK');
513                    this.queryButtonState = false;
514                    this.audioEffectModeSettingIndex = 1;
515                  }
516                  this.showSelector_1 = false;
517                })
518              }, (item:ModeType) => item.id.toString());
519            }
520            .height(296)
521            .width(156)
522            .backgroundColor('#fff')
523            .borderRadius(16)
524            .shadow({ radius: 50, color: 'rgba(0,0,30,0.1500)' })
525            .padding({ left: 4, right: 4, top: 4, bottom: 4 })
526            .position({ x: 54, y: 40 })
527            .zIndex(1);
528          }
529
530          Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
531            Row() {
532              Text(this.streamUsageOptions[this.streamUsageIndex].name)
533                .textAlign(TextAlign.Center)
534                .fontSize(16)
535                .fontWeight(500)
536                .fontColor(this.selectColor)
537                .fontFamily($r('sys.string.ohos_id_text_font_family_medium'));
538              if (this.showSelector_2) {
539                Image($r('app.media.ic_arrow_up_small')).height(7).width(10)
540                  .fillColor(this.selectColor)
541                  .margin({ left: 8 });
542              } else {
543                Image($r('app.media.ic_arrow_down_small')).height(7).width(10)
544                  .fillColor(this.selectColor)
545                  .margin({ left: 8 });
546              }
547            }
548          }
549          .height(40)
550          .width(140)
551          .margin({ right: 37 })
552          .backgroundColor('#F1F3F5')
553          .borderRadius(16)
554          .padding({
555            left: 16,
556            right: 16
557          })
558          .enabled(this.optionsButtonState)
559          .id('select_usage_realtime')
560          .onClick(() => {
561            this.showSelector_1 = false;
562            this.showSelector_2 = !this.showSelector_2;
563            this.showSelector_3 = false;
564          })
565
566          if (this.showSelector_2) {
567            Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) {
568              Scroll(this.scroller) {
569                Column() {
570                  ForEach(this.streamUsageOptions, (item:ModeType) => {
571                    Flex() {
572                      ModeItem_2({ mode: item });
573                    }.width(156)
574                    .onClick(() => {
575                      if (this.streamUsageIndex !== item.id) {
576                        this.streamUsageIndex = item.id;
577                        console.info('this.streamUsageIndex===' + this.streamUsageIndex);
578                        this.queryResult = $r('app.string.BLANK');
579                        this.queryButtonState = false;
580                        this.audioEffectModeSettingIndex = 1;
581                      }
582                      this.showSelector_2 = false;
583                    })
584                  }, (item:ModeType) => item.id.toString());
585
586                }
587              }
588              .scrollable(ScrollDirection.Vertical)
589              .scrollBar(BarState.On)
590              .scrollBarColor(Color.Gray)
591              .scrollBarWidth(10)
592              .edgeEffect(EdgeEffect.None);
593            }
594            .height(344)
595            .width(156)
596            .backgroundColor('#fff')
597            .borderRadius(16)
598            .shadow({ radius: 50, color: 'rgba(0,0,30,0.1500)' })
599            .padding({ left: 4, right: 4, top: 4, bottom: 4 })
600            .position({ x: 145, y: 40 })
601            .zIndex(1);
602          }
603        }.zIndex(1);
604
605        Column() {
606          Row() {
607            Row() {
608              Image(this.stateImg[this.stateImgIndex])
609                .height(36)
610                .width(36)
611                .id('play_pause_realtime')
612                .onClick(async () => {
613                  this.showSelector_1 = false
614                  this.showSelector_2 = false
615                  this.showSelector_3 = false
616                  await this.playOrPause()
617                });
618              Text($r('app.string.CURRENT_AUDIO_EFFECT'))
619                .fontSize(16)
620                .fontWeight(500)
621                .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
622                .width(112)
623                .height(20)
624                .margin({ left: 12 });
625              Text(this.audioPlayState[this.stateTextIndex])
626                .fontSize(2)
627                .height(20)
628                .width(5)
629                .fontColor(Color.White)
630                .id('audio_play_state');
631            }
632
633            Button($r('app.string.QUERY'), { type: ButtonType.Capsule, stateEffect: true })
634              .backgroundColor('rgba(24,36,49,0.05)')
635              .fontColor('#007DFF')
636              .fontSize(12)
637              .fontWeight(500)
638              .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
639              .height(28)
640              .width(72)
641              .id('query_btn_realtime')
642              .enabled(this.queryButtonState)
643              .onClick(async () => {
644                this.showSelector_1 = false;
645                this.showSelector_2 = false;
646                this.showSelector_3 = false;
647                if (this.audioRenderer !== undefined) {
648                  if (this.audioRenderer.state === audio.AudioState.STATE_RUNNING
649                    || this.audioRenderer.state === audio.AudioState.STATE_PAUSED) {
650                    console.info('Cur audio effect query Button onClick')
651                    let audioEffectMode = 1;
652                    try {
653                      audioEffectMode = await this.audioRenderer.getAudioEffectMode();
654                      console.info('cur audio effect mode value:' + audioEffectMode);
655                    } catch (err) {
656                      let error = err as BusinessError;
657                      console.error(`audioRenderer getAudioEffectMode ,Error: ${JSON.stringify(error)}`);
658                      return;
659                    }
660
661                    if (audioEffectMode === 0) {
662                      this.currentAudioEffect = $r('app.string.EFFECT_NONE');
663                    } else if (audioEffectMode === 1) {
664                      this.currentAudioEffect = $r('app.string.EFFECT_DEFAULT');
665                    }
666                    this.queryResult = this.currentAudioEffect;
667                  }
668                }
669              })
670          }
671          .justifyContent(FlexAlign.SpaceBetween)
672          .height(56).width('100%')
673          .margin({ top: 4, bottom: 1 });
674
675          Row() {
676          }
677          .height(1).width(280)
678          .margin({ right: -40 })
679          .backgroundColor('#F1F3F5');
680
681          Row() {
682            Text($r('app.string.QUERY_RESULT'))
683              .fontSize(16)
684              .fontWeight(500)
685              .fontFamily($r('sys.string.ohos_id_text_font_family_medium'));
686            Text(this.queryResult)
687              .fontSize(14)
688              .margin({ top: 22 })
689              .fontColor(Color.Gray)
690              .id('query_result_realtime');
691          }
692          .justifyContent(FlexAlign.SpaceBetween)
693          .height(48)
694          .width('100%');
695
696          Row() {
697          }
698          .height(1).width('100%')
699          .backgroundColor('#F1F3F5');
700
701          Row() {
702            Text($r('app.string.AUDIO_EFFECT_MODE_SETTING'))
703              .fontSize(16)
704              .fontWeight(500)
705              .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
706              .height(22);
707            Flex({ justifyContent: FlexAlign.End, alignItems: ItemAlign.Center }) {
708              Row() {
709                Text(this.audioEffectModeSettingOptions[this.audioEffectModeSettingIndex].name)
710                  .textAlign(TextAlign.Center)
711                  .id('select_mode_name_realtime')
712                  .textOverflow({ overflow: TextOverflow.None })
713                  .fontSize(16)
714                  .fontWeight(500)
715                  .fontFamily($r('sys.string.ohos_id_text_font_family_medium'));
716
717                if (this.showSelector_3) {
718                  Image($r('app.media.ic_arrow_up_small')).height(7).width(10)
719                    .margin({ left: 8 });
720                } else {
721                  Image($r('app.media.ic_arrow_down_small')).height(7).width(10)
722                    .margin({ left: 8 });
723                }
724              }
725            }
726            .height(22)
727            .width(95)
728            .backgroundColor(Color.White)
729            .id('select_mode_realtime')
730            .onClick(() => {
731              this.showSelector_1 = false;
732              this.showSelector_2 = false;
733              this.showSelector_3 = !this.showSelector_3;
734            });
735
736            if (this.showSelector_3) {
737              Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) {
738                ForEach(this.audioEffectModeSettingOptions, (item:ModeType) => {
739                  Flex() {
740                    ModeItem_3({ mode: item });
741                  }.width(156)
742                  .onClick(async () => {
743                    if (this.audioEffectModeSettingIndex !== item.id) {
744                      this.audioEffectModeSettingIndex = item.id;
745                      console.info('this.audioEffectModeSettingIndex===' + this.audioEffectModeSettingIndex);
746                      if (this.audioRenderer !== undefined && this.audioRenderer.state !== audio.AudioState.STATE_RELEASED) {
747                        try {
748                          this.audioRenderer.setAudioEffectMode(item.id);
749                        } catch (err) {
750                          let error = err as BusinessError;
751                          console.error(`audioRenderer setAudioEffectMode ,Error: ${JSON.stringify(error)}`);
752                        }
753                      }
754                    }
755                    this.showSelector_3 = false;
756                  })
757                }, (item:ModeType) => item.id.toString());
758              }
759              .height(105)
760              .width(156)
761              .backgroundColor('#fff')
762              .borderRadius(16)
763              .shadow({ radius: 50, color: 'rgba(0,0,30,0.1500)' })
764              .padding({ left: 4, right: 4, top: 6, bottom: 4 })
765              .position({ x: 180, y: 40 })
766              .zIndex(1);
767            }
768          }
769          .margin({ top: 1, bottom: 4 })
770          .height(48)
771          .width('100%')
772          .justifyContent(FlexAlign.SpaceBetween);
773        }
774        .zIndex(0)
775        .width(360)
776        .height(160)
777        .backgroundColor(Color.White)
778        .padding({ left: 12, right: 12 })
779        .borderRadius(20);
780      }
781
782      Row() {
783        Column() {
784          Image($r('app.media.ic_Silent_normal'))
785            .width(24)
786            .height(24)
787            .margin({ top: 7, bottom: 4 });
788          Text($r('app.string.PRESET_AUDIO_EFFECT_QUERY'))
789            .fontSize(10)
790            .height(14);
791        }
792        .margin({ left: 24 })
793        .height(56)
794        .width(156)
795        .id('switch_btn_realtime')
796        .onClick(async () => {
797          if (this.audioRenderer != null) {
798            try {
799              await this.audioRenderer.stop();
800              await this.audioRenderer.release();
801            } catch (err) {
802              let error = err as BusinessError;
803              console.error(`audioRenderer release ,Error: ${JSON.stringify(error)}`);
804            }
805          }
806          await router.replaceUrl({ url: 'pages/PresetEffect' });
807        })
808
809        Column() {
810          Image($r('app.media.ic_Sound_select'))
811            .width(24)
812            .height(24)
813            .margin({ top: 7, bottom: 4 });
814          Text($r('app.string.REALTIME_AUDIO_EFFECT_SETTING'))
815            .fontSize(10)
816            .height(14)
817            .fontColor('#007DFF');
818        }
819        .height(56)
820        .width(156)
821        .margin({ right: 24 });
822      }
823      .height(56)
824      .width(360);
825    }
826    .height('100%')
827    .width('100%')
828    .justifyContent(FlexAlign.SpaceBetween)
829    .backgroundColor('#F1F3F5')
830    .onClick(() => {
831      this.showSelector_1 = false;
832      this.showSelector_2 = false;
833      this.showSelector_3 = false;
834    });
835  }
836}