本文整理匯總了Java中android.media.MediaFormat類的典型用法代碼示例。如果您正苦於以下問題:Java MediaFormat類的具體用法?Java MediaFormat怎麽用?Java MediaFormat使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MediaFormat類屬於android.media包,在下文中一共展示了MediaFormat類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: selectTrack
import android.media.MediaFormat; //導入依賴的package包/類
private MediaFormat selectTrack(MediaExtractor extractor) {
int trackCount = extractor.getTrackCount();
Log.d(TAG, "trackCount :" + trackCount);
MediaFormat format;
for (int i = 0; i < trackCount; i++) {
extractor.selectTrack(i);
format = extractor.getTrackFormat(i);
Log.d(TAG, "Track media format :" + format.toString());
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
return format;
}
}
return null;
}
示例2: setup
import android.media.MediaFormat; //導入依賴的package包/類
@Override
public void setup() throws IOException {
mExtractor.selectTrack(mTrackIndex);
mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoder.start();
mEncoderStarted = true;
final MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
mDecoder.configure(inputFormat, null, null, 0);
mDecoder.start();
mDecoderStarted = true;
mAudioChannel = new AudioChannel(mDecoder, mEncoder, mOutputFormat);
}
示例3: getDuration
import android.media.MediaFormat; //導入依賴的package包/類
/**
* 獲取視頻信息
*
* @param url
* @return
*/
public static long getDuration(String url) {
try {
MediaExtractor mediaExtractor = new MediaExtractor();
mediaExtractor.setDataSource(url);
int videoExt = TrackUtils.selectVideoTrack(mediaExtractor);
if(videoExt == -1){
videoExt = TrackUtils.selectAudioTrack(mediaExtractor);
if(videoExt == -1){
return 0;
}
}
MediaFormat mediaFormat = mediaExtractor.getTrackFormat(videoExt);
long res = mediaFormat.containsKey(MediaFormat.KEY_DURATION) ? mediaFormat.getLong(MediaFormat.KEY_DURATION) : 0;//時長
mediaExtractor.release();
return res;
} catch (Exception e) {
return 0;
}
}
示例4: open
import android.media.MediaFormat; //導入依賴的package包/類
@Override
public Point open(SurfaceTexture surface) {
try {
if(!extractMedia()){
return new Point(0,0);
}
mFrameSem=new Semaphore(0);
mDecodeSem=new Semaphore(1);
videoProvideEndFlag=false;
isUserWantToStop=false;
mAudioEncodeTrack=mStore.addTrack(mExtractor.getTrackFormat(mAudioDecodeTrack));
MediaFormat format=mExtractor.getTrackFormat(mVideoDecodeTrack);
mVideoDecoder = MediaCodec.createDecoderByType(format.getString(MediaFormat.KEY_MIME));
mVideoDecoder.configure(format,new Surface(surface),null,0);
mVideoDecoder.start();
startDecodeThread();
} catch (IOException e) {
e.printStackTrace();
}
return mVideoSize;
}
示例5: setup
import android.media.MediaFormat; //導入依賴的package包/類
@Override
public void setup() throws IOException {
mExtractor.selectTrack(mTrackIndex);
mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoderInputSurfaceWrapper = new InputSurface(mEncoder.createInputSurface());
mEncoder.start();
mEncoderStarted = true;
mEncoderOutputBuffers = mEncoder.getOutputBuffers();
MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
// if (inputFormat.containsKey(MediaUtil.KEY_ROTATION)) {
// // Decoded video is rotated automatically in Android 5.0 lollipop.
// // Turn off here because we don't want to encode rotated one.
// // refer: https://android.googlesource
// // .com/platform/frameworks/av/+blame/lollipop-release/media/libstagefright/Utils.cpp
// inputFormat.setInteger(MediaUtil.KEY_ROTATION, 0);
// }
mDecoderOutputSurfaceWrapper = new OutputSurface(mSurfaceRender);
mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
mDecoder.configure(inputFormat, mDecoderOutputSurfaceWrapper.getSurface(), null, 0);
mDecoder.start();
mDecoderStarted = true;
mDecoderInputBuffers = mDecoder.getInputBuffers();
}
示例6: recorderEncoderLoop
import android.media.MediaFormat; //導入依賴的package包/類
/**
* Reads bytes from the given recorder and encodes them with the given encoder.
* Uses the (deprecated) Synchronous Processing using Buffer Arrays.
* <p/>
* Encoders (or codecs that generate compressed data) will create and return the codec specific
* data before any valid output buffer in output buffers marked with the codec-config flag.
* Buffers containing codec-specific-data have no meaningful timestamps.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void recorderEncoderLoop(MediaCodec codec, SpeechRecord speechRecord) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
codec.start();
// Getting some buffers (e.g. 4 of each) to communicate with the codec
ByteBuffer[] codecInputBuffers = codec.getInputBuffers();
ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers();
Log.i("input buffers " + codecInputBuffers.length + "; output buffers: " + codecOutputBuffers.length);
boolean doneSubmittingInput = false;
int numRetriesDequeueOutputBuffer = 0;
int index;
while (true) {
if (!doneSubmittingInput) {
index = codec.dequeueInputBuffer(DEQUEUE_TIMEOUT);
if (index >= 0) {
int size = queueInputBuffer(codec, codecInputBuffers, index, speechRecord);
if (size == -1) {
codec.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
Log.i("enc: in: EOS");
doneSubmittingInput = true;
} else {
Log.i("enc: in: " + size);
mNumBytesSubmitted += size;
}
} else {
Log.i("enc: in: timeout, will try again");
}
}
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
index = codec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
Log.i("enc: out: flags/index: " + info.flags + "/" + index);
if (index == MediaCodec.INFO_TRY_AGAIN_LATER) {
Log.i("enc: out: INFO_TRY_AGAIN_LATER: " + numRetriesDequeueOutputBuffer);
if (++numRetriesDequeueOutputBuffer > MAX_NUM_RETRIES_DEQUEUE_OUTPUT_BUFFER) {
break;
}
} else if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
MediaFormat format = codec.getOutputFormat();
Log.i("enc: out: INFO_OUTPUT_FORMAT_CHANGED: " + format.toString());
} else if (index == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
codecOutputBuffers = codec.getOutputBuffers();
Log.i("enc: out: INFO_OUTPUT_BUFFERS_CHANGED");
} else {
dequeueOutputBuffer(codec, codecOutputBuffers, index, info);
mNumBytesDequeued += info.size;
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
Log.i("enc: out: EOS");
break;
}
}
}
codec.stop();
codec.release();
}
}
開發者ID:vaibhavs4424,項目名稱:AI-Powered-Intelligent-Banking-Platform,代碼行數:64,代碼來源:EncodedAudioRecorder.java
示例7: configureDecoder
import android.media.MediaFormat; //導入依賴的package包/類
private static MediaCodec configureDecoder(ImageInfo info, int maxInputSize, Surface surface) {
if (mDecoderSupportedSize.getWidth() < info.size.getWidth() || mDecoderSupportedSize.getHeight() < info.size.getHeight()) {
Log.w(TAG, "HEVC image may exceed decoder capability");
}
try {
MediaCodec decoder = MediaCodec.createByCodecName(mDecoderName);
MediaFormat inputFormat = MediaFormat.createVideoFormat(
MediaFormat.MIMETYPE_VIDEO_HEVC, info.size.getWidth(), info.size.getHeight());
inputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize);
inputFormat.setByteBuffer("csd-0", info.paramset);
Log.d(TAG, "HEVC input-format=" + inputFormat);
decoder.configure(inputFormat, surface, null, 0);
return decoder;
} catch (IOException ex) {
throw new RuntimeException("no HEVC decoding support");
}
}
示例8: getAvailableEncoders
import android.media.MediaFormat; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<String> getAvailableEncoders(int sampleRate) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
MediaFormat format = MediaFormatFactory.createMediaFormat(MediaFormatFactory.Type.FLAC, sampleRate);
MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
String encoderAsStr = mcl.findEncoderForFormat(format);
List<String> encoders = new ArrayList<>();
for (MediaCodecInfo info : mcl.getCodecInfos()) {
if (info.isEncoder()) {
if (info.getName().equals(encoderAsStr)) {
encoders.add("*** " + info.getName() + ": " + TextUtils.join(", ", info.getSupportedTypes()));
} else {
encoders.add(info.getName() + ": " + TextUtils.join(", ", info.getSupportedTypes()));
}
}
}
return encoders;
}
return Collections.emptyList();
}
示例9: recorderLoop
import android.media.MediaFormat; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void recorderLoop(SpeechRecord speechRecord) {
mNumBytesSubmitted = 0;
mNumBytesDequeued = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
MediaFormat format = MediaFormatFactory.createMediaFormat(MediaFormatFactory.Type.FLAC, getSampleRate());
List<String> componentNames = AudioUtils.getEncoderNamesForType(format.getString(MediaFormat.KEY_MIME));
for (String componentName : componentNames) {
Log.i("component/format: " + componentName + "/" + format);
MediaCodec codec = AudioUtils.createCodec(componentName, format);
if (codec != null) {
recorderEncoderLoop(codec, speechRecord);
if (Log.DEBUG) {
AudioUtils.showMetrics(format, mNumBytesSubmitted, mNumBytesDequeued);
}
break; // TODO: we use the first one that is suitable
}
}
}
}
開發者ID:vaibhavs4424,項目名稱:AI-Powered-Intelligent-Banking-Platform,代碼行數:22,代碼來源:EncodedAudioRecorder.java
示例10: createMediaCodecFormat
import android.media.MediaFormat; //導入依賴的package包/類
private MediaFormat createMediaCodecFormat(int colorFmt, int width, int height, Integer profile, Integer level){
String mime = mEncodeFormat.getValue(); // "video/avc"
MediaFormat mediaFormat = MediaFormat.createVideoFormat(mime, width, height);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFmt);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
mediaFormat.setFloat(MediaFormat.KEY_FRAME_RATE, (float) mFrameRate*1.0f);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, mIFrameInterval);
if(null != profile)
{
//mediaFormat.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AVCProfileHigh);
mediaFormat.setInteger(MediaFormat.KEY_PROFILE, profile.intValue());
}
if(null != level)
{
//mediaFormat.setInteger(MediaFormat.KEY_LEVEL, MediaCodecInfo.CodecProfileLevel.AVCLevel4);
//mediaFormat.setInteger(MediaFormat.KEY_LEVEL, level.intValue());
}
return mediaFormat;
}
示例11: createMediaCodec
import android.media.MediaFormat; //導入依賴的package包/類
private MediaCodec createMediaCodec(int bufferSize) throws IOException {
MediaCodec mediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat mediaFormat = new MediaFormat();
mediaFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, CHANNELS);
mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, bufferSize);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
try {
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (Exception e) {
Log.w(TAG, e);
mediaCodec.release();
throw new IOException(e);
}
return mediaCodec;
}
示例12: createMediaFormat
import android.media.MediaFormat; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static MediaFormat createMediaFormat(Type type, int sampleRate) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
MediaFormat format = new MediaFormat();
// TODO: this causes a crash in MediaCodec.configure
//format.setString(MediaFormat.KEY_FRAME_RATE, null);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
if (type == Type.AAC) {
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_AAC_PROFILE, 2); // TODO: or 39?
format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
} else if (type == Type.FLAC) {
//format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_FLAC); // API=21
format.setString(MediaFormat.KEY_MIME, "audio/flac");
format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
//TODO: use another bit rate, does not seem to have effect always
//format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
} else {
format.setString(MediaFormat.KEY_MIME, "audio/amr-wb");
format.setInteger(MediaFormat.KEY_BIT_RATE, 23050);
}
return format;
}
return null;
}
開發者ID:vaibhavs4424,項目名稱:AI-Powered-Intelligent-Banking-Platform,代碼行數:27,代碼來源:MediaFormatFactory.java
示例13: getFirstVideoAndAudioTrack
import android.media.MediaFormat; //導入依賴的package包/類
public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) {
TrackResult trackResult = new TrackResult();
trackResult.mVideoTrackIndex = -1;
trackResult.mAudioTrackIndex = -1;
int trackCount = extractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) {
trackResult.mVideoTrackIndex = i;
trackResult.mVideoTrackMime = mime;
trackResult.mVideoTrackFormat = format;
} else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) {
trackResult.mAudioTrackIndex = i;
trackResult.mAudioTrackMime = mime;
trackResult.mAudioTrackFormat = format;
}
if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break;
}
if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) {
throw new IllegalArgumentException("extractor does not contain video and/or audio tracks.");
}
return trackResult;
}
示例14: mediaFormatFromStaticPayloadType
import android.media.MediaFormat; //導入依賴的package包/類
private static MediaFormat mediaFormatFromStaticPayloadType(String m, int p) {
MediaFormat format = null;
PayloadFormat payloadFormat = PayloadFormat.staticPayloadType(p);
if (payloadFormat != null) {
switch (m.toLowerCase()) {
case "audio":
format = MediaFormat.createAudioFormat(
payloadFormat.mimeType,
payloadFormat.clockRate,
payloadFormat.channelCount);
break;
case "video":
// TODO: Video not supported. Not sure how to get video dimensions
//format = MediaFormat.createVideoFormat();
// Perhaps just set MIME type
//format = new MediaFormat();
//format.setString(MediaFormat.KEY_MIME, payloadFormat.mimeType);
break;
}
}
return format;
}
示例15: AudioEncoderCore
import android.media.MediaFormat; //導入依賴的package包/類
public AudioEncoderCore(MMediaMuxer MMediaMuxer) throws IOException {
super(MMediaMuxer);
final MediaFormat audioFormat = MediaFormat.createAudioFormat(MIME_TYPE, SAMPLE_RATE, 1);
audioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
audioFormat.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_IN_MONO);
audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
audioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
// audioFormat.setLong(MediaFormat.KEY_MAX_INPUT_SIZE, inputFile.length());
// audioFormat.setLong(MediaFormat.KEY_DURATION, (long)durationInMs );
if (VERBOSE) Log.i(TAG, "format: " + audioFormat);
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
mEncoder.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoder.start();
if (mAudioThread == null) {
mAudioThread = new AudioThread();
mAudioThread.start();
capturing=true;
stopped=false;
}
}