HarmonyOS 视频播放时的静音功能的实现和监听音量改变

希望实现视频播放时能静音并且改变音量时取消静音。

audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => {
  console.info(`VolumeType of stream: ${volumeEvent.volumeType} `);
  console.info(`Volume level: ${volumeEvent.volume} `);
  console.info(`Whether to updateUI: ${volumeEvent.updateUi} `);
});

只在aboutToAppear中初始化调用一次,其他时候调节音量不起效。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

可以添加onKeyEvent组件,来监听按下音量键时的变化。

// xxx.ets
import { audio } from '@kit.AudioKit';

let audioManager = audio.getAudioManager();
let audioVolumeManager = audioManager.getVolumeManager();

@Entry
@Component
struct VideoCreateComponent {
  @State videoSrc: Resource = $rawfile('video(1).mp4')
  @State previewUri: Resource = $r('app.media.app_icon')
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X
  @State isAutoPlay: boolean = false
  @State showControls: boolean = true
  controller: VideoController = new VideoController()
  
  build() {
    Column() {
      Video({
        src: this.videoSrc,
        previewUri: this.previewUri,
        currentProgressRate: this.curRate,
        controller: this.controller
      })
        .width('100%').height(600)
        .autoPlay(this.isAutoPlay)
        .controls(this.showControls)
        .onStart(() => {
          console.info('onStart')
        })
        .onPause(() => {
          console.info('onPause')
        })
        .onFinish(() => {
          console.info('onFinish')
        })
        .onError(() => {
          console.info('onError')
        })
        .onPrepared((e) => {
          console.info('onPrepared is ' + e.duration)
        })
        .onSeeking((e) => {
          console.info('onSeeking is ' + e.time)
        })
        .onSeeked((e) => {
          console.info('onSeeked is ' + e.time)
        })
        .onUpdate((e) => {
          console.info('onUpdate is ' + e.time)
        })

      Row() {
        Button('src').onClick(() => {
          this.videoSrc = $rawfile('video(1).mp4') // 切换视频源
        }).margin(5)
        Button('previewUri').onClick(() => {
          this.previewUri = $r('app.media.app_icon') // 切换视频预览海报
        }).margin(5)
        Button('controls').onClick(() => {
          this.showControls = !this.showControls // 切换是否显示视频控制栏
        }).margin(5)
      }

      Row() {
        Button('start').onClick(() => {
          this.controller.start() // 开始播放
        }).margin(5)
        Button('pause').onClick(() => {
          this.controller.pause() // 暂停播放
        }).margin(5)
        Button('stop').onClick(() => {
          this.controller.stop() // 结束播放
        }).margin(5)
        Button('setTime').onClick(() => {
          this.controller.setCurrentTime(10, SeekMode.Accurate) // 精准跳转到视频的10s位置
        }).margin(5)
      }

      Row() {
        Button('rate 0.75').onClick(() => {
          this.curRate = PlaybackSpeed.Speed_Forward_0_75_X // 0.75倍速播放
        }).margin(5)
        Button('rate 1').onClick(() => {
          this.curRate = PlaybackSpeed.Speed_Forward_1_00_X // 原倍速播放
        }).margin(5)
        Button('rate 2').onClick(() => {
          this.curRate = PlaybackSpeed.Speed_Forward_2_00_X // 2倍速播放
        }).margin(5)
      }
    }.onKeyEvent((event?: KeyEvent) => {
      if(event) {
        if (event.keyCode === 16) {
          audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => {
            console.info(`VolumeType of stream: ${volumeEvent.volumeType} `);
            console.info(`Volume level: ${volumeEvent.volume} `);
            console.info(`Whether to updateUI: ${volumeEvent.updateUi} `);
          });
        }
        if (event.keyCode === 17) {
          audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => {
            console.info(`VolumeType of stream: ${volumeEvent.volumeType} `);
            console.info(`Volume level: ${volumeEvent.volume} `);
            console.info(`Whether to updateUI: ${volumeEvent.updateUi} `);
          });
        }
      }
    })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
OHAudio实现延音频录制播放功能
1684浏览 • 1回复 待解决
HarmonyOS 视频如何设置静音
19浏览 • 1回复 待解决
HarmonyOS 需要视频小窗播放功能
26浏览 • 1回复 待解决
HarmonyOS 音量监听可以取消监听
37浏览 • 1回复 待解决
AVPlayer实现视频播放
1071浏览 • 1回复 待解决