Browse Source

Fix: music source path direct to project object

liweihao 6 years ago
parent
commit
e79579ee24

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

@@ -222,8 +222,16 @@ class MovieEditActivity : AppCompatActivity(), DialogInterface.OnDismissListener
     }
 
     private fun addMusic(view: View) {
-        val musicSource = DataParser.parseMusicSource(contentResolver, (Environment.getExternalStorageDirectory().toString()
-                + "/Android/yuheng/public/music"))
+        val libraryPaths = ArrayList<String>()
+
+        project?.let {
+            val assetFolder = it.assetFolder.path
+            it.themeAssetIndex.contains.music.values.forEach {
+                libraryPaths.add("$assetFolder/$it")
+            }
+        }
+
+        val musicSource = DataParser.parseMusicSource(contentResolver, libraryPaths)
         val musicSelectDialog = MusicSelectDialog.newInstance(Gson().toJson(musicSource))
         musicSelectDialog.show(supportFragmentManager, MUSIC_SELECT_DIALOG_TAG)
     }

+ 12 - 11
src/main/java/com/bomostory/sceneeditmodule/basicdata/DataParser.kt

@@ -9,30 +9,31 @@ import java.io.File
 
 object DataParser {
 
-    fun parseMusicSource(contentResolver: ContentResolver, packagePath: String):MusicSource{
+    fun parseMusicSource(contentResolver: ContentResolver,libraryPaths:List<String>):MusicSource{
         val musicSource = MusicSource()
-        musicSource.library = parseMusic(packagePath)
-        musicSource.device = parseMusic(contentResolver, packagePath)
+        musicSource.library = parseMusic(libraryPaths)
+        musicSource.device = parseMusic(contentResolver,libraryPaths)
         return musicSource
     }
 
-    fun parseMusic(packagePath: String): ArrayList<Music> {
+    private fun parseMusic(paths: List<String>): ArrayList<Music> {
         val mediaMetadataRetriever = MediaMetadataRetriever()
-
         val musics = ArrayList<Music>()
-        val files = File(packagePath)
-        for (file in files.listFiles()) {
-            mediaMetadataRetriever.setDataSource(file.path)
+
+        for (path in paths) {
+            val file = File(path)
+            mediaMetadataRetriever.setDataSource(path)
+
             val music = Music()
             music.name = file.name
-            music.path = file.path
+            music.path = path
             music.duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toLong()
             musics.add(music)
         }
         return musics
     }
 
-    fun parseMusic(contentResolver: ContentResolver, packagePath: String): ArrayList<Music> {
+    private fun parseMusic(contentResolver: ContentResolver,paths: List<String>): ArrayList<Music>{
         val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
         val projection = arrayOf(
                 MediaStore.Audio.Media.TITLE,
@@ -53,7 +54,7 @@ object DataParser {
             music.path = cursor.getString(1)
             music.duration = cursor.getString(2).toLong()
             val file = File(music.path)
-            if (file.parent != packagePath) {
+            if (!paths.contains(file.path)) {
                 musics.add(music)
             }
         }