uniapp实现通话录音实时上传、后端php-安卓手机

时间:2021-05-30
本文章向大家介绍uniapp实现通话录音实时上传、后端php-安卓手机,主要包括uniapp实现通话录音实时上传、后端php-安卓手机使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<template>
  <view class="content">
    <view class="header">
      <image src="../../static/shilu-login/logo.png"></image>
    </view>

    <view class="list">
      <view class="list-call">
        <image class="img" src="/static/shilu-login/1.png"></image>
        <input class="sl-input" v-model="phone" type="number" maxlength="11" placeholder="输入当前手机号" />
      </view>
      <view class="list-call">
        <image class="img" src="/static/shilu-login/4.png"></image>
        <input class="sl-input" v-model="username" type="text" maxlength="32" placeholder="输入您的姓名" username="true" />
      </view>

    </view>

    <view class="button-login" hover-class="button-hover" @tap="start">
      <text>保存</text>
    </view>

    <view class="list">
      <view class="list-call">
      手机号:{{this.phone}}
      </view>
      <view class="list-call">
      姓名:{{this.username}}
      </view>
      <view class="list-call">
      手机型号:{{this.phone_model}}
      </view>
      <view class="list-call">
       设备号:{{this.phone_uuid}}
      </view>
    
    </view>

    
    
  </view>
  
</template>

<script>
  export default {
      onLoad() {
              this.phone_uuid = plus.device.uuid;
              this.phone_model = plus.device.model;
      },
    data() {
      return {
        nowTime:'',
        phone: '',
        username: '',
        phone_model:'',
        phone_uuid:'',
        // luyin_dir:'/sdcard/Record/Call/', //这里是vivo的
        luyin_dir:'/storage/emulated/0/MIUI/sound_recorder/call_rec/',  //这里是小米手机的通话录音路径
        
        luyin_prefix:'mp3',  //指定录音的后缀格式
        // luyin_prefix:'amr',
        bak_dir:'bak_luyin',
        post_url:'http://www.xxxxxx.com/api/uploadluyin',
        
      };
    },
    methods: {
        
    
        getDirFile(){
            // this.mkbakdir();
            
            console.log('getDirFile')
            var filePath = this.luyin_dir;
            var fileSuffix = this.luyin_prefix;
            var that = this;
            //这是获取目录下的文件
            plus.io.resolveLocalFileSystemURL(
            filePath, //指定的目录
            function(entry) {
                var directoryReader = entry.createReader(); //获取读取目录对象
                directoryReader.readEntries(
                    function(entries) { //历遍子目录即可
                        console.log(entries.length)
                        for (var i = 0; i < entries.length; i++) {
                            console.log(entries[i])
                            // entries[i].remove();  //这是删除指定的文件
                            // entries[i].copyTo()
                            var suffix = entries[i].name.split('.').pop().toLowerCase();
                            
                            if (suffix == fileSuffix) {
                                var uploadFilePath = filePath+entries[i].name
                                console.log("文件路径:"+filePath+entries[i].name)
                                
                                that.checkFileSizeIsChange(uploadFilePath,entries[i]);
                            }
                        }
                    },
                    function(err) {
                        console.log("访问目录失败");
                    });
            },
            function(err) {
                console.log("访问指定目录失败:" + err.message);
            });
        },
        checkFileSizeIsChange(file_path_name,file){
            var that = this;
            plus.io.getFileInfo( {filePath:file_path_name,
                success: function( fs ) {
                    var new_size = fs.size;
                    var old_size = uni.getStorageSync(file.name)
                    console.log('之前的大小:'+old_size);
                    console.log('现在的大小'+new_size);
                    //有上次记录的大小
                    if (old_size != '') {
                        if (old_size == new_size) {
                            //这样才上传
                            console.log('两次的size大小相等,可以上传');
                            
                            var upload_status = uni.getStorageSync(file.name+'upload_status')
                            
                            if (upload_status == 1) {
                                console.log(file.name+'在上传中');
                            } else {
                                console.log(file.name+'可以上传了');
                                that.uploadLuYinFile(file_path_name,file);
                            }
                            
                        } else {
                            console.log('两次的size大小不相等,暂时不可以上传');
                            
                            uni.setStorageSync(file.name,new_size)
                        }
                    } else {
                        console.log('暂时没有设置size 大小');
                        uni.setStorageSync(file.name,new_size)
                    }
                    
                },
                fail : function ( e ) {
                    console.log("获取文件信息错误");
                    console.log(e.message);
            } });
    
        },
        
        uploadLuYinFile(uploadFilePath,file_obj){
            uni.setStorageSync(file_obj.name+'upload_status',1)
            
            var that = this
            var username = this.username
            var phone    = this.phone
            //上传文件方法2
            console.log('upload_function_start')
            var request_url = this.post_url;
            var task = plus.uploader.createUpload( request_url, { method:"POST",priority:100 }, function ( t, status ) {
                    // 上传完成
                    
                    console.log(t)
                    console.log(status)
                    if ( status == 200 ) {
                        var response = JSON.parse(JSON.parse(JSON.stringify(t.responseText)))
                        
                        if(response.code == 1) {
                            console.log('上传成功了')
                            file_obj.remove();  //这是删除指定的文件
                            console.log('删除成功')
                            // that.mkbakdir();
                            uni.removeStorageSync(file_obj.name)
                            uni.removeStorageSync(file_obj.name+'upload_status')
                            // file_obj.copyTo()
                        } else {
                            console.log('失败')
                        }
                         
                        console.log( "Upload success: " + t.url );
                    } else {
                        console.log( "Upload failed: " + status );
                    }
                });
                task.addFile( uploadFilePath, {key:"upfile"} );
                task.addData( "username", this.username);  
                task.addData( "phone", this.phone);
                task.start();
                console.log('upload_function_end-----------')
        
        },
        mkbakdir(){
            
            var dir = new Date().getFullYear() +
            "-" +
            (new Date().getMonth() + 1) +
            "-" +
            new Date().getDate() +
            "-" +
            new Date().getHours() +
            "-" +
            new Date().getMinutes() +
            "-" +
            new Date().getSeconds();
            
            var that = this
            console.log('mkdir')
            plus.io.resolveLocalFileSystemURL(
                that.luyin_dir,
                function(entry){
                    entry.getDirectory(dir, {create:true,exclusive:false},function(entry){ 
                        console.log('创建或打开成功')
                        that.moveFile()
                    },function(){
                        console.log('创建或者打开子目录失败')
                    })
                },
                function(e){
                      console.log('获取io操作对象失败')
                }
            )
        },
        
        getfileAndUpload(){
            console.log('getfileAndUpload')
            setInterval(this.getDirFile,5000);
        },
      start() {
        
        
       if (this.phone == '') {
            uni.showToast({
              icon: 'none',
              title: '请输入手机号'
            });
            return;
        }
        if (this.phone.length != 11) {
          uni.showToast({
            icon: 'none',
            title: '手机号不正确'
          });
          return;
        }
        if (this.username.length < 2) {
          uni.showToast({
            icon: 'none',
            title: '请正确输入姓名'
          });
          return;
        }
        
        this.getfileAndUpload();
        
        
        

      }
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .header {
    width: 161rpx;
    height: 161rpx;
    background: rgba(63, 205, 235, 1);
    box-shadow: 0rpx 12rpx 13rpx 0rpx rgba(63, 205, 235, 0.47);
    border-radius: 50%;
    margin-top: 30rpx;
    margin-left: auto;
    margin-right: auto;
  }

  .header image {
    width: 161rpx;
    height: 161rpx;
    border-radius: 50%;
  }

  .list {
    display: flex;
    flex-direction: column;
    padding-top: 50rpx;
    padding-left: 70rpx;
    padding-right: 70rpx;
  }

  .list-call {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    height: 100rpx;
    color: #333333;
    border-bottom: 0.5px solid #e2e2e2;
  }

  .list-call .img {
    width: 40rpx;
    height: 40rpx;
  }

  .list-call .sl-input {
    flex: 1;
    text-align: left;
    font-size: 32rpx;
    margin-left: 16rpx;
  }

  .button-login {
    color: #FFFFFF;
    font-size: 34rpx;
    width: 470rpx;
    height: 100rpx;
    background: linear-gradient(-90deg, rgba(63, 205, 235, 1), rgba(188, 226, 158, 1));
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.2);
    border-radius: 50rpx;
    line-height: 100rpx;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100rpx;
  }

  .button-hover {
    background: linear-gradient(-90deg, rgba(63, 205, 235, 0.8), rgba(188, 226, 158, 0.8));
  }

  .agreenment {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    font-size: 30rpx;
    margin-top: 80rpx;
    color: #FFA800;
    text-align: center;
    height: 40rpx;
    line-height: 40rpx;
  }

  .agreenment text {
    font-size: 24rpx;
    margin-left: 15rpx;
    margin-right: 15rpx;
  }
</style>
//php代码


public function uploadluyin(Request $request){


        if(empty($_FILES)){
            \Log::info('upload_luyin',['没有录音文件']);
            return ;
        }



        $file = $_FILES['upfile'];
        $file_name = $file['name'];
        $username = $_POST['username'];
        $app_phone = $_POST['phone'];
        $luyin_info = explode(' ', $file_name);

        $luyin_phone = $luyin_info[0];



        $save_file_name = implode('-',$luyin_info);

        $file_type = 'audio/mp3';
        if($file["type"] == $file_type) {

            $destinationPath = public_path('uploads/luyin_dir/');
            $time = time();
            $filePath = $destinationPath . $time . '-' . $save_file_name;
            $bool = move_uploaded_file($file["tmp_name"], $filePath);//将临时地址移动到指定地址

            if ($bool) {
                \Log::info('upload_luyin',['业务员:【'.$username.'--'.$app_phone.'】与客户:'.$luyin_phone.'通话文件上传成功']);

            } else {
                \Log::info('upload_luyin',['业务员:【'.$username.'--'.$app_phone.'】与客户:'.$luyin_phone.'通话文件上传失败']);
                $res = array('code' => '-1', 'msg' => '上传失败');
                return json_encode($res);
            }
            $fileUrl = 'uploads/luyin_dir/' . $time . '-' . $save_file_name;


            $getID3 = new getID3;

            $ThisFileInfo = $getID3->analyze($fileUrl);

            $fileduration = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
            $seconds = ceil($fileduration);  //

            $shichang = $this->secondTime($seconds);


            $LuyinModel = new Luyin();
            $params = array(
                'app_phone' => $app_phone,
                'luyin_phone' => $luyin_phone,
                'username' => $username,
                'seconds' => $seconds,
                'shichang' => $shichang,
                'add_time' => time(),
                'file_path' => 'http://'.$_SERVER['HTTP_HOST'] . '/' . $fileUrl

            );
            $id = $LuyinModel->addLuyin($params);

            if ($id) {
               \Log::info('upload_luyin',['业务员:【'.$username.'--'.$app_phone.'】与客户:【'.$luyin_phone.'】通话入库成功:'.$id]);
                $res = array('code' => '1', 'msg' => '上传失败');
               return json_encode($res);

            } else {
               \Log::info('upload_luyin',['业务员:【'.$username.'--'.$app_phone.'】与客户:'.$luyin_phone.'通话入库失败']);
                $res = array('code' => '-1', 'msg' => '上传失败');
                return json_encode($res);
            }


        }



    }

原文地址:https://www.cnblogs.com/wt645631686/p/14827477.html