From 524abed53193c67683a363ce0f12fe4aa98323a0 Mon Sep 17 00:00:00 2001
From: lmw <125975490@qq.com>
Date: 星期二, 28 五月 2024 15:28:55 +0800
Subject: [PATCH] 行程录音,接送机

---
 app/src/main/java/com/future/driver/ui/main/MainActivity.kt |  108 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 96 insertions(+), 12 deletions(-)

diff --git a/app/src/main/java/com/future/driver/ui/main/MainActivity.kt b/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
index f1b5833..ac6d2b0 100644
--- a/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
+++ b/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
@@ -6,8 +6,8 @@
 import android.os.Handler
 import android.os.Looper
 import android.provider.Settings
+import android.util.Log
 import android.widget.Toast
-import androidx.core.app.ActivityCompat
 import cn.sinata.rxnetty.NettyClient
 import cn.sinata.xldutils.utils.*
 import com.amap.api.location.AMapLocation
@@ -30,8 +30,10 @@
 import com.future.driver.ui.main.event.MineEventActivity
 import com.future.driver.ui.mine.MineActivity
 import com.future.driver.ui.mine.MsgActivity
+import com.future.driver.utils.AudioRecordUtils
 import com.future.driver.utils.Cache.CacheKey
 import com.future.driver.utils.DateUtil
+import com.future.driver.utils.OSSUtil
 import com.future.driver.utils.download.DownloadUtil
 import com.google.gson.Gson
 import com.lljjcoder.style.citylist.Toast.ToastUtils
@@ -42,8 +44,8 @@
 import org.jetbrains.anko.startActivity
 import org.jetbrains.anko.toast
 
-
-class MainActivity : MyBaseActivity(), AMapLocationListener {
+class MainActivity : MyBaseActivity(), AMapLocationListener,
+    AudioRecordUtils.OnAudioStatusUpdateListener {
 
     val mainFragment by lazy {
         MainFragment()
@@ -55,23 +57,34 @@
         setContentView(R.layout.activity_main)
     }
 
+    private var recorder:AudioRecordUtils? = null  //录音器
+    private val VOICE_LENGTH = 1000L*60*5 //实际5分钟一个片段
+//    private val VOICE_LENGTH = 1000L*15 //测试15秒一个片段
+
     override fun initView() {
         MyApplication.canPlayVoice = true
 //        var packageName = "com.android.providers.downloads";
 //        var intent =  Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
 //    intent.setData(Uri.parse("package:" + packageName));
 //    startActivity(intent);
-        if (CacheKey.getKeyStr(Const.REFUSE_LOCATION).isNotEmpty()&&!RxPermissions(this).isGranted(Manifest.permission.ACCESS_FINE_LOCATION)){
+        if (CacheKey.getKeyStr(Const.REFUSE_LOCATION).isNotEmpty()&&!RxPermissions(this).isGranted(
+                Manifest.permission.ACCESS_FINE_LOCATION
+            )){
             toast("没有定位权限,您将无法收到新订单推送")
         }else{
             if (!RxPermissions(this).isGranted(Manifest.permission.ACCESS_FINE_LOCATION)){
-                DialogUtil.getDelAndSureDialog(this,"去授权","否","《未来出行司机》需要获取您的当前位置信息,用于查询当前城市是否开通服务,我们会将您的位置实时上传至后端为您匹配和推送附近订单。如果拒绝定位权限,您将无法正常接单。",{
-                }){
+                DialogUtil.getDelAndSureDialog(
+                    this,
+                    "去授权",
+                    "否",
+                    "《未来出行司机》需要获取您的当前位置信息,用于查询当前城市是否开通服务,我们会将您的位置实时上传至后端为您匹配和推送附近订单。如果拒绝定位权限,您将无法正常接单。",
+                    {
+                    }){
                     val subscribe =
                         RxPermissions(this).request(Manifest.permission.ACCESS_FINE_LOCATION).subscribe {
                             if (!it){
                                 toast("没有定位权限,您将无法收到新订单推送")
-                                CacheKey.putKeyStr(Const.REFUSE_LOCATION,"1")
+                                CacheKey.putKeyStr(Const.REFUSE_LOCATION, "1")
                             }
                         }
                 }
@@ -96,7 +109,7 @@
         override fun orderInfo(data: OrderSimpleData?) {
             data?.let {
                 when(it.orderType){
-                    1,2,3 -> {
+                    1, 2, 3 -> {
                         if (it.status == 10 || it.status == 12) {
                             Handler(Looper.getMainLooper()).post {
                                 mainFragment?.refresh()
@@ -112,7 +125,7 @@
 
     private fun checkGps() {
         if (!AMapKit.isLocServiceEnable(this)){
-            DialogUtil.getDelAndSureDialog(this,"查看定位","退出","您暂未开启手机定位GPS,是否前往开启?",{
+            DialogUtil.getDelAndSureDialog(this, "查看定位", "退出", "您暂未开启手机定位GPS,是否前往开启?", {
                 onBackPressed()
             }){
                 val intent = Intent()
@@ -163,7 +176,7 @@
         mainFragment?.refresh()
     }
 
-    fun beOnDuty(b:Boolean){
+    fun beOnDuty(b: Boolean){
         if (b){
             avv_anim.visible()
             tv_up_avv.visible()
@@ -274,7 +287,7 @@
                     }else{
                         var map= getMapByAny()
                         map["type"] = bean[0].id
-                        callNet(Api.work,map) {
+                        callNet(Api.work, map) {
                             toast("上班成功")
                             beOnDuty(true)
                             EventBus.getDefault().post(BaseEvent(BaseEvent.START_CAR))
@@ -325,16 +338,87 @@
             BaseEvent.FACE_FAILED -> {
                 tv_face.visible()
             }
+            BaseEvent.START_RECORD -> {
+                if (!MyApplication.isRecording)
+                    startRecord()
+            }
+            BaseEvent.FINISH_RECORD -> {
+                recorder?.stopRecord()
+            }
         }
     }
 
     override fun onLocationChanged(p0: AMapLocation?) {
         if (p0?.errorCode == 0) {
             MyApplication.aMapLocation = p0
-            CacheKey.putKeyStr("location",Gson().toJson(p0))
+            CacheKey.putKeyStr("location", Gson().toJson(p0))
         }
     }
 
+    private fun startRecord(){
+        if (recorder == null){
+            recorder = AudioRecordUtils(getExternalFilesDir(null)?.path+"/")
+            recorder?.setOnAudioStatusUpdateListener(this)
+        }
+        if (RxPermissions(this).isGranted(Manifest.permission.RECORD_AUDIO)&&RxPermissions(this).isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
+            Log.e("mmp","订单${MyApplication.recordOrderId} 开始录音...")
+            recorder?.startRecord()
+        }
+        else
+            toast("缺少录音相关权限")
+    }
+
+    private fun updateVoice(filePath: String) {
+        val ossUtil = OSSUtil(this)
+        ossUtil.uploadSingleWithSize(filePath, object : OSSUtil.OSSUploadCallBack() {
+            override fun onFinishWithSize(url: String?, size: Long) {
+                super.onFinishWithSize(url, size)
+                Log.e("mmp","录音保存路径:${url},大小:${size/1024}kb")
+                val mapByAny = getMapByAny()
+                mapByAny["fileFormat"] = "caf"
+                mapByAny["fileLink"] = url
+                mapByAny["fileSize"] = size/1024
+                mapByAny["orderId"] = MyApplication.recordOrderId
+                mapByAny["orderType"] = MyApplication.recordOrderType
+                callNet(false,Api.tape,mapByAny){
+
+                }
+            }
+
+            override fun onFial(message: String?) {
+                super.onFial(message)
+                runOnUiThread {
+                    Toast.makeText(this@MainActivity, "录音上传失败,请检查网络", Toast.LENGTH_SHORT).show()
+                }
+            }
+        })
+    }
+
+    override fun onUpdate(db: Double, time: Long) {
+        MyApplication.isRecording = true
+        if (time >= VOICE_LENGTH) {
+            Log.e("mmp","订单${MyApplication.recordOrderId} 录音片段完成")
+            recorder?.stopRecord() //结束录音
+        }
+    }
+
+    override fun onStop(filePath: String) {
+        MyApplication.isRecording = false
+        Thread {
+            updateVoice(filePath)
+        }.start()
+        if (MyApplication.currentOrderId.isNotEmpty()){//行程未结束 继续录制下一段
+            Log.e("mmp","订单${MyApplication.recordOrderId} 开始下一片段录音")
+            recorder?.startRecord()
+        }
+    }
+
+    override fun onStartPlay() {
+    }
+
+    override fun onFinishPlay() {
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         MyApplication.canPlayVoice = false

--
Gitblit v1.7.1