瀏覽代碼

修复部分midi播放问题

Pq 3 年之前
父節點
當前提交
f66ae35eb2

+ 40 - 0
midiplaylib/src/main/java/com/jinmingyunle/midiplaylib/MidiPlayerUtils.java

@@ -39,6 +39,7 @@ public class MidiPlayerUtils {
     private static final String TAG = "MidiPlayerUtilsTAG";
     private Context mContext;
     private static volatile MidiPlayerUtils instance = null;
+    public static final int[] PIANO_IDS = new int[]{0, 1, 2, 3, 4, 5, 6, 7};
 
     public String tempSoundFile;
 
@@ -328,6 +329,45 @@ public class MidiPlayerUtils {
         return false;
     }
 
+    public boolean setTrackVolumeById(int id, float volume) {
+        if (tracks == null) {
+            return false;
+        }
+        boolean isPianoId = isPianoId(id);
+        if (isPianoId) {
+            for (int i = 0; i < PIANO_IDS.length; i++) {
+                int pianoId = PIANO_IDS[i];
+                updateTrackVolumeById(pianoId, volume);
+            }
+            return true;
+        } else {
+            return updateTrackVolumeById(id, volume);
+        }
+    }
+
+    public boolean updateTrackVolumeById(int targetId, float volume) {
+        for (int i = 0; i < tracks.size(); i++) {
+            MidiTrack midiTrack = tracks.get(i);
+            if (midiTrack != null) {
+                int id = midiTrack.getInstrument();
+                if (id == targetId) {
+                    return setTrackVolume(i, volume);
+                }
+            }
+        }
+        return false;
+    }
+
+    public boolean isPianoId(int id) {
+        for (int i = 0; i < PIANO_IDS.length; i++) {
+            int pianoId = PIANO_IDS[i];
+            if (id == pianoId) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public void reset() {
         stopSound();
     }

+ 10 - 6
student/src/main/java/com/cooleshow/student/ui/web/AccompanyFragment.java

@@ -1559,7 +1559,7 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
                                     if (Math.abs(midiFileDuration - tempDuration * 1000) > 2000) {
                                         midiFileDuration = (long) (tempDuration * 1000);
                                     }
-                                    JSONObject contentJsonObject =message.optJSONObject("content");
+                                    JSONObject contentJsonObject = message.optJSONObject("content");
                                     if (contentJsonObject != null) {
                                         contentJsonObject.put("midiDuration", midiFileDuration);
                                         contentJsonObject.put("midi", EncodeUtils.urlEncode(midiFile));
@@ -1587,7 +1587,7 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
                                                                 midiFileDuration = (long) (tempDuration * 1000);
                                                             }
                                                             try {
-                                                                JSONObject contentJsonObject =message.optJSONObject("content");
+                                                                JSONObject contentJsonObject = message.optJSONObject("content");
                                                                 if (contentJsonObject != null) {
                                                                     contentJsonObject.put("midiDuration", midiFileDuration);
                                                                     contentJsonObject.put("midi", EncodeUtils.urlEncode(midiFile));
@@ -1774,13 +1774,17 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
 
     @Override
     public void cloudVolume(JSONObject message) {
-
         try {
             JSONObject obj = message.optJSONObject("content");
-            JSONArray array = obj.optJSONArray("parts");
-            for (int i = 0; i < array.length(); i++) {
-                MidiPlayerUtils.getInstance().setTrackVolumeByName(array.optJSONObject(i).optString("name"), (float) array.optJSONObject(i).optInt("volume", 70) / 100f);
+            if (obj != null) {
+                int activeMidiId = obj.optInt("activeMidiId");
+                int activeMidiVolume = obj.optInt("activeMidiVolume", 70);
+                if (activeMidiId == -1) {
+                    return;
+                }
+                MidiPlayerUtils.getInstance().setTrackVolumeById(activeMidiId, (float) activeMidiVolume / 100f);
             }
+            sendMessage(message.toString());
         } catch (Exception e) {
             e.printStackTrace();
             sendMessage(message.toString());

+ 8 - 3
teacher/src/main/java/com/cooleshow/teacher/ui/web/AccompanyFragment.java

@@ -1779,10 +1779,15 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
 
         try {
             JSONObject obj = message.optJSONObject("content");
-            JSONArray array = obj.optJSONArray("parts");
-            for (int i = 0; i < array.length(); i++) {
-                MidiPlayerUtils.getInstance().setTrackVolumeByName(array.optJSONObject(i).optString("name"), (float) array.optJSONObject(i).optInt("volume", 70) / 100f);
+            if (obj != null) {
+                int activeMidiId = obj.optInt("activeMidiId");
+                int activeMidiVolume = obj.optInt("activeMidiVolume", 70);
+                if (activeMidiId == -1) {
+                    return;
+                }
+                MidiPlayerUtils.getInstance().setTrackVolumeById(activeMidiId, (float) activeMidiVolume / 100f);
             }
+            sendMessage(message.toString());
         } catch (Exception e) {
             e.printStackTrace();
             sendMessage(message.toString());