Переглянути джерело

New : add record file play to movie player

liweihao 6 роки тому
батько
коміт
79d60fb2db

+ 77 - 6
src/main/java/com/bomostory/sceneeditmodule/utils/MoviePlayerV2.kt

@@ -6,6 +6,7 @@ import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.bomostory.sceneeditmodule.basicdata.Story
 import java.util.*
 import kotlin.collections.ArrayList
+import kotlin.collections.HashMap
 
 class MoviePlayerV2 {
     interface OnMoviePlayListener {
@@ -29,7 +30,10 @@ class MoviePlayerV2 {
 
     var musicList: List<Music> = ArrayList()
 
-    var mediaPlayers = ArrayList<MediaPlayer>()
+    private var mediaPlayers = ArrayList<MediaPlayer>()
+
+    private val recordPlayers = HashMap<Int, MediaPlayer>()
+
 
     fun init() {
         isPause = false
@@ -42,8 +46,12 @@ class MoviePlayerV2 {
 
         updateMovie()
 
+        tryStopRecord()
+
         tryStopMusic()
 
+        initRecord()
+
         initMediaPlayer()
 
     }
@@ -59,6 +67,8 @@ class MoviePlayerV2 {
 
                         updateMovie()
 
+                        tryPlayRecord()
+
                         tryPlayMusic()
 
                         if (currentTime >= period) {
@@ -76,6 +86,8 @@ class MoviePlayerV2 {
     fun pause() {
         isPause = true
 
+        tryPauseRecord()
+
         tryPauseMusic()
     }
 
@@ -84,6 +96,8 @@ class MoviePlayerV2 {
 
         updateMovie()
 
+        trySeekRecord()
+
         trySeekMusic()
     }
 
@@ -121,8 +135,26 @@ class MoviePlayerV2 {
         return currentScene
     }
 
+    private fun initRecord() {
+        recordPlayers.clear()
+        story?.let { story ->
+            for (scene in story.scenes) {
+                scene.recordPath?.let {
+                    val mediaPlayer = MediaPlayer()
+                    mediaPlayer.apply {
+                        reset()
+                        setDataSource(scene.recordPath)
+                        prepare()
+                    }
+                    recordPlayers.put(story.scenes.indexOf(scene), mediaPlayer)
+                }
+            }
+        }
+    }
+
     private fun initMediaPlayer() {
         mediaPlayers.clear()
+
         for (music in musicList) {
             val mediaPlayer = MediaPlayer()
             mediaPlayer.apply {
@@ -135,9 +167,19 @@ class MoviePlayerV2 {
         }
     }
 
+    private fun tryPlayRecord() {
+        story?.let {
+            recordPlayers[it.scenes.indexOf(getScene(currentTime))]?.apply {
+                if (!isPlaying) {
+                    start()
+                }
+            }
+        }
+    }
+
     private fun tryPlayMusic() {
         for (music in musicList) {
-            val musicEndTime  = if(music.isLoop) (music.offsetTime + music.endTime - music.startTime) + music.loopDuration else (music.offsetTime + music.endTime - music.startTime)
+            val musicEndTime = if (music.isLoop) (music.offsetTime + music.endTime - music.startTime) + music.loopDuration else (music.offsetTime + music.endTime - music.startTime)
 
             if (currentTime in music.offsetTime..musicEndTime) {
                 if (!mediaPlayers[musicList.indexOf(music)].isPlaying) {
@@ -150,18 +192,42 @@ class MoviePlayerV2 {
         }
     }
 
+    private fun tryPauseRecord() {
+        recordPlayers.forEach {
+            it.value.pause()
+        }
+    }
+
     private fun tryPauseMusic() {
         for (mediaPlayer in mediaPlayers) {
             mediaPlayer.pause()
         }
     }
 
+    private fun trySeekRecord() {
+        story?.let {
+            var period: Long = 0
+            for (i in 0 until it.scenes.indexOf(getScene(currentTime))) {
+                period += (it.scenes[i].record?.period ?: 0)
+            }
+
+            recordPlayers[it.scenes.indexOf(getScene(currentTime))]?.apply {
+                if (period == 0L) {
+                    this.seekTo(currentTime.toInt())
+                } else {
+                    this.seekTo((currentTime % period).toInt())
+                }
+            }
+
+        }
+    }
+
     private fun trySeekMusic() {
         for (music in musicList) {
-            val musicEndTime  = if(music.isLoop) (music.offsetTime + music.endTime - music.startTime) + music.loopDuration else (music.offsetTime + music.endTime - music.startTime)
+            val musicEndTime = if (music.isLoop) (music.offsetTime + music.endTime - music.startTime) + music.loopDuration else (music.offsetTime + music.endTime - music.startTime)
 
             if (currentTime in music.offsetTime..musicEndTime) {
-                val actualTime = if (currentTime - music.offsetTime >= music.endTime - music.startTime)((currentTime - music.offsetTime).toDouble() / (music.endTime - music.startTime)).toInt()
+                val actualTime = if (currentTime - music.offsetTime >= music.endTime - music.startTime) ((currentTime - music.offsetTime).toDouble() / (music.endTime - music.startTime)).toInt()
                 else (currentTime - music.offsetTime).toInt()
                 mediaPlayers[musicList.indexOf(music)].seekTo(actualTime)
             } else {
@@ -171,10 +237,15 @@ class MoviePlayerV2 {
         }
     }
 
-    private fun tryStopMusic(){
+    private fun tryStopRecord() {
+        recordPlayers.forEach {
+            it.value.stop()
+        }
+    }
+
+    private fun tryStopMusic() {
         for (mediaPlayer in mediaPlayers) {
             mediaPlayer.stop()
         }
     }
-
 }