


Detailed example of how uniapp implements phone recording function (with code)
Jan 05, 2023 pm 04:41 PMThis article brings you relevant knowledge about uniapp. It mainly introduces how to use uniapp to make calls and synchronize recording. Friends who are interested should take a look at it. I hope it will be helpful to everyone. .
uniapp realizes the call recording function
Recently, I need to implement a function of calling the mobile phone to make a call through uniapp. After making the call, it will be recorded at the same time. After hanging up the call, Upload the recording file and now share some core codes with everyone!
const recorderManager = uni.getRecorderManager(); onLoad(option) { let self = this; recorderManager.onStop(function (res) { console.log("res",res) self.end_time = Math.round(new Date().getTime() / 1000); let voicePath = res.tempFilePath; self.voicePath = voicePath; self.closeTimeOut(); uni.showToast({ icon: 'loading', title: "請稍后...", duration: 0 }); uni.uploadFile({ url: self.upload_url, filePath: voicePath, name: "file", formData: { id: self.phoneInfo.id, start_time: self.start_time, end_time: self.end_time, phone: self.phoneNumber }, header: { Authorization: "Bearer " + uni.getStorageSync(EnumData.token) }, success: (res) => { // console.log("文件上傳成功") console.log(res.data); }, fail(err) { console.log("文件上傳失敗") console.log(err); }, complete() { self.start_time = 0; self.end_time = 0; uni.hideToast(); } }) }); this.getCallStatus(); } getCallStatus() { let that = this; let maintest = plus.android.runtimeMainActivity(); let Contexttest = plus.android.importClass("android.content.Context"); let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager"); let telManager = plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE); let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', { onReceive: function (Contexttest, intent) { plus.android.importClass(intent); let phoneStatus = telManager.getCallState(); that.callStatus = phoneStatus; //電話狀態(tài) 0->空閑狀態(tài) 1->振鈴狀態(tài) 2->通話存在 switch (phoneStatus) { case 0: console.log("3、電話掛斷,上傳錄音") // 結(jié)束錄音 recorderManager.stop(); break; case 1: // console.log('1、振鈴狀態(tài)'); break; case 2: console.log('2、通話存在') // 延遲錄音 that.start_time = Math.round(new Date().getTime() / 1000); recorderManager.start({ duration: EnumData.audioDuration, // 時長 10分鐘 sampleRate: EnumData.audioSampleRate, // 碼率 }); break; } } }); let IntentFilter = plus.android.importClass('android.content.IntentFilter'); let filter = new IntentFilter(); filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED); maintest.registerReceiver(receiver, filter); },
The permissions you need to apply for can be placed in App.vue
if (plus.os.name == 'Android') { plus.android.requestPermissions( ['android.permission.ANSWER_PHONE_CALLS',//手動 掛斷和接聽 需要這個權(quán)限 "android.permission.MODIFY_AUDIO_SETTINGS",//手動 掛斷和接聽 需要這個權(quán)限 "android.permission.CALL_PHONE",//手動 掛斷和接聽 需要這個權(quán)限 "android.permission.READ_PHONE_STATE",//>監(jiān)聽電話狀態(tài) 需要這個權(quán)限 "android.permission.READ_CALL_LOG",//獲取號碼需要這個權(quán)限 "android.permission.READ_AUDIO" // 錄音權(quán)限 ], function(resultObj) { var result = 0; for (var i = 0; i < resultObj.granted.length; i++) { var grantedPermission = resultObj.granted[i]; console.log('已獲取的權(quán)限:' + grantedPermission); result = 1 } for (var i = 0; i < resultObj.deniedPresent.length; i++) { var deniedPresentPermission = resultObj.deniedPresent[i]; console.log('拒絕本次申請的權(quán)限:' + deniedPresentPermission); result = 0 } for (var i = 0; i < resultObj.deniedAlways.length; i++) { var deniedAlwaysPermission = resultObj.deniedAlways[i]; console.log('永久拒絕申請的權(quán)限:' + deniedAlwaysPermission); result = -1 } }, function(error) { console.log('申請權(quán)限錯誤:' + error.code + " = " + error.message); } ); }
Note
It can be used in debug mode It normally monitors call hang-ups and uploads files, but it becomes invalid after packaging?
General mobile phone packaging can be used normally. I use oneplus7. After packaging, it can be installed and used normally. Some mobile phones need to go to the permissions of the system settings and change the application's [Open the phone [Device Status Code] Just turn it on. This is what I have encountered so far.
Recommended learning: "uni-app video tutorial"
The above is the detailed content of Detailed example of how uniapp implements phone recording function (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to develop uni-app in VSCode? The following article will share with you a tutorial on developing uni-app in VSCode. This may be the best and most detailed tutorial. Come and take a look!

How to use uniapp to develop a simple map navigation? This article will provide you with an idea for making a simple map. I hope it will be helpful to you!

uni-app interface, global method encapsulation 1. Create an api file in the root directory, create api.js, baseUrl.js and http.js files in the api folder 2.baseUrl.js file code exportdefault"https://XXXX .test03.qcw800.com/api/"3.http.js file code exportfunctionhttps(opts,data){lethttpDefaultOpts={url:opts.url,data:data,method:opts.method

This article will guide you step by step in developing a uni-app calendar plug-in, and introduce how the next calendar plug-in is developed from development to release. I hope it will be helpful to you!

How to use uniapp to develop a snake game? The following article will take you step by step to implement the Snake game in uniapp. I hope it will be helpful to you!

This article brings you relevant knowledge about uniapp. It mainly introduces how to use uniapp to make calls and synchronize recording. Friends who are interested should take a look at it. I hope it will be helpful to everyone.

This article brings you relevant knowledge about uniapp, which mainly organizes the related issues of implementing the select-all function of the multi-select box. The reason why the select-all function cannot be implemented is that when the checked field of the checkbox is dynamically modified, the status on the interface can Real-time changes, but the change event of checkbox-group cannot be triggered. Let's take a look at it. I hope it will be helpful to everyone.

How does uniapp implement scroll-view drop-down loading? The following article talks about the drop-down loading of the uniapp WeChat applet scroll-view. I hope it will be helpful to everyone!
