live.service.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AlertController } from '@ionic/angular';
  4. import * as Parse from 'parse';
  5. import { AiChatService } from './aichart.service';
  6. import { HttpService } from './http.service';
  7. import { MessageService } from './message.service';
  8. declare const AgoraRTC: any;
  9. import BeautyExtension from 'agora-extension-beauty-effect'; // 引入美颜扩展
  10. @Injectable({
  11. providedIn: 'root',
  12. })
  13. export class LiveService {
  14. isAnchor: boolean = false; //是否是主播
  15. options: {
  16. appid: string;
  17. channel: string;
  18. token: string;
  19. } = {
  20. appid: '',
  21. channel: '',
  22. token: '',
  23. };
  24. localTracks: any = {
  25. audioTrack: null,
  26. videoTrack: null,
  27. };
  28. rid?: string; //房间id(channel)
  29. profile?: any = localStorage.getItem('profile');
  30. client: any; //客户端
  31. company: string = '';
  32. UID: any;
  33. tools: any = {
  34. audio: false, //是否关闭音频
  35. camera: false, //是否切换摄像头
  36. glorify: false, //是否开启美颜
  37. mute: false, //是否静音
  38. };
  39. user_published_list: Array<any> = []; //已订阅频道用户列表
  40. timer: any; //轮询获取频道token的定时
  41. alert: any; //提示框
  42. media_devices: {
  43. //音视频设备列表
  44. audioDevices: Array<any>;
  45. videoDevices: Array<any>;
  46. } = {
  47. audioDevices: [],
  48. videoDevices: [],
  49. };
  50. currentUsedDevice: { audioDevice: string; videoDevice: string } = {
  51. //当前使用的设备
  52. audioDevice: '',
  53. videoDevice: '',
  54. };
  55. room?: Parse.Object; //直播间
  56. connection_state?: string; //连接状态
  57. surplusNumber: number = 0; //剩余可通话时长(单位:秒s)
  58. countdown: number = 0; // 新增倒计时变量
  59. timer_countdown: any;
  60. liveLog?: Parse.Object; //直播记录
  61. isOpenEvaluate: boolean = false; //是否开启评价
  62. processor: any; //音视频处理
  63. constructor(
  64. private http: HttpService,
  65. private router: Router,
  66. private aiServ: AiChatService,
  67. private alertController: AlertController,
  68. private msgSer: MessageService
  69. ) {
  70. this.client?.leave();
  71. this.company = this.aiServ.company;
  72. this.getProfile();
  73. }
  74. async getProfile() {
  75. if (this.profile) return;
  76. let queryProfile = new Parse.Query('Profile');
  77. queryProfile.equalTo('user', Parse.User.current()?.id);
  78. queryProfile.notEqualTo('isDeleted', true);
  79. queryProfile.equalTo('isCross', true);
  80. this.profile = await queryProfile.first();
  81. this.profile?.id &&
  82. localStorage.setItem('profile', JSON.stringify(this.profile.toJSON()));
  83. }
  84. /* 初始化Agora */
  85. initAgora() {
  86. this.tools = {
  87. audio: false, //是否关闭音频
  88. camera: false, //是否切换摄像头
  89. glorify: false, //是否开启美颜
  90. mute: false, //是否静音
  91. };
  92. this.timer && clearTimeout(this.timer);
  93. this.timer_countdown && clearInterval(this.timer_countdown);
  94. this.timer_countdown = null;
  95. this.options['token'] = '';
  96. this.options['channel'] = '';
  97. this.user_published_list = [];
  98. this.client?.leave();
  99. if(!this.client){
  100. this.client = AgoraRTC.createClient({ mode: 'rtc', codec: 'h264' });
  101. AgoraRTC.enableLogUpload();
  102. // 创建 BeautyExtension 实例
  103. const extension: any = new BeautyExtension();
  104. // 注册插件
  105. AgoraRTC.registerExtensions([extension]);
  106. // 创建 BeautyProcessor 实例
  107. this.processor = extension.createProcessor();
  108. }
  109. this.setBeautyOptions();
  110. }
  111. // 获取所有音视频设备
  112. getDevices() {
  113. AgoraRTC.getDevices()
  114. .then((devices: any) => {
  115. this.media_devices.audioDevices = devices.filter(function (
  116. device: any
  117. ) {
  118. return device.kind === 'audioinput';
  119. });
  120. this.media_devices.videoDevices = devices.filter(function (
  121. device: any
  122. ) {
  123. return device.kind === 'videoinput';
  124. });
  125. console.log(this.media_devices);
  126. this.currentUsedDevice = {
  127. audioDevice: this.media_devices.audioDevices[0].deviceId,
  128. videoDevice: this.media_devices.videoDevices[0].deviceId,
  129. };
  130. })
  131. .then((tracks: any) => {
  132. console.log(tracks);
  133. })
  134. .catch((err: any) => {
  135. console.warn('获取媒体权限失败', err);
  136. this.alertTips('获取媒体权限失败');
  137. });
  138. }
  139. /* 获取token */
  140. async getToken(room: Parse.Object) {
  141. this.room = room;
  142. this.isAnchor = room?.get('user')?.id === Parse.User.current()?.id;
  143. this.timer && clearTimeout(this.timer);
  144. let remoteEle = document.getElementById('vice-video');
  145. (remoteEle as any).style.display = 'none';
  146. //获取频道token记录
  147. let baseurl = 'https://server.fmode.cn/api/ailiao/token';
  148. if (this.isAnchor) {
  149. this.UID = 111111;
  150. baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
  151. }
  152. let reqBody = {
  153. company: this.company, // this.aiSer.company,
  154. profile: room?.get('profile').id,
  155. channelName: room?.get('profile').id,
  156. };
  157. let data: any = await this.http.httpRequst(baseurl, reqBody, 'POST');
  158. console.log(data);
  159. if (data.code == 200) {
  160. this.options.token = data.data.token;
  161. this.options.appid = data.data.appid;
  162. } else {
  163. this.timer = setTimeout(() => {
  164. this.getToken(room);
  165. }, 2000);
  166. return;
  167. }
  168. this.options.channel = room?.get('profile').id;
  169. this.join();
  170. }
  171. async updateToken(pid: string) {
  172. let sql = `select "rtc"."objectId" as "rid","rtc"."channel", "rtc"."token", "rtc"."expiraTime" from "RtcToken" as "rtc" where "rtc"."profile" = '${pid}' and "rtc"."expiraTime" >= now() order by "createdAt" desc limit 1`;
  173. let tokenData: any = await this.http.customSQL(sql);
  174. if (
  175. tokenData &&
  176. tokenData.code == 200 &&
  177. tokenData.data &&
  178. tokenData.data.length > 0
  179. ) {
  180. return tokenData.data[0];
  181. } else {
  182. return null;
  183. }
  184. }
  185. /* 进入频道 */
  186. async join() {
  187. let path = location.pathname;
  188. if (path.indexOf('/live/link-room/') == -1) return;
  189. console.log('频道id:', this.options.channel);
  190. this.client
  191. .join(
  192. this.options.appid,
  193. this.options.channel,
  194. this.options.token,
  195. this.UID
  196. )
  197. .then(async (uid: any) => {
  198. // await this.client.setClientRole('host');
  199. this.connection_state = this.client.connectionState;
  200. console.log('进入频道当前uid:', uid, '状态:', this.connection_state);
  201. let user = Parse.User.current();
  202. this.msgSer?.publishMessage(
  203. `${user?.get('name') || user?.get('nickname')}加入频道直播`,
  204. this.room?.get('user')?.id
  205. );
  206. if (!this.isAnchor) {
  207. // 观众进入直播间,创建直播记录
  208. await this.createLiveLog(uid);
  209. } else {
  210. //主播进入直播间直接发送自己推流
  211. await this.publishSelf();
  212. }
  213. await this.joinReady();
  214. this.afterJoin();
  215. })
  216. .catch((err: any) => {
  217. console.error('进入频道失败:', err);
  218. });
  219. }
  220. /* 发布本地视频 */
  221. async publishSelf() {
  222. try {
  223. let data = await Promise.all([
  224. /* 创建音频和视频轨道 */
  225. AgoraRTC.createMicrophoneAudioTrack(this.currentUsedDevice.audioDevice),
  226. AgoraRTC.createCameraVideoTrack(this.currentUsedDevice.videoDevice),
  227. ]);
  228. this.localTracks.audioTrack = data[0];
  229. this.localTracks.videoTrack = data[1];
  230. // console.log(this.localTracks);
  231. let remoteEle = document.getElementById('vice-video');
  232. if (remoteEle) {
  233. remoteEle.textContent = '';
  234. remoteEle.style.display = 'block';
  235. }
  236. this.localTracks.videoTrack.play('vice-video'); //播放自己视频渲染
  237. // if (this.tools['audio']) {
  238. // this.localTracks.audioTrack.setEnabled(false);
  239. // } else {
  240. // this.localTracks.audioTrack.setEnabled(true);
  241. // }
  242. if (this.processor && this.localTracks.videoTrack) {
  243. // 将插件注入 SDK 内的视频处理管道
  244. this.localTracks.videoTrack
  245. .pipe(this.processor)
  246. .pipe(this.localTracks.videoTrack.processorDestination);
  247. // 开启美颜
  248. await this.processor.enable();
  249. }
  250. await this.client.publish(Object.values(this.localTracks));
  251. } catch (err) {
  252. console.log('发布本地视频失败:', err);
  253. // history.back()
  254. this.alertTips('发布本地视频失败,请检查摄像头是否授权或正常', '提示');
  255. this.client.leave();
  256. }
  257. }
  258. /* 设置各项美颜参数 */
  259. async setBeautyOptions() {
  260. this.processor.setOptions({
  261. lighteningContrastLevel: 2, // 对比度
  262. lighteningLevel: 0.8, // 亮度
  263. smoothnessLevel: 0.8, // 平滑度
  264. sharpnessLevel: 0.5, // 锐化程度
  265. rednessLevel: 0.5, // 红润度
  266. });
  267. this.tools['glorify'] = true;
  268. }
  269. /* 订阅远程视频 */
  270. async joinReady() {
  271. this.client.remoteUsers.forEach((user: any) => {
  272. // console.log('remoteUsers', user.uid);
  273. this.client.subscribe(user, 'audio').then((audioTrack: any) => {
  274. if (!this.user_published_list.find((item) => item.uid === user.uid)) {
  275. this.user_published_list.push(user);
  276. }
  277. audioTrack.setVolume(100);
  278. audioTrack.play();
  279. });
  280. this.client.subscribe(user, 'video').then((videoTrack: any) => {
  281. let remoteEle = document.getElementById('video');
  282. if (remoteEle) {
  283. remoteEle.textContent = '';
  284. }
  285. videoTrack.play('video');
  286. });
  287. });
  288. this.client.on('user-joined', (user: any) => {
  289. console.log(user, `${user.uid} 加入频道`);
  290. });
  291. this.client.on('user-published', async (user: any, mediaType: string) => {
  292. console.log('用户推流成功', user);
  293. await this.client.subscribe(user, mediaType);
  294. let remoteEle = document.getElementById('video');
  295. if (remoteEle) {
  296. remoteEle.textContent = '';
  297. }
  298. if (mediaType === 'video') {
  299. user.videoTrack.play('video');
  300. this.alert?.dismiss();
  301. }
  302. if (mediaType === 'audio') {
  303. user.audioTrack.play();
  304. if (!this.user_published_list.find((item) => item.uid === user.uid)) {
  305. this.user_published_list.push(user);
  306. }
  307. }
  308. });
  309. this.client.on('user-unpublished', async (user: any, mediaType: string) => {
  310. if (mediaType === 'audio') {
  311. console.log('对方已静音');
  312. let idx = this.user_published_list.findIndex(
  313. (item) => item.uid === user.uid
  314. );
  315. if (idx >= 0) {
  316. this.user_published_list.splice(idx, 1);
  317. }
  318. }
  319. if (mediaType === 'video') {
  320. console.log('用户取消推流');
  321. // let remoteEle = document.getElementById('video');
  322. // if (remoteEle) {
  323. // remoteEle.textContent = '对方已离开直播间';
  324. // }
  325. // // this.client.leave();
  326. // this.alertTips('对方已离开直播间');
  327. }
  328. });
  329. // 注册用户离开事件处理函数
  330. this.client.on('user-left', (user: any) => {
  331. console.log(`用户 ${user.uid} 离开了频道`);
  332. //主播离开,停止计时计费
  333. if (user.uid !== 100001) {
  334. this.client.leave();
  335. this.alertTips('对方已离开直播间');
  336. }
  337. });
  338. this.client.on(
  339. 'connection-state-change',
  340. (curState: any, prevState: any) => {
  341. this.connection_state = curState;
  342. if (
  343. curState == 'RECONNECTING' ||
  344. curState == 'DISCONNECTED' ||
  345. curState == 'DISCONNECTING'
  346. ) {
  347. this.timer && clearTimeout(this.timer);
  348. this.timer_countdown && clearInterval(this.timer_countdown);
  349. if (!this.isAnchor) {
  350. //用户离开直播间,断开与主播状态连接频道
  351. this.msgSer.unsubscribeMessage(this.room?.get('user')?.id);
  352. this.isOpenEvaluate = true;
  353. }
  354. console.log(this.router.url);
  355. if (this.router.url.indexOf('/live/link-room/') == 0 && curState == 'DISCONNECTED'){
  356. localStorage.removeItem("isLive")
  357. history.back();
  358. }
  359. }
  360. console.log('live状态变更:', this.connection_state);
  361. }
  362. );
  363. this.monitorDevices();
  364. }
  365. async createLiveLog(uid: string) {
  366. let profile = JSON.parse(localStorage.getItem('profile') || '{}');
  367. let obj = Parse.Object.extend('LiveLog');
  368. let liveLog = new obj();
  369. liveLog.set('title', `与${profile?.name || profile?.mobile}进行直播`);
  370. liveLog.set('uid', String(uid));
  371. liveLog.set('company', {
  372. __type: 'Pointer',
  373. className: 'Company',
  374. objectId: this.company,
  375. });
  376. liveLog.set('room', {
  377. __type: 'Pointer',
  378. className: 'Room',
  379. objectId: this.room?.id,
  380. });
  381. liveLog.set('user', {
  382. __type: 'Pointer',
  383. className: '_User',
  384. objectId: Parse.User.current()?.id,
  385. });
  386. liveLog.set('profile', {
  387. __type: 'Pointer',
  388. className: 'Profile',
  389. objectId: this.room?.get('profile')?.id,
  390. });
  391. this.liveLog = await liveLog.save();
  392. }
  393. /* 连线成功立即创建直播记录,同步获取剩余可通话时长,并且开始计时 */
  394. async afterJoin() {
  395. this.timer && clearTimeout(this.timer);
  396. this.timer_countdown && clearInterval(this.timer_countdown);
  397. const targetUser = this.client.remoteUsers?.find(
  398. (user: any) => user.uid !== 100001
  399. ); //排出超管
  400. console.log(targetUser);
  401. if (this.client.remoteUsers.length > 0 && targetUser) {
  402. if (this.isAnchor) {
  403. //如果是主播,进入获取remoteUsers.user获取livelog再获取对方剩余通话时长
  404. this.getLiveLog(targetUser.uid);
  405. } else {
  406. let query = new Parse.Query('LiveLog');
  407. query.equalTo('objectId', this.liveLog?.id);
  408. query.equalTo('isLive', true);
  409. query.select('objectId');
  410. const resultData = await query.first();
  411. if (resultData?.id) {
  412. //首次进入至少计时2分钟,不足2分钟按2分钟计算扣时
  413. await this.publishSelf();
  414. await this.get_duration();
  415. this.computeDuration(60000 * 2);
  416. } else {
  417. this.timer = setTimeout(() => {
  418. this.afterJoin();
  419. }, 1000);
  420. }
  421. }
  422. } else {
  423. this.timer = setTimeout(() => {
  424. this.afterJoin();
  425. }, 1000);
  426. }
  427. }
  428. async getLiveLog(tarUid: string) {
  429. // let uid = this.client.remoteUsers[0].uid;
  430. this.timer && clearTimeout(this.timer);
  431. let query = new Parse.Query('LiveLog');
  432. query.equalTo('uid', String(tarUid));
  433. query.equalTo('room', this.room?.id);
  434. query.notEqualTo('isDeleted', true);
  435. query.notEqualTo('isLive', true);
  436. query.descending('createdAt');
  437. this.liveLog = await query.first();
  438. if (this.liveLog?.id) {
  439. this.liveLog?.set('isLive', true);
  440. await this.liveLog?.save();
  441. await this.get_duration();
  442. this.getCallDuration();
  443. return;
  444. }
  445. this.timer = setTimeout(() => {
  446. this.getLiveLog(tarUid);
  447. }, 1000);
  448. }
  449. async get_duration() {
  450. // this.timer_countdown && clearInterval(this.timer_countdown);
  451. let url = 'https://server.fmode.cn/api/ailiao/remain_second';
  452. let params = {
  453. rid: this.room?.id,
  454. uid: this.liveLog?.get('user')?.id || this.liveLog?.get('user')?.objectId,
  455. };
  456. let data = await this.http.httpRequst(url, params, 'POST');
  457. console.log(data);
  458. this.surplusNumber = data.data ?? 0;
  459. this.countdown = this.surplusNumber; // 初始化倒计时
  460. let arr = ['CONNECTING', 'CONNECTED'];
  461. if (arr.includes(this.connection_state as any)) {
  462. if (this.countdown <= 120) {
  463. this.alertTips('剩余通话时间不足2分钟,请及时充值');
  464. }
  465. !this.timer_countdown && this.startCountdown();
  466. }
  467. }
  468. startCountdown() {
  469. this.timer_countdown = setInterval(() => {
  470. if (this.countdown > 0) {
  471. this.countdown--;
  472. console.log(this.countdown);
  473. } else {
  474. clearInterval(this.timer_countdown);
  475. // history.back()
  476. this.alertTips('通话时间结束');
  477. this.client.leave(); // 结束通话
  478. }
  479. }, 1000);
  480. }
  481. /* 开始计时 */
  482. async computeDuration(num?: number) {
  483. //每10秒保存一次数据
  484. this.timer && clearTimeout(this.timer);
  485. let p = JSON.parse(this.profile);
  486. // console.log(p);
  487. let url = 'https://server.fmode.cn/api/ailiao/count/duration';
  488. let params = {
  489. company: this.company,
  490. profile: p?.objectId,
  491. rid: this.room?.id,
  492. uid: Parse.User.current()?.id,
  493. duration: num ? num / 1000 : 10,
  494. logid: this.liveLog?.id,
  495. };
  496. let data = await this.http.httpRequst(url, params, 'POST');
  497. // console.log(data);
  498. this.timer = setTimeout(() => {
  499. this.computeDuration();
  500. }, num ?? 10000);
  501. }
  502. /* 主播每10s获取一次对方可通话时长 */
  503. getCallDuration() {
  504. this.timer && clearTimeout(this.timer);
  505. this.timer = setTimeout(async () => {
  506. await this.get_duration();
  507. let arr = ['CONNECTING', 'CONNECTED'];
  508. if (arr.includes(this.connection_state as any)) {
  509. this.getCallDuration();
  510. }
  511. }, 10000);
  512. }
  513. /* 监听音视频设备插拔 */
  514. monitorDevices() {
  515. AgoraRTC.onMicrophoneChanged = async (changedDevice: any) => {
  516. // 插入麦克风设备时,切换到新插入的设备
  517. if (changedDevice.state === 'ACTIVE') {
  518. this.localTracks.audioTrack.setDevice(changedDevice.device.deviceId);
  519. this.currentUsedDevice.audioDevice = changedDevice.device.deviceId;
  520. // 拔出设备为当前设备时,切换到一个已有的设备
  521. } else if (
  522. changedDevice.device.label ===
  523. this.localTracks.audioTrack.getTrackLabel()
  524. ) {
  525. const oldMicrophones = await AgoraRTC.getMicrophones();
  526. oldMicrophones[0] &&
  527. this.localTracks.audioTrack.setDevice(oldMicrophones[0].deviceId);
  528. this.currentUsedDevice.audioDevice = oldMicrophones[0].deviceId;
  529. }
  530. };
  531. AgoraRTC.onCameraChanged = async (changedDevice: any) => {
  532. // 插入相机设备时,切换到新插入的设备
  533. if (changedDevice.state === 'ACTIVE') {
  534. this.localTracks.videoTrack.setDevice(changedDevice.device.deviceId);
  535. this.currentUsedDevice.videoDevice = changedDevice.device.deviceId;
  536. // 拔出设备为当前设备时,切换到一个已有的设备
  537. } else if (
  538. changedDevice.device.label ===
  539. this.localTracks.videoTrack.getTrackLabel()
  540. ) {
  541. const oldCameras = await AgoraRTC.getCameras();
  542. oldCameras[0] &&
  543. this.localTracks.videoTrack.setDevice(oldCameras[0].deviceId);
  544. this.currentUsedDevice.videoDevice = oldCameras[0].deviceId;
  545. }
  546. };
  547. }
  548. /* 关开麦 */
  549. async updatePublishedAudioTrack() {
  550. this.tools['audio'] = !this.tools['audio'];
  551. if (this.tools['audio'] && this.localTracks.audioTrack) {
  552. // await this.localTracks.audioTrack.setEnabled(false);
  553. this.localTracks.audioTrack.setVolume(0);
  554. console.log('停止推送音频');
  555. } else {
  556. // await this.localTracks.audioTrack.setEnabled(true);
  557. this.localTracks.audioTrack.setVolume(100);
  558. console.log('恢复推送音频');
  559. }
  560. // await this.client.unpublish(this.localTracks.audioTrack);
  561. return true;
  562. }
  563. /* 静音 */
  564. async muteAudio() {
  565. this.tools['mute'] = !this.tools['mute'];
  566. let list = Array.from(this.user_published_list);
  567. for (let index = 0; index < list.length; index++) {
  568. const user = list[index];
  569. if (this.tools['mute']) {
  570. console.log('静音');
  571. await this.client.unsubscribe(user, 'audio');
  572. } else {
  573. await this.client.subscribe(user, 'audio').then((audioTrack: any) => {
  574. audioTrack.setVolume(100);
  575. audioTrack.play();
  576. });
  577. console.log('恢复声音');
  578. }
  579. }
  580. }
  581. /* 切换摄像头 */
  582. async changeCamera() {
  583. const oldCameras = await AgoraRTC.getCameras(true);
  584. // console.log('oldCameras:', oldCameras);
  585. let newCamers = oldCameras.find(
  586. (item: any) => item.deviceId !== this.currentUsedDevice.videoDevice
  587. );
  588. console.log(newCamers);
  589. newCamers &&
  590. this.localTracks.videoTrack
  591. .setDevice(newCamers.deviceId)
  592. .then(() => {
  593. this.tools['camera'] = !this.tools['camera'];
  594. this.currentUsedDevice.videoDevice = newCamers.deviceId;
  595. })
  596. .catch((err: any) => {
  597. console.log('set device error', err);
  598. this.tools['camera'] = !this.tools['camera'];
  599. this.currentUsedDevice.videoDevice = newCamers.deviceId;
  600. // this.alertTips('切换摄像头失败');
  601. });
  602. // await this.localTracks.videoTrack.setDevice({facingMode: this.tools['camera'] ? 'user' : 'environment'})
  603. // this.tools['camera'] = !this.tools['camera'];
  604. // this.currentUsedDevice.videoDevice = newCamers.deviceId;
  605. return true;
  606. }
  607. /* 切换美颜 */
  608. changeBeauty() {
  609. if (!this.tools['glorify']) {
  610. this.setBeautyOptions();
  611. } else {
  612. // 关闭美颜
  613. this.processor.setOptions({
  614. lighteningContrastLevel: 0,
  615. lighteningLevel: 0,
  616. smoothnessLevel: 0,
  617. sharpnessLevel: 0,
  618. rednessLevel: 0,
  619. });
  620. this.tools['glorify'] = false;
  621. }
  622. }
  623. /* 提示 */
  624. async alertTips(message: string, title?: string, callBack?: Function) {
  625. this.alert = await this.alertController.create({
  626. header: title || '提示',
  627. message: message,
  628. backdropDismiss: false,
  629. buttons: [
  630. {
  631. text: '确认',
  632. handler: () => {
  633. callBack && callBack();
  634. },
  635. },
  636. ],
  637. });
  638. await this.alert.present();
  639. }
  640. }