|
@@ -32,6 +32,9 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
* Author by pq, Date on 2022/10/26.
|
|
|
*/
|
|
|
public class MergeTrackManager {
|
|
|
+ public static final int READ_STATUS_END = -1;
|
|
|
+ public static final int READ_STATUS_NOT_START = -2;
|
|
|
+
|
|
|
public static final int PLAY_COMPLETED = 1001;
|
|
|
public static final int MAX_FILE_COUNT = 2;
|
|
|
|
|
@@ -61,7 +64,7 @@ public class MergeTrackManager {
|
|
|
private long playProgress = -1;
|
|
|
private int minBufferSize;
|
|
|
private byte[][] mAllAudioData;
|
|
|
- private byte[][] offsetAudioData;
|
|
|
+ // private byte[][] offsetAudioData;
|
|
|
public static final int RATE = 44100;
|
|
|
|
|
|
public static final float MAX_VOLUME = 1f;
|
|
@@ -307,12 +310,28 @@ public class MergeTrackManager {
|
|
|
}
|
|
|
|
|
|
private int countBytesByPosition(int bytesReadStart, byte[][] allAudioData2) {
|
|
|
- int[] results = new int[offsetAudioData.length];
|
|
|
- for (int i = 0; i < offsetAudioData.length; i++) {
|
|
|
+ int[] results = new int[mAllAudioData.length];
|
|
|
+ for (int i = 0; i < mAllAudioData.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);
|
|
|
+ long timeOffset = positionOffset[i];
|
|
|
+ int startPos = bytesReadStart * minBufferSize;
|
|
|
+
|
|
|
+ LOG.i(TAG, "startPos:" + startPos);
|
|
|
+ LOG.i(TAG, "source:" + mAllAudioData[i].length + "--bytesReadStart:" + bytesReadStart);
|
|
|
+ //希望偏移的位置量
|
|
|
+ int byteOffset = getLengthFromDuration(timeOffset);
|
|
|
+ LOG.i(TAG, "timeOffset:" + timeOffset);
|
|
|
+ LOG.i(TAG, "byteOffset:" + byteOffset);
|
|
|
+ //累加到当前位置处
|
|
|
+ startPos = startPos + byteOffset;
|
|
|
+ LOG.i(TAG, "startPos result:" + startPos);
|
|
|
+ if (startPos >= 0) {
|
|
|
+ int t = splitByteArray(mAllAudioData[i], startPos, buffer);
|
|
|
+ result = t != READ_STATUS_END ? bytesReadStart : READ_STATUS_END;
|
|
|
+ } else {
|
|
|
+ result = bytesReadStart;
|
|
|
+ }
|
|
|
results[i] = result;
|
|
|
allAudioData2[i] = buffer;
|
|
|
}
|
|
@@ -332,18 +351,18 @@ public class MergeTrackManager {
|
|
|
if (allAudioData == null) {
|
|
|
return;
|
|
|
}
|
|
|
- if (offsetAudioData == null) {
|
|
|
- offsetAudioData = new byte[mAllAudioData.length][];
|
|
|
- }
|
|
|
- for (int i = 0; i < allAudioData.length; i++) {
|
|
|
- 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;
|
|
|
- }
|
|
|
+// if (offsetAudioData == null) {
|
|
|
+// offsetAudioData = new byte[mAllAudioData.length][];
|
|
|
+// }
|
|
|
+// for (int i = 0; i < allAudioData.length; i++) {
|
|
|
+// 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) {
|
|
@@ -351,10 +370,6 @@ public class MergeTrackManager {
|
|
|
LOG.i(TAG, "source:" + source);
|
|
|
if (timeOffset != 0) {
|
|
|
int exceptAudioLength = getLengthFromDuration(timeOffset);
|
|
|
- //因为采样率44100 双声道 16位的位深 需要偶数项
|
|
|
- if (exceptAudioLength % 2 != 0) {
|
|
|
- exceptAudioLength += 1;
|
|
|
- }
|
|
|
LOG.i(TAG, "exceptAudioLength:" + exceptAudioLength);
|
|
|
byte[] lastLengthFromExcept = getLastLengthFromExcept(source, exceptAudioLength);
|
|
|
LOG.i(TAG, "lastLengthFromExcept:" + lastLengthFromExcept.length);
|
|
@@ -370,6 +385,10 @@ public class MergeTrackManager {
|
|
|
private int getLengthFromDuration(long timeOffset) {
|
|
|
float d = timeOffset / 1000f;
|
|
|
int exceptAudioLength = (int) (d * (RATE * CHANNEL_CONFIG * AUDIO_FORMAT));
|
|
|
+ //因为采样率44100 双声道 16位的位深 需要偶数项
|
|
|
+ if (exceptAudioLength % 2 != 0) {
|
|
|
+ exceptAudioLength += 1;
|
|
|
+ }
|
|
|
return exceptAudioLength;
|
|
|
}
|
|
|
|
|
@@ -424,18 +443,18 @@ public class MergeTrackManager {
|
|
|
}
|
|
|
|
|
|
private int splitByteArray(byte[] source, int startIndex, byte[] destination) {
|
|
|
- if (startIndex * destination.length >= source.length) {
|
|
|
- return -1;
|
|
|
+ if (startIndex >= source.length) {
|
|
|
+ return READ_STATUS_END;
|
|
|
}
|
|
|
- int remainByteLength = source.length - startIndex * destination.length;
|
|
|
+ int remainByteLength = source.length - startIndex;
|
|
|
boolean isLast = remainByteLength <= destination.length;
|
|
|
if (isLast) {
|
|
|
- System.arraycopy(source, startIndex * destination.length, destination, 0, remainByteLength);
|
|
|
+ System.arraycopy(source, startIndex, destination, 0, remainByteLength);
|
|
|
} else {
|
|
|
- System.arraycopy(source, startIndex * destination.length, destination, 0, destination.length);
|
|
|
+ System.arraycopy(source, startIndex, destination, 0, destination.length);
|
|
|
}
|
|
|
if (isLast) {
|
|
|
- return -1;
|
|
|
+ return READ_STATUS_END;
|
|
|
}
|
|
|
return startIndex;
|
|
|
}
|
|
@@ -609,7 +628,8 @@ public class MergeTrackManager {
|
|
|
if (ISPLAYSOUND) {
|
|
|
return;
|
|
|
}
|
|
|
- if (mAllAudioData == null || offsetAudioData == null) {
|
|
|
+// if (mAllAudioData == null || offsetAudioData == null) {
|
|
|
+ if (mAllAudioData == null) {
|
|
|
return;
|
|
|
}
|
|
|
Observable.create(new ObservableOnSubscribe<Boolean>() {
|
|
@@ -655,11 +675,11 @@ public class MergeTrackManager {
|
|
|
byte[][] allAudioData2 = new byte[mAllAudioData.length][];
|
|
|
while (ISPLAYSOUND) {
|
|
|
int result = countBytesByPosition(bytesReadPos, allAudioData2);
|
|
|
- bytesReadPos = result != -1 ? ++result : -1;
|
|
|
+ bytesReadPos = result != READ_STATUS_END ? ++result : READ_STATUS_END;
|
|
|
LOG.i(TAG, "bytesRead:" + bytesReadPos);
|
|
|
byte[] audiodata = weightAudioMixer.mixRawAudioBytes(allAudioData2);
|
|
|
mAudioTrack.write(audiodata, 0, minBufferSize);
|
|
|
- if (bytesReadPos == -1) {
|
|
|
+ if (bytesReadPos == READ_STATUS_END) {
|
|
|
LOG.i(TAG, "读取完成:" + getPlayProgress());
|
|
|
sendPlayCompletedMsg(audiodata.length);
|
|
|
reset();
|
|
@@ -687,7 +707,7 @@ public class MergeTrackManager {
|
|
|
eventListener = null;
|
|
|
ISPLAYSOUND = false;
|
|
|
mAllAudioData = null;
|
|
|
- offsetAudioData = null;
|
|
|
+// offsetAudioData = null;
|
|
|
if (mHandler != null) {
|
|
|
mHandler.removeCallbacksAndMessages(null);
|
|
|
}
|