harmony 鸿蒙Distributed Audio Playback (for System Applications Only)

  • 2023-06-24
  • 浏览 (449)

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 Audio Output Device Management.

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

Name Description
NONE_DEVICES_FLAG9+ None. This is a system API.
OUTPUT_DEVICES_FLAG Local output device.
INPUT_DEVICES_FLAG Local input device.
ALL_DEVICES_FLAG Local 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 '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';

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 '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';

let audioManager = audio.getAudioManager();
let audioRoutingManager = audioManager.getRoutingManager();
let outputAudioRendererFilter: audio.AudioRendererFilter  = {
    uid: 20010041,
    rendererInfo: {
        content: audio.ContentType.CONTENT_TYPE_MUSIC,
        usage: audio.StreamUsage.STREAM_USAGE_MEDIA,
        rendererFlags: 0 } 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.');
        }
    });
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Development

harmony 鸿蒙Audio Decoding

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Audio Encoding

harmony 鸿蒙Audio Input Device Management

harmony 鸿蒙Audio Output Device Management

harmony 鸿蒙Audio Playback Concurrency Policy

harmony 鸿蒙Audio Playback Development

0  赞