Как воспроизвести аудио для длины для видео, используя mp4parser после мультиплексирования в Android
Я сделал слияние аудио и видео с помощью библиотеки mp4parser. У меня проблема с воспроизведением видео. Объединенное видео воспроизводится успешно, но если длина звука больше, чем длина видео, то оставшееся время воспроизводится, когда видео останавливается. Например, у меня есть видео 10 секунд и аудио 20 секунд. Я хочу заменить видео на определенное время и использовать исходное видео для остальных, но приведенный ниже код останавливает исходное видео и заменяет видео для первого 10 секунд, остальные 10 секунд - остановка.
public boolean mux(String videoFile, String audioFile, String outputFile) {
Movie video;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
Movie audio;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
/*int size = audio.getTracks().size();
Track audioTrack = audio.getTracks().get((size - 1));*/
Track audioTrack = audio.getTracks().get(0);
/* TrimVideo trimVideo = new TrimVideo();
trimVideo.correctTimeToNextSyncSample(audioTrack,20.0);*/
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
FileOutputStream fos;
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
try {
out.writeContainer(byteBufferByteChannel);
byteBufferByteChannel.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private static class BufferedWritableFileByteChannel implements WritableByteChannel {
private static final int BUFFER_CAPACITY = 1000000;
private boolean isOpen = true;
private final OutputStream outputStream;
private final ByteBuffer byteBuffer;
private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];
private BufferedWritableFileByteChannel(OutputStream outputStream) {
this.outputStream = outputStream;
this.byteBuffer = ByteBuffer.wrap(rawBuffer);
}
@Override
public int write(ByteBuffer inputBuffer) throws IOException {
int inputBytes = inputBuffer.remaining();
if (inputBytes > byteBuffer.remaining()) {
dumpToFile();
byteBuffer.clear();
if (inputBytes > byteBuffer.remaining()) {
throw new BufferOverflowException();
}
}
byteBuffer.put(inputBuffer);
return inputBytes;
}
@Override
public boolean isOpen() {
return isOpen;
}
@Override
public void close() throws IOException {
dumpToFile();
isOpen = false;
}
private void dumpToFile() {
try {
outputStream.write(rawBuffer, 0, byteBuffer.position());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
И я реализую этот метод, как:
File newFile = new File(Environment.getExternalStorageDirectory(), "/Stakes");
if (!newFile.exists()) {
newFile.mkdirs();
}
File audioFile = new File(Environment.getExternalStorageDirectory(), "/SongList");
String FILE_PATH = audioFile.getAbsolutePath();
File mergedFile = new File(Environment.getExternalStorageDirectory(), "/Stakes_Merged_Video");
if (!mergedFile.exists()) {
mergedFile.mkdirs();
}
File audioVideo = new File(mergedFile, "stakes" + randomInteger(0, 500) + ".mp4");
String output = audioVideo.getAbsolutePath();
Toast.makeText(getApplicationContext(), recordMediaPath, Toast.LENGTH_LONG).show();
Я исследую много о том, как воспроизводить объединенное аудио одинаковой длины для видео, но я не могу добиться успеха. пожалуйста, помогите мне об этом. Заранее спасибо.