tencent cloud

Mobile Live Video Broadcasting

DeviceStore

Unduh
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-11-02 17:47:28

Introduction

DeviceStore provides a comprehensive set of APIs to manage audio and video devices, including microphone, camera and screen sharing features.
Notice:
Use shared singleton to get the DeviceStore instance. Do not attempt to initialize directly.
Note:
Device state updates are delivered through the state publisher. Subscribe to it to receive real-time updates about microphone, camera, network and other states.

Features

Microphone Management: Open/close microphone, set capture volume and output volume.
Camera Management: Open/close camera, switch front/rear camera, set mirror and video quality.
Audio Route: Switch between speaker and earpiece.
Network Status: Real-time monitoring of network quality information.

Subscribable Data

DeviceState fields are described below:
Property
Type
Description
microphoneStatus
ValueListenable<DeviceStatus>
Microphone status.
microphoneLastError
ValueListenable<DeviceError>
Microphone error, used to extract error information when an error occurs.
captureVolume
ValueListenable<int>
Capture volume, with a value range from 0 to 100. Deprecated, use microphoneVolume instead.
currentMicVolume
ValueListenable<int>
Current user's actual output volume. Deprecated, use microphoneInputLevel instead.
outputVolume
ValueListenable<int>
Maximum output volume, with a value range from 0 to 100. Deprecated, use speakerVolume instead.
cameraStatus
ValueListenable<DeviceStatus>
Camera status.
cameraLastError
ValueListenable<DeviceError>
Camera error, used to extract error information when an error occurs.
isFrontCamera
ValueListenable<bool>
Whether it's front camera.
localMirrorType
ValueListenable<MirrorType>
Mirror state.
localVideoQuality
ValueListenable<VideoQuality>
Local video quality.
currentAudioRoute
ValueListenable<AudioRoute>
Current audio route location. Deprecated, use currentSpeaker instead.
screenStatus
ValueListenable<DeviceStatus>
Screen sharing status. Deprecated, use ScreenShareStore.state.screenStatus instead.
networkInfo
ValueListenable<NetworkInfo>
Network information.
networkType
ValueListenable<NetworkType>
Current network type.
microphoneList
ValueListenable<List<DeviceInfo>>
Current microphone device list. Only populated on desktop platforms, it is empty on mobile platforms.
currentMicrophone
ValueListenable<DeviceInfo>
Current microphone device, an empty deviceId means no microphone is selected.
microphoneVolume
ValueListenable<int>
Microphone capture volume, with a value range from 0 to 100.
microphoneInputLevel
ValueListenable<int>
Real-time level value used to display the microphone volume bar, range 0-100.
Only updated while the microphone test is running, see startMicrophoneTest.
cameraList
ValueListenable<List<DeviceInfo>>
Camera device list. On desktop platforms it contains all the cameras, on mobile platforms it contains the logical front camera and rear camera.
currentCamera
ValueListenable<DeviceInfo>
Current camera device, an empty deviceId means no camera is selected.
speakerList
ValueListenable<List<DeviceInfo>>
Speaker device list. On desktop platforms it contains all the speakers, on mobile platforms it contains the logical speaker and earpiece.
currentSpeaker
ValueListenable<DeviceInfo>
Current speaker device, an empty deviceId means no speaker is selected.
speakerVolume
ValueListenable<int>
Speaker playback volume, with a value range from 0 to 100.
speakerOutputLevel
ValueListenable<int>
Speaker real-time output level value, range 0-100.
Only updated while the speaker test is running, see startSpeakerTest.

API List

Function
Description
shared
Singleton object.
Open local microphone.
Close local microphone.
Set microphone capture volume.
Set speaker playback volume.
Select microphone device.
Start microphone test.
Stop microphone test.
Select speaker device.
Start speaker test.
Stop speaker test.
Start camera test.
Stop camera test.
Open local camera.
Close local camera.
Select camera device.
Switch mirror state.
Update video quality.
Set capture volume, use setMicrophoneVolume instead.
Set output volume, use setSpeakerVolume instead.
Set audio route, use setCurrentSpeaker instead.
Switch camera, use setCurrentCamera instead.
reset
Reset to default state.

Getting Instance

shared

Singleton object.

Microphone Operations

openLocalMicrophone

Open local microphone.
Future<CompletionHandler> openLocalMicrophone();
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
completion
CompletionClosure?
Whether operation succeeded.

closeLocalMicrophone

Close local microphone.
void closeLocalMicrophone();
Version
Supported since version 3.5.

setMicrophoneVolume

Set microphone capture volume.
void setMicrophoneVolume(int volume);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
volume
int
Capture volume, with a value range from 0 to 100.

setSpeakerVolume

Set speaker playback volume.
void setSpeakerVolume(int volume);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
volume
int
Playback volume, with a value range from 0 to 100.

setCurrentMicrophone

Set current microphone device.
Only supported on desktop platforms, mobile platforms do not allow switching the microphone device and calling it has no effect there. It takes effect only when the microphone is on.
void setCurrentMicrophone(DeviceInfo microphone);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
microphone
Microphone device, an element of microphoneList.

startMicrophoneTest

Start microphone test, the capture level is reported to microphoneInputLevel in real time, with a capture interval in milliseconds.
void startMicrophoneTest(int interval);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
interval
int
Capture interval in milliseconds. Default is 200ms.

stopMicrophoneTest

Stop microphone test.
void stopMicrophoneTest();
Version
Supported since version 3.5.

Speaker Operations

setCurrentSpeaker

Set current speaker device.
void setCurrentSpeaker(DeviceInfo speaker);
Supported on all platforms: on desktop platforms any device in speakerList can be selected, on mobile platforms only the logical speaker/earpiece switch takes effect.
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
speaker
Speaker device, an element of speakerList.

startSpeakerTest

Start speaker test, the playback level is reported to speakerOutputLevel in real time.
An audio file is played during the test, the speaker device in use can be verified.
void startSpeakerTest(String filePath);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
filePath
String
Audio file path used for the test playback. Required; an empty value returns an invalid-parameter error.

stopSpeakerTest

Stop speaker test.
void stopSpeakerTest();
Version
Supported since version 3.5.

Camera Operations

startCameraTest

Start camera test, if camera opens successfully, the view will be rendered to the set CameraView.
Future<CompletionHandler> startCameraTest(int cameraViewId);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
cameraViewId
int
Render view id for camera capture, obtained from CameraView.onViewCreated. Pass 0 to set no render target.
completion
CompletionClosure?
Whether operation succeeded.

stopCameraTest

Stop camera test.
void stopCameraTest();
Version
Supported since version 3.5.

openLocalCamera

Open local camera.
Future<CompletionHandler> openLocalCamera(bool isFront);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
isFront
bool
Whether front camera.
completion
CompletionClosure?
Whether operation succeeded.

closeLocalCamera

Close local camera.
void closeLocalCamera();
Version
Supported since version 3.5.

setCurrentCamera

Set current camera device.
Supported on all platforms: on desktop platforms any device in cameraList can be selected, on mobile platforms it is equivalent to switching between the front and rear cameras.
It takes effect only when the camera is on.
void setCurrentCamera(DeviceInfo camera);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
camera
Camera device, an element of cameraList.

switchMirror

Switch mirror state.
void switchMirror(MirrorType mirrorType);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
mirrorType
Mirror state.

updateVideoQuality

Update video quality.
void updateVideoQuality(VideoQuality quality, {VideoOrientation orientation = VideoOrientation.portrait});
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
quality
Video quality.
orientation
Video orientation. Defaults to portrait. Pass landscape to use the base (width x height) resolution.

Deprecated

setCaptureVolume

Set capture volume (deprecated, use setMicrophoneVolume instead).
Warning:
Deprecated: Use setMicrophoneVolume instead. Will be removed in version 5.5.
@Deprecated('Use setMicrophoneVolume instead. Will be removed in version 5.5.')
void setCaptureVolume(int volume);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
volume
int
Capture volume, with a value range from 0 to 100.

setOutputVolume

Set maximum output volume (deprecated, use setSpeakerVolume instead).
Warning:
Deprecated: Use setSpeakerVolume instead. Will be removed in version 5.5.
@Deprecated('Use setSpeakerVolume instead. Will be removed in version 5.5.')
void setOutputVolume(int volume);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
volume
int
Maximum volume, with a value range from 0 to 100.

setAudioRoute

Set audio route (deprecated, use setCurrentSpeaker instead).
Warning:
Deprecated: Use setCurrentSpeaker instead. Will be removed in version 5.5.
@Deprecated('Use setCurrentSpeaker instead. Will be removed in version 5.5.')
void setAudioRoute(AudioRoute route);
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
route
Route location. On mobile platforms speakerList contains the speaker and the earpiece, so setCurrentSpeaker covers this capability.

switchCamera

Switch camera (deprecated, use setCurrentCamera instead).
Warning:
Deprecated: Use setCurrentCamera instead. Will be removed in version 5.5.
@Deprecated('Use setCurrentCamera instead. Will be removed in version 5.5.')
void switchCamera(bool isFront);
On mobile platforms cameraList contains the front and rear cameras, so setCurrentCamera covers this capability.
Version
Supported since version 3.5.
Parameters
Parameter
Type
Description
isFront
bool
Whether front camera.

Reset

reset

Reset to default state.
void reset();
Version
Supported since version 3.5.

Data Structures

DeviceType

Device type.
Enum Value
Value
Description
microphone
1
Microphone type.
camera
2
Camera type.
screenShare
3
Screen sharing type.

DeviceError

Device related error codes.
Enum Value
Value
Description
noError
0
Operation successful.
noDeviceDetected
1
No device detected.
noSystemPermission
2
No system permission.
notSupportCapture
3
Capture not supported.
occupiedError
4
Device occupied.
unknownError
5
Unknown error.

DeviceStatus

Device on/off status.
Enum Value
Value
Description
off
0
Off.
on
1
On.

AudioRoute

Audio route. Deprecated, use DeviceInfo instead.
Enum Value
Value
Description
speakerphone
0
Speaker, using speaker to play (i.e., "hands-free"), located at the bottom of the phone, louder sound, suitable for playing music out loud.
earpiece
1
Earpiece, using earpiece to play, located at the top of the phone, quieter sound, suitable for private call scenarios.

VideoQuality

Video quality level. The final encoding resolution is determined by the combination of this level and VideoOrientation: landscape keeps the base resolution (width x height), portrait swaps the width and height.
Enum Value
Value
Description
quality360P
1
360P. Landscape resolution is 640 x 360; portrait (width and height swapped) is 360 x 640.
quality540P
2
540P. Landscape resolution is 960 x 540; portrait (width and height swapped) is 540 x 960.
quality720P
3
720P. Landscape resolution is 1280 x 720; portrait (width and height swapped) is 720 x 1280.
quality1080P
4
1080P. Landscape resolution is 1920 x 1080; portrait (width and height swapped) is 1080 x 1920.

VideoOrientation

Video orientation. Combined with VideoQuality to determine the final encoding resolution: landscape keeps the base resolution (width x height), portrait swaps the width and height.
Enum Value
Value
Description
landscape
0
Landscape, the encoding resolution width is greater than the height (e.g. 1280 x 720).
portrait
1
Portrait, the encoding resolution height is greater than the width (e.g. 720 x 1280).

NetworkQuality

Network quality.
Enum Value
Value
Description
unknown
0
Unknown network.
excellent
1
Excellent.
good
2
Good.
poor
3
Poor.
bad
4
Bad.
veryBad
5
Very bad.
down
6
Disconnected.

MirrorType

Camera mirror state.
Enum Value
Value
Description
auto
0
Auto, front camera mirrored, rear camera not mirrored.
enable
1
Both front and rear cameras mirrored.
disable
2
Neither front nor rear camera mirrored.

NetworkType

Network type.
Enum Value
Value
Description
unknown
0
Unknown or unidentified network type.
wifi
1
WiFi network connection.
cellular
2
Cellular/mobile data network connection.

DeviceInfo

Device information.
Property
Type
Description
deviceId
String
Device ID.
deviceName
String
Device name.

NetworkInfo

Network information.
Property
Type
Description
userID
String
User unique ID.
quality
Network quality.
upLoss
int
Uplink packet loss rate, with a value range from 0 to 100.
downLoss
int
Downlink packet loss rate, with a value range from 0 to 100.
delay
int
Latency (unit: milliseconds).

DeviceState

Device state.
Property
Type
Description
microphoneStatus
ValueListenable<DeviceStatus>
Microphone status.
microphoneLastError
ValueListenable<DeviceError>
Microphone error, used to extract error information when an error occurs.
captureVolume
ValueListenable<int>
Capture volume, with a value range from 0 to 100. Deprecated, use microphoneVolume instead.
currentMicVolume
ValueListenable<int>
Current user's actual output volume. Deprecated, use microphoneInputLevel instead.
outputVolume
ValueListenable<int>
Maximum output volume, with a value range from 0 to 100. Deprecated, use speakerVolume instead.
cameraStatus
ValueListenable<DeviceStatus>
Camera status.
cameraLastError
ValueListenable<DeviceError>
Camera error, used to extract error information when an error occurs.
isFrontCamera
ValueListenable<bool>
Whether it's front camera.
localMirrorType
ValueListenable<MirrorType>
Mirror state.
localVideoQuality
ValueListenable<VideoQuality>
Local video quality.
currentAudioRoute
ValueListenable<AudioRoute>
Current audio route location. Deprecated, use currentSpeaker instead.
screenStatus
ValueListenable<DeviceStatus>
Screen sharing status. Deprecated, use ScreenShareStore.state.screenStatus instead.
networkInfo
ValueListenable<NetworkInfo>
Network information.
networkType
ValueListenable<NetworkType>
Current network type.
microphoneList
ValueListenable<List<DeviceInfo>>
Current microphone device list. Only populated on desktop platforms, it is empty on mobile platforms.
currentMicrophone
ValueListenable<DeviceInfo>
Current microphone device, an empty deviceId means no microphone is selected.
microphoneVolume
ValueListenable<int>
Microphone capture volume, with a value range from 0 to 100.
microphoneInputLevel
ValueListenable<int>
Real-time level value used to display the microphone volume bar, range 0-100.
Only updated while the microphone test is running, see startMicrophoneTest.
cameraList
ValueListenable<List<DeviceInfo>>
Camera device list. On desktop platforms it contains all the cameras, on mobile platforms it contains the logical front camera and rear camera.
currentCamera
ValueListenable<DeviceInfo>
Current camera device, an empty deviceId means no camera is selected.
speakerList
ValueListenable<List<DeviceInfo>>
Speaker device list. On desktop platforms it contains all the speakers, on mobile platforms it contains the logical speaker and earpiece.
currentSpeaker
ValueListenable<DeviceInfo>
Current speaker device, an empty deviceId means no speaker is selected.
speakerVolume
ValueListenable<int>
Speaker playback volume, with a value range from 0 to 100.
speakerOutputLevel
ValueListenable<int>
Speaker real-time output level value, range 0-100.
Only updated while the speaker test is running, see startSpeakerTest.

Usage Example

// Get singleton instance
let store = DeviceStore.shared
// Subscribe to state changes
store.state.subscribe { state in
print("Microphone status: \\(state.microphoneStatus)")
print("Camera status: \\(state.cameraStatus)")
print("Network quality: \\(state.networkInfo.quality)")
}
// Open microphone
store.openLocalMicrophone { code, message in
if code == 0 {
print("Microphone opened successfully")
}
}
// Open front camera
store.openLocalCamera(isFront: true) { code, message in
if code == 0 {
print("Camera opened successfully")
}
}


Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan