|
@@ -53,8 +53,8 @@ public class MergeTrackManager {
|
|
|
*/
|
|
|
private boolean ISPLAYSOUND = false;
|
|
|
|
|
|
- private float[] mWeghts;
|
|
|
- private long[] positionOffset;
|
|
|
+ private volatile float[] mWeghts;
|
|
|
+ private volatile long[] positionOffset;
|
|
|
|
|
|
private long totalDuration = -1;
|
|
|
|
|
@@ -150,7 +150,7 @@ public class MergeTrackManager {
|
|
|
public void setPositionOffset(int pos, int timeOffset) {
|
|
|
LOG.i(TAG, "setPositionOffset pos:" + pos + "--value:" + timeOffset);
|
|
|
if (positionOffset == null) {
|
|
|
- positionOffset = new long[2];
|
|
|
+ initPositionOffset(MAX_FILE_COUNT);
|
|
|
}
|
|
|
if (pos < positionOffset.length) {
|
|
|
positionOffset[pos] = timeOffset;
|
|
@@ -207,6 +207,9 @@ public class MergeTrackManager {
|
|
|
//解析
|
|
|
mAllAudioData = parseFile(filePaths);
|
|
|
//对齐
|
|
|
+ if (positionOffset == null) {
|
|
|
+ initPositionOffset(MAX_FILE_COUNT);
|
|
|
+ }
|
|
|
offsetAudioData(mAllAudioData);
|
|
|
startPlay(emitter);
|
|
|
// WeightAudioMixer weightAudioMixer = new WeightAudioMixer(mWeghts);
|
|
@@ -259,6 +262,11 @@ public class MergeTrackManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private void initPositionOffset(int length) {
|
|
|
+ positionOffset = new long[length];
|
|
|
+ Arrays.fill(positionOffset, 0);
|
|
|
+ }
|
|
|
+
|
|
|
private void initWeight(int length) {
|
|
|
mWeghts = new float[length];
|
|
|
Arrays.fill(mWeghts, MAX_VOLUME);
|
|
@@ -299,21 +307,28 @@ public class MergeTrackManager {
|
|
|
}
|
|
|
|
|
|
private int countBytesByPosition(int bytesReadStart, byte[][] allAudioData2) {
|
|
|
- int result = 0;
|
|
|
- boolean hasEnd = false;
|
|
|
+ int[] results = new int[offsetAudioData.length];
|
|
|
for (int i = 0; i < offsetAudioData.length; i++) {
|
|
|
+ int result = 0;
|
|
|
byte[] buffer = new byte[minBufferSize];
|
|
|
LOG.i(TAG, "source:" + offsetAudioData[i].length + "--bytesReadStart:" + bytesReadStart);
|
|
|
result = splitByteArray(offsetAudioData[i], bytesReadStart, buffer);
|
|
|
- if (result == -1) {
|
|
|
- hasEnd = true;
|
|
|
- }
|
|
|
+ results[i] = result;
|
|
|
allAudioData2[i] = buffer;
|
|
|
}
|
|
|
- return hasEnd ? -1 : result;
|
|
|
+ return countResult(results);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int countResult(int[] results) {
|
|
|
+ for (int i = 0; i < results.length; i++) {
|
|
|
+ if (results[i] != -1) {
|
|
|
+ return results[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
- private void offsetAudioData(byte[][] allAudioData) {
|
|
|
+ private synchronized void offsetAudioData(byte[][] allAudioData) {
|
|
|
if (allAudioData == null) {
|
|
|
return;
|
|
|
}
|
|
@@ -321,14 +336,19 @@ public class MergeTrackManager {
|
|
|
offsetAudioData = new byte[mAllAudioData.length][];
|
|
|
}
|
|
|
for (int i = 0; i < allAudioData.length; i++) {
|
|
|
- byte[] afterChangeBytes = countFilePosFromTime(allAudioData[i], positionOffset[i]);
|
|
|
- LOG.i(TAG, "offsetAudioData[i]" + afterChangeBytes.length);
|
|
|
+ byte[] allAudioDatum = allAudioData[i];
|
|
|
+ if (allAudioDatum == null || allAudioDatum.length == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ byte[] afterChangeBytes = countFilePosFromTime(allAudioDatum, positionOffset[i]);
|
|
|
+ LOG.i(TAG, "afterChangeBytes" + i + "-length:" + afterChangeBytes.length);
|
|
|
offsetAudioData[i] = afterChangeBytes;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private byte[] countFilePosFromTime(byte[] source, long timeOffset) {
|
|
|
LOG.i(TAG, "timeOffset:" + timeOffset);
|
|
|
+ LOG.i(TAG, "source:" + source);
|
|
|
if (timeOffset != 0) {
|
|
|
int exceptAudioLength = getLengthFromDuration(timeOffset);
|
|
|
//因为采样率44100 双声道 16位的位深 需要偶数项
|
|
@@ -655,6 +675,8 @@ public class MergeTrackManager {
|
|
|
totalDuration = -1;
|
|
|
eventListener = null;
|
|
|
ISPLAYSOUND = false;
|
|
|
+ mAllAudioData = null;
|
|
|
+ offsetAudioData = null;
|
|
|
if (mHandler != null) {
|
|
|
mHandler.removeCallbacksAndMessages(null);
|
|
|
}
|