android mp4parser,android - Android使用mediamuxer mediacodec和mediaextractor(没有mp4parser)合并mp4和aac文件 - ...

我正在录制音频,然后尝试合并aac音频和mp4视频文件(静音,没有音频),并共享合并的mp4文件。 请帮忙。

recordAudio.java中存在共享代码,如下所示:

public void shareVroom(View view) {

// Toast.makeText(this, "Share feature is temporarily disabled", android.widget.Toast.LENGTH_LONG).show();

// Toast.makeText(this, "Share feature is enabled", android.widget.Toast.LENGTH_LONG).show();

// Code commented for UAT

try {

MediaMultiplexer mediaMultiplexer = new MediaMultiplexer();

mediaMultiplexer.startMuxing(this);

Toast.makeText(this, "in share",Toast.LENGTH_SHORT).show();

String shareableFileName = "";

Intent intentShareFile = new Intent(Intent.ACTION_SEND);

shareableFileName = Environment.getExternalStorageDirectory().getAbsolutePath();

shareableFileName += getString(R.string.vroom_video_output_file_name);

File fileWithinMyDir = new File(shareableFileName);

Uri videoUri=Uri.parse(shareableFileName);

if (fileWithinMyDir.exists()) {

intentShareFile.setType("video/mp4");

intentShareFile.putExtra(Intent.EXTRA_STREAM, videoUri);

intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Listen to my VROOM");

intentShareFile.putExtra(Intent.EXTRA_TEXT, "Vroom attached");

startActivity(Intent.createChooser(intentShareFile, "Share your Vroom with"));

}

} catch (IllegalStateException e) {

e.printStackTrace();

Log.e("tag", e.getMessage(), e);

Toast.makeText(this, "could not shared"+e.getMessage(),Toast.LENGTH_SHORT).show();

}

//TODO:Use event to identify if muxing is done

}

多路复用代码:

public class MediaMultiplexer {

private static final int MAX_SAMPLE_SIZE = 256 * 1024;

public void startMuxing(Context context) {

MediaMuxer muxer = null;

MediaFormat VideoFormat = null;

Resources mResources = context.getResources();

int sourceVideo = R.raw.vid;

String outputVideoFileName = Environment.getExternalStorageDirectory().getAbsolutePath();

outputVideoFileName += context.getString(R.string.vroom_video_output_file_name);

try {

muxer = new MediaMuxer(outputVideoFileName, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

} catch (IOException e) {

e.printStackTrace();

}

MediaExtractor extractorVideo = new MediaExtractor();

try {

AssetFileDescriptor srcVideoFd = mResources.openRawResourceFd(sourceVideo);

extractorVideo.setDataSource(srcVideoFd.getFileDescriptor(), srcVideoFd.getStartOffset(), srcVideoFd.getLength());

int tracks = extractorVideo.getTrackCount();

for (int i = 0; i < tracks; i++) {

MediaFormat mf = extractorVideo.getTrackFormat(i);

String mime = mf.getString(MediaFormat.KEY_MIME);

if (mime.startsWith("video/")) {

extractorVideo.selectTrack(i);

VideoFormat = extractorVideo.getTrackFormat(i);

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

MediaExtractor extractorAudio = new MediaExtractor();

try {

String audioFileName = Environment.getExternalStorageDirectory().getAbsolutePath();

audioFileName += context.getString(R.string.vroom_audio_file_name);

extractorAudio.setDataSource(audioFileName);

int tracks = extractorAudio.getTrackCount();

// Toast.makeText(context, "No of tracks::::" + String.valueOf(tracks), Toast.LENGTH_SHORT).show();

extractorAudio.selectTrack(0);

MediaFormat AudioFormat = extractorAudio.getTrackFormat(0);

int audioTrackIndex = muxer.addTrack(AudioFormat);

int videoTrackIndex = muxer.addTrack(VideoFormat);

boolean sawEOS = false;

boolean sawAudioEOS = false;

int bufferSize = MAX_SAMPLE_SIZE;

ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);

int offset = 100;

MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

muxer.start();

while (!sawEOS) {

bufferInfo.offset = offset;

bufferInfo.size = extractorVideo.readSampleData(dstBuf, offset);

if (bufferInfo.size < 0) {

sawEOS = true;

bufferInfo.size = 0;

} else {

bufferInfo.presentationTimeUs = extractorVideo.getSampleTime();

bufferInfo.flags = extractorVideo.getSampleFlags();

int trackIndex = extractorVideo.getSampleTrackIndex();

muxer.writeSampleData(videoTrackIndex, dstBuf, bufferInfo);

extractorVideo.advance();

}

}

ByteBuffer audioBuf = ByteBuffer.allocate(bufferSize);

while (!sawAudioEOS) {

bufferInfo.offset = offset;

bufferInfo.size = extractorAudio.readSampleData(audioBuf, offset);

if (bufferInfo.size < 0) {

sawAudioEOS = true;

bufferInfo.size = 0;

} else {

bufferInfo.presentationTimeUs = extractorAudio.getSampleTime();

bufferInfo.flags = extractorAudio.getSampleFlags();

int trackIndex = extractorAudio.getSampleTrackIndex();

muxer.writeSampleData(audioTrackIndex, audioBuf, bufferInfo);

extractorAudio.advance();

}

}

muxer.stop();

muxer.release();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值