|
@@ -1,18 +1,21 @@
|
|
|
package com.bomostory.sceneeditmodule
|
|
|
|
|
|
import android.content.Context
|
|
|
-import android.net.Uri
|
|
|
+import android.content.DialogInterface
|
|
|
import android.os.Bundle
|
|
|
import android.support.v4.app.DialogFragment
|
|
|
import android.support.v4.app.Fragment
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
+import android.widget.SeekBar
|
|
|
import com.bomostory.sceneeditmodule.basicdata.Music
|
|
|
import com.bomostory.sceneeditmodule.utils.TimeUtils
|
|
|
+import com.example.audiotrack.EditAudioTrackView
|
|
|
|
|
|
import com.example.tfat.myapplication.R
|
|
|
import com.google.gson.Gson
|
|
|
+import kotlinx.android.synthetic.main.fragment_music_edit_dialog.*
|
|
|
import kotlinx.android.synthetic.main.fragment_music_edit_dialog.view.*
|
|
|
|
|
|
private const val ARG_PARAM1 = "editMusic"
|
|
@@ -27,9 +30,43 @@ private const val ARG_PARAM1 = "editMusic"
|
|
|
*
|
|
|
*/
|
|
|
class MusicEditDialog : DialogFragment() {
|
|
|
- // TODO: Rename and change types of parameters
|
|
|
private var editMusic: Music? = null
|
|
|
private var listener: OnFragmentInteractionListener? = null
|
|
|
+ private var dismissListener: DialogInterface.OnDismissListener? = null
|
|
|
+
|
|
|
+ var currentPosition: Int = 0
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+
|
|
|
+ editMusic?.let {
|
|
|
+ val endPosition = endScrollRatio * it.duration
|
|
|
+
|
|
|
+ var playPosition = currentPosition
|
|
|
+ if (currentPosition >= endPosition) {
|
|
|
+ playPosition = 0
|
|
|
+ listener?.onMusicStop()
|
|
|
+ editAudioTrackView.playProgressDialogVisible = false
|
|
|
+ }
|
|
|
+ editAudioTrackView.playProgress = playPosition.toFloat() / it.duration
|
|
|
+ editAudioTrackView.playProgressText = TimeUtils.getAudioTimeFormat(playPosition.toLong())
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var headScrollRatio: Double = 0.0
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+
|
|
|
+ updateMusicLength()
|
|
|
+ updateEditAudioTrackHeadText()
|
|
|
+ }
|
|
|
+
|
|
|
+ var endScrollRatio: Double = 1.0
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+
|
|
|
+ updateMusicLength()
|
|
|
+ updateEditAudioTrackEndText()
|
|
|
+ }
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
@@ -43,13 +80,97 @@ class MusicEditDialog : DialogFragment() {
|
|
|
return inflater.inflate(R.layout.fragment_music_edit_dialog, container, false).apply {
|
|
|
editMusic?.let {
|
|
|
musicName.text = it.name
|
|
|
+ musicLength.text = getString(R.string.music_length, TimeUtils.getPlayMovieTimeFormat(it.duration))
|
|
|
+ startTime.text = TimeUtils.getPlayMovieTimeFormat(0)
|
|
|
+ endTime.text = TimeUtils.getPlayMovieTimeFormat(it.duration)
|
|
|
+ volumeSeekBar.progress = (it.volume * volumeSeekBar.max).toInt()
|
|
|
+ volumeText.text = "${volumeSeekBar.progress} %"
|
|
|
+
|
|
|
+ play.setOnClickListener { _ ->
|
|
|
+ var playPosition = currentPosition
|
|
|
+ val startPosition = it.duration * headScrollRatio
|
|
|
+ if (startPosition > currentPosition) {
|
|
|
+ playPosition = startPosition.toInt()
|
|
|
+ }
|
|
|
+ listener?.onMusicPlayClick2(it, playPosition)
|
|
|
+
|
|
|
+ editAudioTrackView.playProgressDialogVisible = true
|
|
|
+ play.visibility = View.INVISIBLE
|
|
|
+ pause.visibility = View.VISIBLE
|
|
|
+ }
|
|
|
+
|
|
|
+ pause.setOnClickListener { _ ->
|
|
|
+ editAudioTrackView.playProgressDialogVisible = false
|
|
|
+
|
|
|
+ listener?.onMusicPauseClick()
|
|
|
+
|
|
|
+ play.visibility = View.VISIBLE
|
|
|
+ pause.visibility = View.INVISIBLE
|
|
|
+ }
|
|
|
+
|
|
|
+ volumeSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
|
|
+ override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
|
|
|
+ volumeText.text = "$p1 %"
|
|
|
+
|
|
|
+ editMusic?.apply {
|
|
|
+ volume = p1.toFloat() / volumeSeekBar.max.toFloat()
|
|
|
+ listener?.onMusicVolumeChanged(volume)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStartTrackingTouch(p0: SeekBar?) {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStopTrackingTouch(p0: SeekBar?) {
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ editAudioTrackView.onHeadScrollListener = object : EditAudioTrackView.OnEditScrollListener {
|
|
|
+ override fun onScroll(view: EditAudioTrackView, scrollRatio: Double) {
|
|
|
+ headScrollRatio = scrollRatio
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ editAudioTrackView.onEndScrollListener = object : EditAudioTrackView.OnEditScrollListener {
|
|
|
+ override fun onScroll(view: EditAudioTrackView, scrollRatio: Double) {
|
|
|
+ endScrollRatio = scrollRatio
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ cancel.setOnClickListener { _ ->
|
|
|
+ dismiss()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun updateEditAudioTrackHeadText() {
|
|
|
+ editAudioTrackView?.let {
|
|
|
+ editMusic?.apply {
|
|
|
+ val newDuration = duration * headScrollRatio
|
|
|
+ it.headScrollProgressText = TimeUtils.getAudioTimeFormat(newDuration.toLong())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // TODO: Rename method, update argument and hook method into UI event
|
|
|
- fun onButtonPressed(uri: Uri) {
|
|
|
- listener?.onFragmentInteraction(uri)
|
|
|
+ private fun updateEditAudioTrackEndText() {
|
|
|
+ editAudioTrackView?.let {
|
|
|
+ editMusic?.apply {
|
|
|
+ val newDuration = duration * endScrollRatio
|
|
|
+ it.endScrollProgressText = TimeUtils.getAudioTimeFormat(newDuration.toLong())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun updateMusicLength() {
|
|
|
+ musicLength?.let {
|
|
|
+ editMusic?.apply {
|
|
|
+ val newDuration = duration * (-headScrollRatio + endScrollRatio)
|
|
|
+ musicLength.text = getString(R.string.music_length, TimeUtils.getPlayMovieTimeFormat(newDuration.toLong()))
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onAttach(context: Context) {
|
|
@@ -59,11 +180,21 @@ class MusicEditDialog : DialogFragment() {
|
|
|
} else {
|
|
|
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
|
|
|
}
|
|
|
+
|
|
|
+ if (context is DialogInterface.OnDismissListener) {
|
|
|
+ dismissListener = context
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onDetach() {
|
|
|
super.onDetach()
|
|
|
listener = null
|
|
|
+ dismissListener = null
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDismiss(dialog: DialogInterface?) {
|
|
|
+ super.onDismiss(dialog)
|
|
|
+ dismissListener?.onDismiss(dialog)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -78,8 +209,11 @@ class MusicEditDialog : DialogFragment() {
|
|
|
* for more information.
|
|
|
*/
|
|
|
interface OnFragmentInteractionListener {
|
|
|
- // TODO: Update argument type and name
|
|
|
- fun onFragmentInteraction(uri: Uri)
|
|
|
+ fun onMusicPlayClick2(music: Music, startPosition: Int)
|
|
|
+ fun onMusicPauseClick()
|
|
|
+ fun onMusicVolumeChanged(volume: Float)
|
|
|
+ fun onMusicSeekTo(mSec: Int)
|
|
|
+ fun onMusicStop()
|
|
|
}
|
|
|
|
|
|
companion object {
|