openharmony 鸿蒙 distributed-audio-playback-sys

2026-08-25 浏览 (1)

Distributed Audio Playback (for System Applications Only)

Distributed audio playback enables an application to continue audio playback on another device in the same network.

You can use distributed audio playback to transfer all audio streams or the specified audio stream being played on the current device to a remote device.

How to Develop

Before continuing audio playback on another device in the same network, you must obtain the device list on the network and listen for device connection state changes. For details, see Querying and Listening for Audio Output Devices.

When obtaining the device list on the network, you can specify DeviceFlag to filter out the required devices.

NameDescription
NONE_DEVICES_FLAG9+No device is available. This is a system API.
OUTPUT_DEVICES_FLAGLocal output device.
INPUT_DEVICES_FLAGLocal input device.
ALL_DEVICES_FLAGLocal input and output device.
DISTRIBUTED_OUTPUT_DEVICES_FLAG9+Remote output device. This is a system API.
DISTRIBUTED_INPUT_DEVICES_FLAG9+Remote input device. This is a system API.
ALL_DISTRIBUTED_DEVICES_FLAG9+Remote input and output device. This is a system API.

For details about the API reference, see AudioRoutingManager.

Continuing the Playing of All Audio Streams

  1. Obtain the output device information.

  2. Create an AudioDeviceDescriptor instance to describe an audio output device.

  3. Call selectOutputDevice to select a remote device, on which all the audio streams will continue playing.

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let audioManager = audio.getAudioManager();
let audioRoutingManager = audioManager.getRoutingManager();
let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
  deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
  deviceType: audio.DeviceType.SPEAKER,
  id: 1,
  name: "",
  address: "",
  sampleRates: [44100],
  channelCounts: [2],
  channelMasks: [0],
  networkId: audio.LOCAL_NETWORK_ID,
  interruptGroupId: 1,
  volumeGroupId: 1,
  displayName: ""
}];

async function selectOutputDevice(): Promise<void> {
  audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor, (err: BusinessError) => {
    if (err) {
      console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('Invoke selectOutputDevice succeeded.');
    }
  });
}

Continuing the Playing of the Specified Audio Stream

  1. Obtain the output device information.

  2. Create an AudioRendererFilter instance, with uid to specify an application and rendererId to specify an audio stream.

  3. Create an AudioDeviceDescriptor instance to describe an audio output device.

  4. Call selectOutputDeviceByFilter to select a remote device, on which the specified audio stream will continue playing.

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let audioManager = audio.getAudioManager();
let audioRoutingManager = audioManager.getRoutingManager();
let outputAudioRendererFilter: audio.AudioRendererFilter  = {
  uid: 20010041,
  rendererInfo: {
    usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario.
    rendererFlags: 0 // AudioRenderer flag.
  } as audio.AudioRendererInfo,
  rendererId: 0
};

let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
  deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
  deviceType: audio.DeviceType.SPEAKER,
  id: 1,
  name: "",
  address: "",
  sampleRates: [44100],
  channelCounts: [2],
  channelMasks: [0],
  networkId: audio.LOCAL_NETWORK_ID,
  interruptGroupId: 1,
  volumeGroupId: 1,
  displayName: ""
}];

async function selectOutputDeviceByFilter(): Promise<void> {
  audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor, (err: BusinessError) => {
    if (err) {
      console.error(`Invoke selectOutputDeviceByFilter failed, code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('Invoke selectOutputDeviceByFilter succeeded.');
    }
  });
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 public-audio-spatialization-management

openharmony 鸿蒙 audio-session-management

openharmony 鸿蒙 using-opensl-es-for-playback

openharmony 鸿蒙 using-ohaudio-for-recording

openharmony 鸿蒙 audio-kit-intro

openharmony 鸿蒙 audio-workgroup

openharmony 鸿蒙 audio-collaborative-management-sys

openharmony 鸿蒙 audio-recording-concurrency

openharmony 鸿蒙 audio-recording-overview

openharmony 鸿蒙 audio-suite-manual-rendering

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/rCGcjU9B