本文整理汇总了Java中android.media.MediaExtractor.readSampleData方法的典型用法代码示例。如果您正苦于以下问题:Java MediaExtractor.readSampleData方法的具体用法?Java MediaExtractor.readSampleData怎么用?Java MediaExtractor.readSampleData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.MediaExtractor
的用法示例。
在下文中一共展示了MediaExtractor.readSampleData方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: internal_process_input
import android.media.MediaExtractor; //导入方法依赖的package包/类
/**
* @param codec
* @param extractor
* @param inputBuffers
* @param presentationTimeUs
* @param isAudio
*/
protected boolean internal_process_input(final MediaCodec codec, final MediaExtractor extractor, final ByteBuffer[] inputBuffers, final long presentationTimeUs, final boolean isAudio) {
// if (DEBUG) Log.v(TAG, "internal_process_input:presentationTimeUs=" + presentationTimeUs);
boolean result = true;
while (mIsRunning) {
final int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_USEC);
if (inputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER)
break;
if (inputBufIndex >= 0) {
final int size = extractor.readSampleData(inputBuffers[inputBufIndex], 0);
if (size > 0) {
codec.queueInputBuffer(inputBufIndex, 0, size, presentationTimeUs, 0);
}
result = extractor.advance(); // return false if no data is available
break;
}
}
return result;
}
示例2: writeSample
import android.media.MediaExtractor; //导入方法依赖的package包/类
/**
* Write a media sample to the decoder.
*
* A "sample" here refers to a single atomic access unit in the media stream. The definition
* of "access unit" is dependent on the type of encoding used, but it typically refers to
* a single frame of video or a few seconds of audio. {@link MediaExtractor}
* extracts data from a stream one sample at a time.
*
* @param extractor Instance of {@link MediaExtractor} wrapping the media.
*
* @param presentationTimeUs The time, relative to the beginning of the media stream,
* at which this buffer should be rendered.
*
* @param flags Flags to pass to the decoder. See {@link MediaCodec#queueInputBuffer(int,
* int, int, long, int)}
*
* @throws MediaCodec.CryptoException
*/
public boolean writeSample(final MediaExtractor extractor,
final boolean isSecure,
final long presentationTimeUs,
int flags) {
boolean result = false;
boolean isEos = false;
if (!mAvailableInputBuffers.isEmpty()) {
int index = mAvailableInputBuffers.remove();
ByteBuffer buffer = mInputBuffers[index];
// reads the sample from the file using extractor into the buffer
int size = extractor.readSampleData(buffer, 0);
if (size <= 0) {
flags |= MediaCodec.BUFFER_FLAG_END_OF_STREAM;
}
// Submit the buffer to the codec for decoding. The presentationTimeUs
// indicates the position (play time) for the current sample.
if (!isSecure) {
mDecoder.queueInputBuffer(index, 0, size, presentationTimeUs, flags);
} else {
extractor.getSampleCryptoInfo(cryptoInfo);
mDecoder.queueSecureInputBuffer(index, 0, cryptoInfo, presentationTimeUs, flags);
}
result = true;
}
return result;
}
示例3: verifySamplesMatch
import android.media.MediaExtractor; //导入方法依赖的package包/类
/**
* Uses 2 MediaExtractor, seeking to the same position, reads the sample and
* makes sure the samples match.
*/
private void verifySamplesMatch(int srcMedia, String testMediaPath,
int seekToUs) throws IOException {
AssetFileDescriptor testFd = mResources.openRawResourceFd(srcMedia);
MediaExtractor extractorSrc = new MediaExtractor();
extractorSrc.setDataSource(testFd.getFileDescriptor(),
testFd.getStartOffset(), testFd.getLength());
int trackCount = extractorSrc.getTrackCount();
MediaExtractor extractorTest = new MediaExtractor();
extractorTest.setDataSource(testMediaPath);
assertEquals("wrong number of tracks", trackCount,
extractorTest.getTrackCount());
// Make sure the format is the same and select them
for (int i = 0; i < trackCount; i++) {
MediaFormat formatSrc = extractorSrc.getTrackFormat(i);
MediaFormat formatTest = extractorTest.getTrackFormat(i);
String mimeIn = formatSrc.getString(MediaFormat.KEY_MIME);
String mimeOut = formatTest.getString(MediaFormat.KEY_MIME);
if (!(mimeIn.equals(mimeOut))) {
fail("format didn't match on track No." + i +
formatSrc.toString() + "\n" + formatTest.toString());
}
extractorSrc.selectTrack(i);
extractorTest.selectTrack(i);
}
// Pick a time and try to compare the frame.
extractorSrc.seekTo(seekToUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
extractorTest.seekTo(seekToUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
int bufferSize = MAX_SAMPLE_SIZE;
ByteBuffer byteBufSrc = ByteBuffer.allocate(bufferSize);
ByteBuffer byteBufTest = ByteBuffer.allocate(bufferSize);
extractorSrc.readSampleData(byteBufSrc, 0);
extractorTest.readSampleData(byteBufTest, 0);
if (!(byteBufSrc.equals(byteBufTest))) {
fail("byteBuffer didn't match");
}
}
示例4: verifySamplesMatch
import android.media.MediaExtractor; //导入方法依赖的package包/类
/**
* Uses 2 MediaExtractor, seeking to the same position, reads the sample and
* makes sure the samples match.
*/
private void verifySamplesMatch(int srcMedia, String testMediaPath,
int seekToUs) throws IOException {
AssetFileDescriptor testFd = mResources.openRawResourceFd(srcMedia);
MediaExtractor extractorSrc = new MediaExtractor();
extractorSrc.setDataSource(testFd.getFileDescriptor(),
testFd.getStartOffset(), testFd.getLength());
int trackCount = extractorSrc.getTrackCount();
MediaExtractor extractorTest = new MediaExtractor();
extractorTest.setDataSource(testMediaPath);
assertEquals("wrong number of tracks", trackCount,
extractorTest.getTrackCount());
// Make sure the format is the same and select them
for (int i = 0; i < trackCount; i++) {
MediaFormat formatSrc = extractorSrc.getTrackFormat(i);
MediaFormat formatTest = extractorTest.getTrackFormat(i);
String mimeIn = formatSrc.getString(MediaFormat.KEY_MIME);
String mimeOut = formatTest.getString(MediaFormat.KEY_MIME);
if (!(mimeIn.equals(mimeOut))) {
fail("format didn't match on track No." + i +
formatSrc.toString() + "\n" + formatTest.toString());
}
extractorSrc.selectTrack(i);
extractorTest.selectTrack(i);
}
// Pick a time and try to compare the frame.
extractorSrc.seekTo(seekToUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
extractorTest.seekTo(seekToUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
int bufferSize = MAX_SAMPLE_SIZE;
ByteBuffer byteBufSrc = ByteBuffer.allocate(bufferSize);
ByteBuffer byteBufTest = ByteBuffer.allocate(bufferSize);
extractorSrc.readSampleData(byteBufSrc, 0);
extractorTest.readSampleData(byteBufTest, 0);
if (!(byteBufSrc.equals(byteBufTest))) {
fail("byteBuffer didn't match");
}
}
示例5: readAndWriteTrack
import android.media.MediaExtractor; //导入方法依赖的package包/类
@TargetApi(16)
private long readAndWriteTrack(final MessageObject messageObject, MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception {
int trackIndex = selectTrack(extractor, isAudio);
if (trackIndex >= 0) {
extractor.selectTrack(trackIndex);
MediaFormat trackFormat = extractor.getTrackFormat(trackIndex);
int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio);
int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
boolean inputDone = false;
if (start > 0) {
extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
} else {
extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
}
ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
long startTime = -1;
checkConversionCanceled();
long lastTimestamp = -100;
while (!inputDone) {
checkConversionCanceled();
boolean eof = false;
int index = extractor.getSampleTrackIndex();
if (index == trackIndex) {
info.size = extractor.readSampleData(buffer, 0);
if (info.size >= 0) {
info.presentationTimeUs = extractor.getSampleTime();
} else {
info.size = 0;
eof = true;
}
if (info.size > 0 && !eof) {
if (start > 0 && startTime == -1) {
startTime = info.presentationTimeUs;
}
if (end < 0 || info.presentationTimeUs < end) {
if (info.presentationTimeUs > lastTimestamp) {
info.offset = 0;
info.flags = extractor.getSampleFlags();
if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) {
didWriteData(messageObject, file, false, false);
}
}
lastTimestamp = info.presentationTimeUs;
} else {
eof = true;
}
}
if (!eof) {
extractor.advance();
}
} else if (index == -1) {
eof = true;
} else {
extractor.advance();
}
if (eof) {
inputDone = true;
}
}
extractor.unselectTrack(trackIndex);
return startTime;
}
return -1;
}
示例6: readAndWriteTrack
import android.media.MediaExtractor; //导入方法依赖的package包/类
@TargetApi(16)
private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception {
int trackIndex = selectTrack(extractor, isAudio);
if (trackIndex >= 0) {
extractor.selectTrack(trackIndex);
MediaFormat trackFormat = extractor.getTrackFormat(trackIndex);
int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio);
int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
boolean inputDone = false;
if (start > 0) {
extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
} else {
extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
}
ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
long startTime = -1;
while (!inputDone) {
boolean eof = false;
int index = extractor.getSampleTrackIndex();
if (index == trackIndex) {
info.size = extractor.readSampleData(buffer, 0);
if (info.size < 0) {
info.size = 0;
eof = true;
} else {
info.presentationTimeUs = extractor.getSampleTime();
if (start > 0 && startTime == -1) {
startTime = info.presentationTimeUs;
}
if (end < 0 || info.presentationTimeUs < end) {
info.offset = 0;
info.flags = extractor.getSampleFlags();
if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) {
// didWriteData(messageObject, file, false, false);
}
extractor.advance();
} else {
eof = true;
}
}
} else if (index == -1) {
eof = true;
}
if (eof) {
inputDone = true;
}
}
extractor.unselectTrack(trackIndex);
return startTime;
}
return -1;
}
示例7: extractAndFeedDecoder
import android.media.MediaExtractor; //导入方法依赖的package包/类
/**
* Extract and feed to decoder.
*
* @return Finished. True when it extracts the last frame.
*/
private boolean extractAndFeedDecoder(MediaCodec decoder, ByteBuffer[] buffers, Component component) {
String type = component.getType() == Component.COMPONENT_TYPE_VIDEO ? "video" : "audio";
int decoderInputBufferIndex = decoder.dequeueInputBuffer(TIMEOUT_USEC);
if (decoderInputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
mLogger.d(String.format("no %s decoder input buffer", type));
return false;
}
mLogger.d(String.format("%s decoder: returned input buffer: %d", type, decoderInputBufferIndex));
MediaExtractor extractor = component.getMediaExtractor();
int size = extractor.readSampleData(buffers[decoderInputBufferIndex], 0);
long presentationTime = extractor.getSampleTime();
mLogger.d(String.format("%s extractor: returned buffer of size %d", type, size));
mLogger.d(String.format("%s extractor: returned buffer for time %d", type, presentationTime));
if (mTrimEndTime > 0 && presentationTime > (mTrimEndTime * 1000)) {
mLogger.d("The current sample is over the trim time. Lets stop.");
decoder.queueInputBuffer(
decoderInputBufferIndex,
0,
0,
0,
MediaCodec.BUFFER_FLAG_END_OF_STREAM);
return true;
}
if (size >= 0) {
decoder.queueInputBuffer(
decoderInputBufferIndex,
0,
size,
presentationTime,
extractor.getSampleFlags());
mStats.incrementExtractedFrameCount(component);
}
if (!extractor.advance()) {
mLogger.d(String.format("%s extractor: EOS", type));
decoder.queueInputBuffer(
decoderInputBufferIndex,
0,
0,
0,
MediaCodec.BUFFER_FLAG_END_OF_STREAM);
return true;
}
return false;
}