Home Web Front-end uni-app Detailed explanation of how uniapp implements phone call recording function (with core code attached)

Detailed explanation of how uniapp implements phone call recording function (with core code attached)

Apr 06, 2023 pm 03:00 PM
uniapp

This article brings you relevant knowledge about uniapp. It mainly introduces how uniapp implements the call recording function. Friends who are interested can take a look below. I hope it will be helpful to everyone.

Recently I need to implement a function of calling a mobile phone through uniapp. After making a call, I will record at the same time. After hanging up the phone, I will upload the recording file. Now I will share some core codes with everyone. I hope it will be helpful to everyone

const recorderManager = uni.getRecorderManager();
Copy after login
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();
}
Copy after login
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; //电话状态 0->空闲状态 1->振铃状态 2->通话存在
            switch (phoneStatus) {
                case 0:
                    console.log("3、电话挂断,上传录音")
                    // 结束录音
                    recorderManager.stop();
                    break;
                case 1:
                    // console.log('1、振铃状态');
                    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);
},
Copy after login

The permissions that need to be applied for can be placed in App.vue

if (plus.os.name == 'Android') {
      plus.android.requestPermissions(
         ['android.permission.ANSWER_PHONE_CALLS',//手动 挂断和接听 需要这个权限
          "android.permission.MODIFY_AUDIO_SETTINGS",//手动 挂断和接听 需要这个权限
          "android.permission.CALL_PHONE",//手动 挂断和接听 需要这个权限
          "android.permission.READ_PHONE_STATE",//>监听电话状态 需要这个权限
          "android.permission.READ_CALL_LOG",//获取号码需要这个权限
          "android.permission.READ_AUDIO" // 录音权限
          ],
      function(resultObj) {
          var result = 0;
          for (var i = 0; i < resultObj.granted.length; i++) {
          var grantedPermission = resultObj.granted[i];
          console.log('已获取的权限:' + grantedPermission);
          result = 1
      }
      for (var i = 0; i < resultObj.deniedPresent.length; i++) {
          var deniedPresentPermission = resultObj.deniedPresent[i];
          console.log('拒绝本次申请的权限:' + deniedPresentPermission);
          result = 0
      }
      for (var i = 0; i < resultObj.deniedAlways.length; i++) {
          var deniedAlwaysPermission = resultObj.deniedAlways[i];
          console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
          result = -1
      }
     },  function(error) {
          console.log('申请权限错误:' + error.code + " = " + error.message);
     } );
}
Copy after login

Notes

1. In debugging mode, the call hangup can be monitored normally And I uploaded the file, but it becomes invalid after being packaged?
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 turn on the [Turn on mobile device status code] of the application. This is what I've encountered so far.                                                                                                                                                                                 

The above is the detailed content of Detailed explanation of how uniapp implements phone call recording function (with core code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start preview of uniapp project developed by webstorm How to start preview of uniapp project developed by webstorm Apr 08, 2024 pm 06:42 PM

How to start preview of uniapp project developed by webstorm

Which one is better, uniapp or mui? Which one is better, uniapp or mui? Apr 06, 2024 am 05:18 AM

Which one is better, uniapp or mui?

What development tools do uniapp use? What development tools do uniapp use? Apr 06, 2024 am 04:27 AM

What development tools do uniapp use?

What basics are needed to learn uniapp? What basics are needed to learn uniapp? Apr 06, 2024 am 04:45 AM

What basics are needed to learn uniapp?

What are the disadvantages of uniapp What are the disadvantages of uniapp Apr 06, 2024 am 04:06 AM

What are the disadvantages of uniapp

Which is better, uniapp or native development? Which is better, uniapp or native development? Apr 06, 2024 am 05:06 AM

Which is better, uniapp or native development?

In-depth comparison between Flutter and uniapp: explore their similarities, differences and characteristics In-depth comparison between Flutter and uniapp: explore their similarities, differences and characteristics Dec 23, 2023 pm 02:16 PM

In-depth comparison between Flutter and uniapp: explore their similarities, differences and characteristics

What component library does uniapp use to develop small programs? What component library does uniapp use to develop small programs? Apr 06, 2024 am 03:54 AM

What component library does uniapp use to develop small programs?

See all articles