PHP前端开发

详解uniapp实现打电话录音功能(附核心代码)

百变鹏仔 4周前 (11-20) #uniapp
文章标签 详解

本篇文章给大家带来了关于uniapp的相关知识,其中主要跟大家介绍uniapp怎么实现打电话录音功能,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

最近需要实现一个通过uniapp调用手机拨打电话的功能,拨打之后同时录音,挂断电话之后将录音文件进行上传,现在将几个核心代码分享给大家,希望对大家有帮助

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; //电话状态 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);},

需要申请的权限,可以放到 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",//&gt;监听电话状态 需要这个权限          "android.permission.READ_CALL_LOG",//获取号码需要这个权限          "android.permission.READ_AUDIO" // 录音权限          ],      function(resultObj) {          var result = 0;          for (var i = 0; i <p>注意点</p><p>1、调试模式下可以正常监听通话挂断并且上传文件的,但是打包之后就失效?<br>一般的手机打包是可以正常使用的,我用的是oneplus7 ,打包后安装正常使用,部分手机需要到系统设置的权限中,将应用的【开启手机设备状态码】进行开启即可,目前遇到的就这个。<span class="rm-link-color">                       </span> </p><p>推荐学习:《<a href="https://www.php.cn/course/list/58.html" target="_blank">uniapp视频教程</a>》                            </p>