1、设备:RK3568开发板

2、系统:openharmony4.0

3、问题描述:

在调用接口AudioRoutingManager的getDevices(deviceFlag: DeviceFlag)方法获取音频设备列表时,扬声器设备和麦克风接口总可以被获取到,不论是否连接设备。有线耳机接口正常,不接入设备,无法获取到设备,接入后可以获取到设备。请问这个问题是怎么回事,如何修改,在线求助大佬解惑。

4、代码:

import router from '@ohos.router'
import audio from '@ohos.multimedia.audio';

@Entry
@Component
struct audioEquipmentManagementIndex {
  private setIntervalID: number = -1
  private audioManager: audio.AudioManager | undefined = undefined
  private audioRoutingManager: audio.AudioRoutingManager | undefined = undefined
  @State audioOutputDeviceList: audio.AudioDeviceDescriptors | undefined = undefined
  @State checkMode: audio.DeviceFlag = audio.DeviceFlag.OUTPUT_DEVICES_FLAG
@State modeText:string='输出设备'
  async initial() {
    this.audioManager = audio.getAudioManager();
    this.audioRoutingManager = this.audioManager.getRoutingManager();
    if (this.audioRoutingManager != undefined) {
      let data: audio.AudioDeviceDescriptors | undefined = await this.audioRoutingManager.getDevices(this.checkMode)
      this.audioOutputDeviceList = data
    }
  }

  async aboutToAppear() {

    await this.initial()
    this.setIntervalID = setInterval(async () => {
      if (this.audioRoutingManager != undefined) {
        let data: audio.AudioDeviceDescriptors | undefined = await this.audioRoutingManager.getDevices(this.checkMode)
        this.audioOutputDeviceList = data
      }
    }, 1000)

  }

  aboutToDisappear() {

    clearInterval(this.setIntervalID)
  }

  build() {
    Column() {
      Button("首页")
        .width(120)
        .height(40)
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ top: 10, bottom: 10 })
        .onClick(() => {
          router.back()
        })
      Button("切换")
        .width(120)
        .height(40)
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ top: 10, bottom: 10 })
        .onClick(() => {
          if (this.checkMode === audio.DeviceFlag.OUTPUT_DEVICES_FLAG) {
            this.checkMode = audio.DeviceFlag.INPUT_DEVICES_FLAG
            this. modeText='输入设备'
          } else {
            this.checkMode = audio.DeviceFlag.OUTPUT_DEVICES_FLAG
            this. modeText='输出设备'
          }
        })
      Scroll() {
        Column() {
          Text(this. modeText)
            .fontSize(25)
            .fontWeight(FontWeight.Bold)
            .size({ width: '100%' })
            .margin({ top: 5, bottom: 10 })
          ForEach(this.audioOutputDeviceList, (item: audio.AudioDeviceDescriptor, index: number) => {
            Text(`设备列表${index}\n${JSON.stringify(item)}`)
              .fontSize(25)
              .fontWeight(FontWeight.Bold)
              .size({ width: '100%' })
              .margin({ top: 5, bottom: 10 })
          }, (item: audio.AudioDeviceDescriptor, index: number) => `${JSON.stringify(item)}` + index)

        }.constraintSize({ minWidth: '100%', minHeight: '100%' })
      }.size({ width: '100%', height: '75%' })

    }
    .padding({ left: 10, right: 10, top: 15, bottom: 15 })
    .size({ width: '100%', height: '100%' })

  }
}

5、结果

扬声器连接前获取音频输出设备返回结果:
[{"deviceRole":2,"deviceType":2,"id":1,"name":"","address":"","networkId":"LocalDevice","displayName":"OpenHarmony 3.2","interruptGroupId":1,"volumeGroupId":1,"sampleRates":[44100],"channelCounts":[2],"channelMasks":[0],"encodingTypes":[0]}]
扬声器连接后获取音频输出设备返回结果:
[{"deviceRole":2,"deviceType":2,"id":1,"name":"","address":"","networkId":"LocalDevice","displayName":"OpenHarmony 3.2","interruptGroupId":1,"volumeGroupId":1,"sampleRates":[44100],"channelCounts":[2],"channelMasks":[0],"encodingTypes":[0]}]
耳机连接前获取音频输出设备返回结果:
[{"deviceRole":2,"deviceType":2,"id":1,"name":"","address":"","networkId":"LocalDevice","displayName":"OpenHarmony 3.2","interruptGroupId":1,"volumeGroupId":1,"sampleRates":[44100],"channelCounts":[2],"channelMasks":[0],"encodingTypes":[0]}]
耳机连接后获取音频输出设备返回结果:
[{"deviceRole":2,"deviceType":3,"id":3,"name":"","address":"","networkId":"LocalDevice","displayName":"OpenHarmony 3.2","interruptGroupId":1,"volumeGroupId":1,"sampleRates":[44100],"channelCounts":[2],"channelMasks":[0],"encodingTypes":[0]},
{"deviceRole":2,"deviceType":2,"id":1,"name":"","address":"","networkId":"LocalDevice","displayName":"OpenHarmony 3.2","interruptGroupId":1,"volumeGroupId":1,"sampleRates":[44100],"channelCounts":[2],"channelMasks":[0],"encodingTypes":[0]}]

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐