Procházet zdrojové kódy

New: ffmpeg audio editor to music edit dialog

liweihao před 6 roky
rodič
revize
6f63873429

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

@@ -19,9 +19,11 @@ import com.bomostory.sceneeditmodule.share.ExportPdfDialog
 import com.bomostory.sceneeditmodule.share.ShareDialog
 import com.bomostory.sceneeditmodule.utils.MoviePlayer
 import com.bomostory.sceneeditmodule.utils.MusicPlayer
+import com.bomostory.sceneeditmodule.utils.TimeUtils
 import com.bomostory.sceneeditmodule.view.AudioTrackGroupView
 import com.bomostory.sceneeditmodule.view.MovieSelectView
 import com.example.exportmedia.MediaHelper
+import com.example.exportmedia.audio.AudioEditor
 import com.example.tfat.myapplication.R
 import com.google.gson.Gson
 import io.reactivex.Observable
@@ -80,6 +82,26 @@ class MovieEditActivity : AppCompatActivity(),
         musicPlayer.stop()
     }
 
+    override fun onMusicEditFinish(music: Music) {
+        if (music.endTime - music.startTime > moviePlayer.totalPeriod) {
+            music.endTime = music.startTime + moviePlayer.totalPeriod
+        }
+
+        val builder = AudioEditor.Builder(mediaHelper)
+        builder.editAudioFile = File(music.path)
+        builder.startTime = TimeUtils.getEncodeTimeFormt(music.startTime)
+        builder.endTime = TimeUtils.getEncodeTimeFormt(music.endTime)
+        builder.volume = music.volume
+
+        val fileName = "test${System.currentTimeMillis()}.mp3"
+        val outputFile = File(Environment.getExternalStorageDirectory(), fileName)
+        val audioEditor = builder.build()
+        audioEditor.output(outputFile).subscribe({
+        }, {
+        })
+
+    }
+
     override fun onDismiss(dialog: DialogInterface?) {
         musicPlayer.stop()
         musicPlayer.release()

+ 12 - 4
src/main/java/com/bomostory/sceneeditmodule/MusicEditDialog.kt

@@ -116,10 +116,8 @@ class MusicEditDialog : DialogFragment() {
                     override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                         volumeText.text = "$p1 %"
 
-                        editMusic?.apply {
-                            volume = p1.toFloat() / volumeSeekBar.max.toFloat()
-                            listener?.onMusicVolumeChanged(volume)
-                        }
+                        val volume = p1.toFloat() / volumeSeekBar.max.toFloat()
+                        listener?.onMusicVolumeChanged(volume)
                     }
 
                     override fun onStartTrackingTouch(p0: SeekBar?) {
@@ -146,6 +144,15 @@ class MusicEditDialog : DialogFragment() {
                     dismiss()
                 }
 
+                save.setOnClickListener { _ ->
+                    editMusic?.apply {
+                        startTime = (headScrollRatio * duration).toLong()
+                        endTime = (endScrollRatio * duration).toLong()
+                        volume = volumeSeekBar.progress.toFloat() / volumeSeekBar.max.toFloat()
+
+                        listener?.onMusicEditFinish(this)
+                    }
+                }
             }
         }
     }
@@ -217,6 +224,7 @@ class MusicEditDialog : DialogFragment() {
         fun onMusicPauseClick()
         fun onMusicVolumeChanged(volume: Float)
         fun onMusicStop()
+        fun onMusicEditFinish(music: Music)
     }
 
     companion object {

+ 2 - 0
src/main/java/com/bomostory/sceneeditmodule/basicdata/Music.kt

@@ -2,9 +2,11 @@ package com.bomostory.sceneeditmodule.basicdata
 
 class Music {
     var name: String? = null
+    var originPath:String? = null
     var path: String? = null
     var offsetTime: Long = 0
     var startTime: Long = 0
+    var endTime:Long = 0
     var duration: Long = 0
     var volume: Float = 0.5f
 }

+ 8 - 1
src/main/java/com/bomostory/sceneeditmodule/utils/TimeUtils.kt

@@ -2,6 +2,7 @@ package com.bomostory.sceneeditmodule.utils
 
 import java.util.concurrent.TimeUnit
 
+
 object TimeUtils {
 
     fun getPlayMovieTimeFormat(millis: Long): String {
@@ -10,9 +11,15 @@ object TimeUtils {
                 TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1))
     }
 
-    fun getAudioTimeFormat(millis: Long):String{
+    fun getAudioTimeFormat(millis: Long): String {
         return String.format("%01d:%02d",
                 TimeUnit.MILLISECONDS.toMinutes(millis),
                 TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1))
     }
+
+    fun getEncodeTimeFormt(millis: Long): String {
+        return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
+                TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
+                TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1))
+    }
 }