|
1 | 1 | import 'dart:async';
|
2 | 2 | import 'dart:html' as html;
|
| 3 | +import 'dart:html_common'; |
| 4 | +import 'dart:js' as js; |
3 | 5 | import 'dart:js_util' as jsutil;
|
4 | 6 | import 'package:webrtc_interface/webrtc_interface.dart';
|
5 | 7 |
|
@@ -126,4 +128,66 @@ class MediaDevicesWeb extends MediaDevices {
|
126 | 128 | width: _mapConstraints['width'],
|
127 | 129 | zoom: _mapConstraints['zoom']);
|
128 | 130 | }
|
| 131 | + |
| 132 | + @override |
| 133 | + Future<MediaDeviceInfo> selectAudioOutput( |
| 134 | + [AudioOutputOptions? options]) async { |
| 135 | + try { |
| 136 | + final mediaDevices = html.window.navigator.mediaDevices; |
| 137 | + if (mediaDevices == null) throw Exception('MediaDevices is null'); |
| 138 | + |
| 139 | + if (jsutil.hasProperty(mediaDevices, 'selectAudioOutput')) { |
| 140 | + if (options != null) { |
| 141 | + final arg = jsutil.jsify(options); |
| 142 | + final deviceInfo = await jsutil.promiseToFuture<html.MediaDeviceInfo>( |
| 143 | + jsutil.callMethod(mediaDevices, 'selectAudioOutput', [arg])); |
| 144 | + return MediaDeviceInfo( |
| 145 | + kind: deviceInfo.kind, |
| 146 | + label: deviceInfo.label ?? '', |
| 147 | + deviceId: deviceInfo.deviceId ?? '', |
| 148 | + groupId: deviceInfo.groupId, |
| 149 | + ); |
| 150 | + } else { |
| 151 | + final deviceInfo = await jsutil.promiseToFuture<html.MediaDeviceInfo>( |
| 152 | + jsutil.callMethod(mediaDevices, 'selectAudioOutput', [])); |
| 153 | + return MediaDeviceInfo( |
| 154 | + kind: deviceInfo.kind, |
| 155 | + label: deviceInfo.label ?? '', |
| 156 | + deviceId: deviceInfo.deviceId ?? '', |
| 157 | + groupId: deviceInfo.groupId, |
| 158 | + ); |
| 159 | + } |
| 160 | + } else { |
| 161 | + throw UnimplementedError('selectAudioOutput is missing'); |
| 162 | + } |
| 163 | + } catch (e) { |
| 164 | + throw 'Unable to selectAudioOutput: ${e.toString()}, Please try to use MediaElement.setSinkId instead.'; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @override |
| 169 | + set ondevicechange(Function(dynamic event)? listener) { |
| 170 | + try { |
| 171 | + final mediaDevices = html.window.navigator.mediaDevices; |
| 172 | + if (mediaDevices == null) throw Exception('MediaDevices is null'); |
| 173 | + |
| 174 | + jsutil.setProperty(mediaDevices, 'ondevicechange', |
| 175 | + js.allowInterop((evt) => listener?.call(evt))); |
| 176 | + } catch (e) { |
| 177 | + throw 'Unable to set ondevicechange: ${e.toString()}'; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @override |
| 182 | + Function(dynamic event)? get ondevicechange { |
| 183 | + try { |
| 184 | + final mediaDevices = html.window.navigator.mediaDevices; |
| 185 | + if (mediaDevices == null) throw Exception('MediaDevices is null'); |
| 186 | + |
| 187 | + jsutil.getProperty(mediaDevices, 'ondevicechange'); |
| 188 | + } catch (e) { |
| 189 | + throw 'Unable to get ondevicechange: ${e.toString()}'; |
| 190 | + } |
| 191 | + return null; |
| 192 | + } |
129 | 193 | }
|
0 commit comments