Procházet zdrojové kódy

Merge branch 'feature_audio_track_edit'

# Conflicts:
#	src/main/java/com/bomostory/sceneeditmodule/MovieEditActivity.kt
Shan-PC\Shan před 6 roky
rodič
revize
4e809f0e12

+ 22 - 10
src/main/java/com/bomostory/sceneeditmodule/MovieEditActivity.kt

@@ -10,7 +10,6 @@ import android.os.Environment
 import android.os.Handler
 import android.support.v4.app.DialogFragment
 import android.support.v7.app.AppCompatActivity
-import android.util.Log
 import android.view.View
 import android.widget.CompoundButton
 import android.widget.SeekBar
@@ -21,6 +20,7 @@ import com.bomostory.sceneeditmodule.basicdata.Project
 import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.bomostory.sceneeditmodule.share.ExportPdfDialog
 import com.bomostory.sceneeditmodule.share.ShareDialog
+import com.bomostory.sceneeditmodule.basicdata.*
 import com.bomostory.sceneeditmodule.utils.MoviePlayer
 import com.bomostory.sceneeditmodule.utils.MusicPlayer
 import com.bomostory.sceneeditmodule.view.AudioTrackGroupView
@@ -97,10 +97,17 @@ class MovieEditActivity : AppCompatActivity(),
         set(value) {
             field = value
             value?.let {
-                it.story?.let {
-                    movieEditView.story = it
-                    scene = it.scenes[0]
-                }
+                story = it.story
+            }
+        }
+
+    private var story: Story? = null
+        set(value) {
+            field = value
+            moviePlayer.story = value
+            movieEditView.story = value
+            value?.let {
+                scene = it.scenes[0]
             }
         }
 
@@ -109,8 +116,6 @@ class MovieEditActivity : AppCompatActivity(),
             field = value
             value?.let {
                 movieView.scene = value
-                movieEditView.scene = value
-                moviePlayer.scene = value
             }
         }
 
@@ -192,7 +197,16 @@ class MovieEditActivity : AppCompatActivity(),
         }
 
         moviePlayer.moviePlayListener = object : MoviePlayer.OnMoviePlayListener {
+            override fun onMovieSceneUpdate(scene: Scene) {
+                if (scene != this@MovieEditActivity.scene) {
+                    this@MovieEditActivity.scene = scene
+                }
+            }
+
             override fun onMoviePlayComplete() {
+                story?.scenes?.let {
+                    this@MovieEditActivity.scene = it[0]
+                }
                 movieEditView.isBtnPanelEnable = true
             }
 
@@ -250,9 +264,7 @@ class MovieEditActivity : AppCompatActivity(),
     }
 
     private fun editMusic(view: View) {
-        Log.d("Debug", "edit music track $currentAudioPosition")
-
-        val musicEditDialog = MusicEditDialog.newInstance("", "")
+        val musicEditDialog = MusicEditDialog.newInstance(Gson().toJson(musics[currentAudioPosition]))
         musicEditDialog.show(supportFragmentManager, MUSIC_EDIT_DIALOG_TAG)
     }
 

+ 14 - 22
src/main/java/com/bomostory/sceneeditmodule/MusicEditDialog.kt

@@ -8,13 +8,14 @@ import android.support.v4.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import com.bomostory.sceneeditmodule.basicdata.Music
+import com.bomostory.sceneeditmodule.utils.TimeUtils
 
 import com.example.tfat.myapplication.R
+import com.google.gson.Gson
+import kotlinx.android.synthetic.main.fragment_music_edit_dialog.view.*
 
-// TODO: Rename parameter arguments, choose names that match
-// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
-private const val ARG_PARAM1 = "param1"
-private const val ARG_PARAM2 = "param2"
+private const val ARG_PARAM1 = "editMusic"
 
 /**
  * A simple [Fragment] subclass.
@@ -27,22 +28,23 @@ private const val ARG_PARAM2 = "param2"
  */
 class MusicEditDialog : DialogFragment() {
     // TODO: Rename and change types of parameters
-    private var param1: String? = null
-    private var param2: String? = null
+    private var editMusic: Music? = null
     private var listener: OnFragmentInteractionListener? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         arguments?.let {
-            param1 = it.getString(ARG_PARAM1)
-            param2 = it.getString(ARG_PARAM2)
+            editMusic = Gson().fromJson(it.getString(ARG_PARAM1), Music::class.java)
         }
     }
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                               savedInstanceState: Bundle?): View? {
-        // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_music_edit_dialog, container, false)
+        return inflater.inflate(R.layout.fragment_music_edit_dialog, container, false).apply {
+            editMusic?.let {
+                musicName.text = it.name
+            }
+        }
     }
 
     // TODO: Rename method, update argument and hook method into UI event
@@ -81,21 +83,11 @@ class MusicEditDialog : DialogFragment() {
     }
 
     companion object {
-        /**
-         * Use this factory method to create a new instance of
-         * this fragment using the provided parameters.
-         *
-         * @param param1 Parameter 1.
-         * @param param2 Parameter 2.
-         * @return A new instance of fragment MusicEditDialog.
-         */
-        // TODO: Rename and change types and number of parameters
         @JvmStatic
-        fun newInstance(param1: String, param2: String) =
+        fun newInstance(editMusic: String) =
                 MusicEditDialog().apply {
                     arguments = Bundle().apply {
-                        putString(ARG_PARAM1, param1)
-                        putString(ARG_PARAM2, param2)
+                        putString(ARG_PARAM1, editMusic)
                     }
                 }
     }

+ 60 - 16
src/main/java/com/bomostory/sceneeditmodule/utils/MoviePlayer.kt

@@ -1,6 +1,7 @@
 package com.bomostory.sceneeditmodule.utils
 
 import com.bomostory.sceneeditmodule.basicdata.Scene
+import com.bomostory.sceneeditmodule.basicdata.Story
 import io.reactivex.Observable
 import io.reactivex.Observer
 import io.reactivex.android.schedulers.AndroidSchedulers
@@ -11,6 +12,7 @@ import java.util.concurrent.atomic.AtomicLong
 
 class MoviePlayer {
     interface OnMoviePlayListener {
+        fun onMovieSceneUpdate(scene: Scene)
         fun onMoviePlayViewUpdate(x: Int)
         fun onMoviePlayTimeUpdate(time: Long)
         fun onMoviePlayComplete()
@@ -20,7 +22,20 @@ class MoviePlayer {
         const val INTERVAL_PERIOD: Long = 1
     }
 
-    var scene: Scene? = null
+    var story: Story? = null
+        set(value) {
+            field = value
+            value?.apply {
+                for (scene in scenes) {
+                    scene.record?.period?.let {
+                        totalPeriod += it
+                    }
+                }
+            }
+        }
+
+    var totalPeriod = 0L
+
     var musicPlayer = MusicPlayer()
 
     var moviePlayListener: OnMoviePlayListener? = null
@@ -31,7 +46,7 @@ class MoviePlayer {
         musicPlayer.add(path)
     }
 
-    fun removeMusic(path: String){
+    fun removeMusic(path: String) {
         musicPlayer.remove(path)
     }
 
@@ -74,20 +89,13 @@ class MoviePlayer {
                     }
 
                     override fun onNext(t: Long) {
-                        scene?.let {
-                            it.record?.let {
-                                for (track in it.tracks) {
-                                    if (track.time == t) {
-                                        moviePlayListener?.onMoviePlayViewUpdate(track.positionX)
-                                    }
-                                }
-
-                                moviePlayListener?.onMoviePlayTimeUpdate(t)
-
-                                if (t > (it.period / INTERVAL_PERIOD)) {
-                                    disposable?.dispose()
-                                    onComplete()
-                                }
+                        story?.apply {
+
+                            updateMovie(t)
+
+                            if (t > (totalPeriod / INTERVAL_PERIOD)) {
+                                disposable?.dispose()
+                                onComplete()
                             }
                         }
                     }
@@ -102,7 +110,43 @@ class MoviePlayer {
     fun seekTo(mSec: Int?) {
         mSec?.let {
             lastTick.set(it.toLong())
+            updateMovie(mSec.toLong())
             musicPlayer.seekTo(it)
         }
     }
+
+    private fun updateMovie(t: Long) {
+        val scene = getScene(t)
+        scene?.apply {
+            moviePlayListener?.onMovieSceneUpdate(this)
+            record?.let {
+                for (track in it.tracks) {
+                    if (track.time == t) {
+                        moviePlayListener?.onMoviePlayViewUpdate(track.positionX)
+                    }
+                }
+            }
+        }
+        moviePlayListener?.onMoviePlayTimeUpdate(t)
+    }
+
+    private fun getScene(t: Long): Scene? {
+        var currentScene: Scene? = null
+        var startPeriod = 0L
+        var endPeriod = 0L
+
+        story?.apply {
+            for (scene in scenes) {
+                scene.record?.apply {
+                    endPeriod += period
+                    if (t in startPeriod..endPeriod) {
+                        currentScene = scene
+                        startPeriod = endPeriod
+                    }
+                }
+            }
+        }
+
+        return currentScene
+    }
 }

+ 10 - 7
src/main/java/com/bomostory/sceneeditmodule/utils/MusicPlayer.kt

@@ -10,11 +10,11 @@ class MusicPlayer {
         musicPaths.add(path)
     }
 
-    fun remove(path: String){
+    fun remove(path: String) {
         musicPaths.remove(path)
     }
 
-    fun clear(){
+    fun clear() {
         musicPaths.clear()
     }
 
@@ -38,12 +38,15 @@ class MusicPlayer {
                 setOnPreparedListener {
                     it.start()
                 }
+                setOnCompletionListener {
+                    mediaPlayers.remove(it)
+                }
             }
             mediaPlayers.add(mediaPlayer)
         }
     }
 
-    fun start(){
+    fun start() {
         for (mediaPlayer in mediaPlayers) {
             mediaPlayer.apply {
                 start()
@@ -51,14 +54,14 @@ class MusicPlayer {
         }
     }
 
-    fun pause(){
-        for(mediaPlayer in mediaPlayers){
+    fun pause() {
+        for (mediaPlayer in mediaPlayers) {
             mediaPlayer.pause()
         }
     }
 
-    fun seekTo(mSec:Int){
-        for(mediaPlayer in mediaPlayers){
+    fun seekTo(mSec: Int) {
+        for (mediaPlayer in mediaPlayers) {
             mediaPlayer.seekTo(mSec)
         }
     }

+ 12 - 5
src/main/java/com/bomostory/sceneeditmodule/view/MovieEditView.kt

@@ -18,14 +18,21 @@ class MovieEditView : ConstraintLayout {
     var story: Story? = null
         set(value) {
             movieSelectView.story = value
+
+            value?.apply {
+                for (scene in scenes) {
+                    scene.record?.let {
+                        period += it.period
+                    }
+                }
+            }
         }
 
-    var scene: Scene? = null
+    var period: Long = 0
         set(value) {
-            movieSeekView.scene = value
-            value?.record?.period?.let {
-                audioTrackGroupView.scenePeriod = it
-            }
+            field = value
+            audioTrackGroupView.scenePeriod = value
+            movieSeekView.period = value
         }
 
     var playTime: Long = 0

+ 5 - 11
src/main/java/com/bomostory/sceneeditmodule/view/MovieSeekView.kt

@@ -6,24 +6,18 @@ import android.util.AttributeSet
 import android.view.LayoutInflater
 import android.view.View
 import android.widget.SeekBar
-import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.bomostory.sceneeditmodule.utils.TimeUtils
 import com.example.tfat.myapplication.R
 import kotlinx.android.synthetic.main.movie_seek_view.view.*
 
 class MovieSeekView : ConstraintLayout {
 
-    var scene: Scene? = null
+    var period: Long = 0
         set(value) {
-            field = value
-            value?.let {
-                it.record?.let {
-                    progressTime.text = TimeUtils.getPlayMovieTimeFormat(playTime)
-                    totalTime.text = TimeUtils.getPlayMovieTimeFormat(it.period)
-                    seekBar.progress = playTime.toInt()
-                    seekBar.max = it.period.toInt()
-                }
-            }
+            progressTime.text = TimeUtils.getPlayMovieTimeFormat(playTime)
+            totalTime.text = TimeUtils.getPlayMovieTimeFormat(value)
+            seekBar.progress = playTime.toInt()
+            seekBar.max = value.toInt()
         }
 
     var playTime: Long = 0