| 
				
			 | 
			
			
				@@ -1,4 +1,4 @@ 
			 | 
		
	
		
			
			| 
				1
			 | 
			
				
			 | 
			
			
				-package com.fgodt.mixplayer; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				1
			 | 
			
			
				+package com.shuangyubang.mofun_flutter_plugin_video.mixplayer; 
			 | 
		
	
		
			
			| 
				2
			 | 
			
				2
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				3
			 | 
			
				3
			 | 
			
			
				 import android.content.Context; 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				4
			 | 
			
			
				 import android.media.AudioFormat; 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -28,7 +28,7 @@ public class Render extends VideoRender { 
			 | 
		
	
		
			
			| 
				28
			 | 
			
				28
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				29
			 | 
			
				29
			 | 
			
			
				     public int mixUpdateAudio(byte[] data, int size, double pts) { 
			 | 
		
	
		
			
			| 
				30
			 | 
			
				30
			 | 
			
			
				         playTime = pts; 
			 | 
		
	
		
			
			| 
				31
			 | 
			
				
			 | 
			
			
				-        synchronized (audioBuf) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				31
			 | 
			
			
				+        synchronized (audioBufLock) { 
			 | 
		
	
		
			
			| 
				32
			 | 
			
				32
			 | 
			
			
				             if (audioBufLen - audioHas < size) { 
			 | 
		
	
		
			
			| 
				33
			 | 
			
				33
			 | 
			
			
				                 return -1; 
			 | 
		
	
		
			
			| 
				34
			 | 
			
				34
			 | 
			
			
				             } 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -93,6 +93,7 @@ public class Render extends VideoRender { 
			 | 
		
	
		
			
			| 
				93
			 | 
			
				93
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				94
			 | 
			
				94
			 | 
			
			
				     AudioTrack audioTrack; 
			 | 
		
	
		
			
			| 
				95
			 | 
			
				95
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				
			 | 
			
				96
			 | 
			
			
				+    final Object audioBufLock = new Object(); 
			 | 
		
	
		
			
			| 
				96
			 | 
			
				97
			 | 
			
			
				     byte[] audioBuf; 
			 | 
		
	
		
			
			| 
				97
			 | 
			
				98
			 | 
			
			
				     long totalAudioBuf; 
			 | 
		
	
		
			
			| 
				98
			 | 
			
				99
			 | 
			
			
				     int audioHas; 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -105,75 +106,74 @@ public class Render extends VideoRender { 
			 | 
		
	
		
			
			| 
				105
			 | 
			
				106
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				106
			 | 
			
				107
			 | 
			
			
				     private void initAudioTrack() { 
			 | 
		
	
		
			
			| 
				107
			 | 
			
				108
			 | 
			
			
				         silencBuf = new byte[4096]; 
			 | 
		
	
		
			
			| 
				108
			 | 
			
				
			 | 
			
			
				-        audioBuf = new byte[audioBufLen]; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				109
			 | 
			
			
				+        synchronized (audioBufLock) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				110
			 | 
			
			
				+            audioBuf = new byte[audioBufLen]; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				111
			 | 
			
			
				+        } 
			 | 
		
	
		
			
			| 
				109
			 | 
			
				112
			 | 
			
			
				         audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, asample, 
			 | 
		
	
		
			
			| 
				110
			 | 
			
				113
			 | 
			
			
				                 AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, audioBufLen, AudioTrack.MODE_STREAM); 
			 | 
		
	
		
			
			| 
				111
			 | 
			
				114
			 | 
			
			
				         play = true; 
			 | 
		
	
		
			
			| 
				112
			 | 
			
				
			 | 
			
			
				-        new Thread(new Runnable() { 
			 | 
		
	
		
			
			| 
				113
			 | 
			
				
			 | 
			
			
				-            @Override 
			 | 
		
	
		
			
			| 
				114
			 | 
			
				
			 | 
			
			
				-            public void run() { 
			 | 
		
	
		
			
			| 
				115
			 | 
			
				
			 | 
			
			
				-                int wsize = 1024; 
			 | 
		
	
		
			
			| 
				116
			 | 
			
				
			 | 
			
			
				-                long posPre = 0; 
			 | 
		
	
		
			
			| 
				117
			 | 
			
				
			 | 
			
			
				-                boolean needUpdate = false; 
			 | 
		
	
		
			
			| 
				118
			 | 
			
				
			 | 
			
			
				-                int retryCount = 0; 
			 | 
		
	
		
			
			| 
				119
			 | 
			
				
			 | 
			
			
				-                while (play) { 
			 | 
		
	
		
			
			| 
				120
			 | 
			
				
			 | 
			
			
				-                    synchronized (audioBuf) { 
			 | 
		
	
		
			
			| 
				121
			 | 
			
				
			 | 
			
			
				-                        long pos = audioTrack.getPlaybackHeadPosition(); 
			 | 
		
	
		
			
			| 
				122
			 | 
			
				
			 | 
			
			
				-                        // 16bit 2channels = 4 Byte 
			 | 
		
	
		
			
			| 
				123
			 | 
			
				
			 | 
			
			
				-                        pos *= 4; 
			 | 
		
	
		
			
			| 
				124
			 | 
			
				
			 | 
			
			
				-                        pos -= silenceLen; 
			 | 
		
	
		
			
			| 
				125
			 | 
			
				
			 | 
			
			
				-                        if (posPre != pos) { 
			 | 
		
	
		
			
			| 
				126
			 | 
			
				
			 | 
			
			
				-                            posPre = pos; 
			 | 
		
	
		
			
			| 
				127
			 | 
			
				
			 | 
			
			
				-                            retryCount = 0; 
			 | 
		
	
		
			
			| 
				128
			 | 
			
				
			 | 
			
			
				-                        } else { 
			 | 
		
	
		
			
			| 
				129
			 | 
			
				
			 | 
			
			
				-                            if (retryCount > 5) 
			 | 
		
	
		
			
			| 
				130
			 | 
			
				
			 | 
			
			
				-                                needUpdate = true; 
			 | 
		
	
		
			
			| 
				131
			 | 
			
				
			 | 
			
			
				-                            retryCount++; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				115
			 | 
			
			
				+        new Thread(() -> { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				116
			 | 
			
			
				+            int wsize = 1024; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				117
			 | 
			
			
				+            long posPre = 0; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				118
			 | 
			
			
				+            boolean needUpdate = false; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				119
			 | 
			
			
				+            int retryCount = 0; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				120
			 | 
			
			
				+            while (play) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				121
			 | 
			
			
				+                synchronized (audioBufLock) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				122
			 | 
			
			
				+                    long pos = audioTrack.getPlaybackHeadPosition(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				123
			 | 
			
			
				+                    // 16bit 2channels = 4 Byte 
			 | 
		
	
		
			
			| 
				
			 | 
			
				124
			 | 
			
			
				+                    pos *= 4; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				125
			 | 
			
			
				+                    pos -= silenceLen; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				126
			 | 
			
			
				+                    if (posPre != pos) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				127
			 | 
			
			
				+                        posPre = pos; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				128
			 | 
			
			
				+                        retryCount = 0; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				129
			 | 
			
			
				+                    } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				130
			 | 
			
			
				+                        if (retryCount > 5) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				131
			 | 
			
			
				+                            needUpdate = true; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				132
			 | 
			
			
				+                        retryCount++; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				133
			 | 
			
			
				+                    } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				134
			 | 
			
			
				+                    if (audioHas >= wsize && totalAudioBuf - pos < audioBufLen || pos <= 0 || needUpdate) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				135
			 | 
			
			
				+                        needUpdate = false; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				136
			 | 
			
			
				+                        int len = audioBufLen - audioRPos; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				137
			 | 
			
			
				+                        len = len > wsize ? wsize : len; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				138
			 | 
			
			
				+                        int ret = audioTrack.write(audioBuf, audioRPos, len); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				139
			 | 
			
			
				+                        audioRPos += ret; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				140
			 | 
			
			
				+                        audioHas -= ret; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				141
			 | 
			
			
				+                        totalAudioBuf += ret; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				142
			 | 
			
			
				+                        if (audioRPos == audioBufLen) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				143
			 | 
			
			
				+                            audioRPos = 0; 
			 | 
		
	
		
			
			| 
				132
			 | 
			
				144
			 | 
			
			
				                         } 
			 | 
		
	
		
			
			| 
				133
			 | 
			
				
			 | 
			
			
				-                        if (audioHas >= wsize && totalAudioBuf - pos < audioBufLen || pos <= 0 || needUpdate) { 
			 | 
		
	
		
			
			| 
				134
			 | 
			
				
			 | 
			
			
				-                            needUpdate = false; 
			 | 
		
	
		
			
			| 
				135
			 | 
			
				
			 | 
			
			
				-                            int len = audioBufLen - audioRPos; 
			 | 
		
	
		
			
			| 
				136
			 | 
			
				
			 | 
			
			
				-                            len = len > wsize ? wsize : len; 
			 | 
		
	
		
			
			| 
				137
			 | 
			
				
			 | 
			
			
				-                            int ret = audioTrack.write(audioBuf, audioRPos, len); 
			 | 
		
	
		
			
			| 
				138
			 | 
			
				
			 | 
			
			
				-                            audioRPos += ret; 
			 | 
		
	
		
			
			| 
				139
			 | 
			
				
			 | 
			
			
				-                            audioHas -= ret; 
			 | 
		
	
		
			
			| 
				140
			 | 
			
				
			 | 
			
			
				-                            totalAudioBuf += ret; 
			 | 
		
	
		
			
			| 
				141
			 | 
			
				
			 | 
			
			
				-                            if (audioRPos == audioBufLen) { 
			 | 
		
	
		
			
			| 
				142
			 | 
			
				
			 | 
			
			
				-                                audioRPos = 0; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				145
			 | 
			
			
				+                    } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				146
			 | 
			
			
				+                        //    if (pos == totalAudioBuf && audioHas < wsize){ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				147
			 | 
			
			
				+                        //        int c = audioTrack.write(silencBuf,0,2048); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				148
			 | 
			
			
				+                        //        silenceLen +=c; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				149
			 | 
			
			
				+                        //    } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				150
			 | 
			
			
				+                    } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				151
			 | 
			
			
				+                    if (ispause) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				152
			 | 
			
			
				+                        if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PAUSED) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				153
			 | 
			
			
				+                            audioTrack.pause(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				154
			 | 
			
			
				+                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				155
			 | 
			
			
				+                                audioTrack.setVolume(0); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				156
			 | 
			
			
				+                            } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				157
			 | 
			
			
				+                                audioTrack.setStereoVolume(0, 0); 
			 | 
		
	
		
			
			| 
				143
			 | 
			
				158
			 | 
			
			
				                             } 
			 | 
		
	
		
			
			| 
				144
			 | 
			
				
			 | 
			
			
				-                        } else { 
			 | 
		
	
		
			
			| 
				145
			 | 
			
				
			 | 
			
			
				-                            //    if (pos == totalAudioBuf && audioHas < wsize){ 
			 | 
		
	
		
			
			| 
				146
			 | 
			
				
			 | 
			
			
				-                            //        int c = audioTrack.write(silencBuf,0,2048); 
			 | 
		
	
		
			
			| 
				147
			 | 
			
				
			 | 
			
			
				-                            //        silenceLen +=c; 
			 | 
		
	
		
			
			| 
				148
			 | 
			
				
			 | 
			
			
				-                            //    } 
			 | 
		
	
		
			
			| 
				149
			 | 
			
				159
			 | 
			
			
				                         } 
			 | 
		
	
		
			
			| 
				150
			 | 
			
				
			 | 
			
			
				-                        if (ispause) { 
			 | 
		
	
		
			
			| 
				151
			 | 
			
				
			 | 
			
			
				-                            if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PAUSED) { 
			 | 
		
	
		
			
			| 
				152
			 | 
			
				
			 | 
			
			
				-                                audioTrack.pause(); 
			 | 
		
	
		
			
			| 
				153
			 | 
			
				
			 | 
			
			
				-                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
			 | 
		
	
		
			
			| 
				154
			 | 
			
				
			 | 
			
			
				-                                    audioTrack.setVolume(0); 
			 | 
		
	
		
			
			| 
				155
			 | 
			
				
			 | 
			
			
				-                                } else { 
			 | 
		
	
		
			
			| 
				156
			 | 
			
				
			 | 
			
			
				-                                    audioTrack.setStereoVolume(0, 0); 
			 | 
		
	
		
			
			| 
				157
			 | 
			
				
			 | 
			
			
				-                                } 
			 | 
		
	
		
			
			| 
				158
			 | 
			
				
			 | 
			
			
				-                            } 
			 | 
		
	
		
			
			| 
				159
			 | 
			
				
			 | 
			
			
				-                        } else { 
			 | 
		
	
		
			
			| 
				160
			 | 
			
				
			 | 
			
			
				-                            if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) { 
			 | 
		
	
		
			
			| 
				161
			 | 
			
				
			 | 
			
			
				-                                audioTrack.play(); 
			 | 
		
	
		
			
			| 
				162
			 | 
			
				
			 | 
			
			
				-                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
			 | 
		
	
		
			
			| 
				163
			 | 
			
				
			 | 
			
			
				-                                    audioTrack.setVolume(volume); 
			 | 
		
	
		
			
			| 
				164
			 | 
			
				
			 | 
			
			
				-                                } else { 
			 | 
		
	
		
			
			| 
				165
			 | 
			
				
			 | 
			
			
				-                                    audioTrack.setStereoVolume(volume, volume); 
			 | 
		
	
		
			
			| 
				166
			 | 
			
				
			 | 
			
			
				-                                } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				160
			 | 
			
			
				+                    } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				161
			 | 
			
			
				+                        if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				162
			 | 
			
			
				+                            audioTrack.play(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				163
			 | 
			
			
				+                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				164
			 | 
			
			
				+                                audioTrack.setVolume(volume); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				165
			 | 
			
			
				+                            } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				166
			 | 
			
			
				+                                audioTrack.setStereoVolume(volume, volume); 
			 | 
		
	
		
			
			| 
				167
			 | 
			
				167
			 | 
			
			
				                             } 
			 | 
		
	
		
			
			| 
				168
			 | 
			
				168
			 | 
			
			
				                         } 
			 | 
		
	
		
			
			| 
				169
			 | 
			
				169
			 | 
			
			
				                     } 
			 | 
		
	
		
			
			| 
				170
			 | 
			
				
			 | 
			
			
				-                    try { 
			 | 
		
	
		
			
			| 
				171
			 | 
			
				
			 | 
			
			
				-                        sleep(3); 
			 | 
		
	
		
			
			| 
				172
			 | 
			
				
			 | 
			
			
				-                    } catch (Exception ex) { 
			 | 
		
	
		
			
			| 
				173
			 | 
			
				
			 | 
			
			
				-                    } 
			 | 
		
	
		
			
			| 
				174
			 | 
			
				170
			 | 
			
			
				                 } 
			 | 
		
	
		
			
			| 
				175
			 | 
			
				
			 | 
			
			
				-                Log.d("mixplayer", "audio close"); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				171
			 | 
			
			
				+                try { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				172
			 | 
			
			
				+                    sleep(3); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				173
			 | 
			
			
				+                } catch (Exception ignored) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				174
			 | 
			
			
				+                } 
			 | 
		
	
		
			
			| 
				176
			 | 
			
				175
			 | 
			
			
				             } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				176
			 | 
			
			
				+            Log.d("mixplayer", "audio close"); 
			 | 
		
	
		
			
			| 
				177
			 | 
			
				177
			 | 
			
			
				         }).start(); 
			 | 
		
	
		
			
			| 
				178
			 | 
			
				178
			 | 
			
			
				         audioTrack.play(); 
			 | 
		
	
		
			
			| 
				179
			 | 
			
				179
			 | 
			
			
				     } 
			 |