I done audio and video merging using mp4parser library. I have problem for playing video. Merged video is playing successfully but when audio length is greater than video length then remaining time audio is play when video is stop. For example, i have a video of 10 seconds and audio of 20 seconds I want to replace the video for specific time, and use the original video for the rest, but the below code stop the original video, and replaces the video for the first 10 seconds, rest 10 seconds is stop.
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);
}
}
}
And i implement this method like:
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();
I research lots of about how to play merged audio as the same length for video but i cant success. please help me about it. Thanks in advance.