Jelajahi Sumber

New: Integrate audio looper to movie edit

liweihao 6 tahun lalu
induk
melakukan
24e90ee1f7

+ 20 - 4
src/main/java/com/bomostory/sceneeditmodule/SuperMovieMaker.kt

@@ -1,12 +1,15 @@
 package com.bomostory.sceneeditmodule
 
 import android.graphics.Bitmap
+import android.media.MediaMetadataRetriever
+import android.os.Environment
 import android.util.Log
 import com.bomostory.sceneeditmodule.screen.movie.MovieEditActivity.Companion.FPS
 import com.bomostory.sceneeditmodule.basicdata.Music
 import com.bomostory.sceneeditmodule.basicdata.Project
 import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.example.exportmedia.MediaHelper
+import com.example.exportmedia.audio.AudioLooper
 import com.example.exportmedia.data.AudioSource
 import com.example.exportmedia.vedio.MovieMaker
 import io.reactivex.Completable
@@ -31,7 +34,7 @@ class SuperMovieMaker {
         return Observable.create<String> { emitter ->
             val movieBuilder = MovieMaker.Builder(mediaHelper)
             movieBuilder.fps = FPS
-            movieBuilder.audioSources = generateAudioSource(musics)
+            movieBuilder.audioSources = generateAudioSource(musics,mediaHelper)
 
             val inputSceneStatus = LinkedHashMap<Int, Int>()
             val frameDataList = generateFrameDataList(project)
@@ -71,13 +74,26 @@ class SuperMovieMaker {
         }
     }
 
-    private fun generateAudioSource(musics: List<Music>): ArrayList<AudioSource> {
+    private fun generateAudioSource(musics: List<Music>,mediaHelper: MediaHelper): ArrayList<AudioSource> {
         val audioSources = ArrayList<AudioSource>()
         for (music in musics) {
+            val fileName = "test${System.currentTimeMillis()}.mp3"
+            val outputFile = File(Environment.getExternalStorageDirectory(), fileName)
+
+            val audioLooperBuilder = AudioLooper.Builder(mediaHelper)
+            audioLooperBuilder.loopAudioFile = File(music.path)
+            audioLooperBuilder.loopDuration = music.loopDuration.toInt()
+
+            val audioLooper = audioLooperBuilder.build()
+             audioLooper.output(outputFile).blockingFirst()
+
+            val mediaMetadataRetriever = MediaMetadataRetriever()
+            mediaMetadataRetriever.setDataSource(outputFile.path)
+
             val audioSource = AudioSource()
-            audioSource.duration = music.duration
+            audioSource.path = outputFile.path
+            audioSource.duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toLong()
             audioSource.offsetSec = music.offsetTime.toFloat() / 1000
-            audioSource.path = music.path
             audioSources.add(audioSource)
         }
         return audioSources

+ 1 - 1
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditActivity.kt

@@ -390,7 +390,7 @@ class MovieEditActivity : AppCompatActivity(),
         SuperMovieMaker().make(
                 outputFile,
                 project,
-                viewModel.musicListLiveData.value ?: ArrayList<Music>(),
+                viewModel.musicListLiveData.value?:ArrayList(),
                 scaledWidth,
                 scaleHeight,
                 mediaHelper)