Pārlūkot izejas kodu

implement AudioRecorder & fix bug

cooperku_kdanmobile 6 gadi atpakaļ
vecāks
revīzija
3cfc6529be

+ 16 - 0
src/main/java/com/bomostory/sceneeditmodule/AudioRecorder.kt

@@ -100,6 +100,22 @@ class AudioRecorder {
         lock.unlock()
     }
 
+    public fun cancelRecording() {
+        lock.lock()
+        var filename = file?.absolutePath
+        if (isRecording()) {
+            audioRecord?.stop()
+            recordingFlag = false;
+        }
+        else {
+            release()
+        }
+        if (filename != null) {
+            File(filename).delete()
+        }
+        lock.unlock()
+    }
+
     public fun isRecording(): Boolean {
         return recordingFlag
     }

+ 34 - 5
src/main/java/com/bomostory/sceneeditmodule/SceneEditActivity.kt

@@ -68,6 +68,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
     private var editTextDialog = EditTextDialog()
     private val monitorSize = DisplayMetrics()
     private var timer = Timer()
+    private val audioRecorder = AudioRecorder()
     companion object {
         const val PHOTO_FROM_GALLERY = 1
         const val LAYER_MANAGEMENT = "layer_management"
@@ -441,16 +442,44 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
         startRecord.visibility = View.VISIBLE
     }
 
-    private fun getRecordFilePath(sceneIndex: Int): String {
-        return "${Config.RECORD_FOLDER}/Project${project.name}_Record_Scene$sceneIndex.mp3"
+    override fun onPause() {
+        super.onPause()
+        cancelRecording()
+    }
+
+    private fun cancelRecording() {
+        audioRecorder.cancelRecording()
+
+        tv_scene_activity_record_time.visibility = View.INVISIBLE
+        timer.cancel()
+        timer = Timer()
+        recordingTime = 0L
+        tv_scene_activity_record_time.text = "Recording 00:00"
+        isRecord = false
+        startRecord.setImageDrawable(resources.getDrawable(R.drawable.ic_btn_record))
+        navigationBar.visibility = View.VISIBLE
+        initControlBarView()
+        initControlSceneView()
+        initRecordFinishView()
+    }
+
+    private fun getRecordFilePath(): String {
+        return "${Config.RECORD_FOLDER}/Project${project.name}_Record_Scene${System.currentTimeMillis()}.mp3"
     }
 
     private fun initRecord(){
-        val audioRecorder = AudioRecorder()
-        var recordPath = ""
+        var recordPath: String? = null
         startRecord.setOnClickListener(View.OnClickListener {
             if (!isRecord) {
-                recordPath = getRecordFilePath(currentSceneIndex)
+                //  delete record
+                project.story?.let {
+                    it.scenes?.let{
+                        it[currentSceneIndex].record = null
+                        if (it[currentSceneIndex].recordPath != null)
+                            File(it[currentSceneIndex].recordPath).delete()
+                    }
+                }
+                recordPath = getRecordFilePath()
                 println("recordPath = $recordPath")
                 audioRecorder.prepare(File(recordPath))
                 audioRecorder.startRecording()