本文整理匯總了Java中android.media.MediaCodec.createEncoderByType方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaCodec.createEncoderByType方法的具體用法?Java MediaCodec.createEncoderByType怎麽用?Java MediaCodec.createEncoderByType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.media.MediaCodec
的用法示例。
在下文中一共展示了MediaCodec.createEncoderByType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: VideoEncoderCore
import android.media.MediaCodec; //導入方法依賴的package包/類
/**
* Configures encoder and muxer state, and prepares the input Surface.
*/
public VideoEncoderCore(int width, int height, int bitRate, MMediaMuxer MMediaMuxer)
throws IOException {
super(MMediaMuxer);
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, width, height);
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, calcBitRate(width,height));
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
if (VERBOSE) Log.d(TAG, "format: " + format);
// Create a MediaCodec encoder, and configure it with our format. Get a Surface
// we can use for input and wrap it with a class that handles the EGL work.
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
}
示例2: openVideoEncoder
import android.media.MediaCodec; //導入方法依賴的package包/類
private void openVideoEncoder(){
AvLog.d(TAG,"openVideoEncoder startTime-->");
if(mVideoEncoder==null){
try {
MediaFormat format=convertVideoConfigToFormat(mConfig.mVideo);
mVideoEncoder= MediaCodec.createEncoderByType(mConfig.mVideo.mime);
mVideoEncoder.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
super.setSurface(mVideoEncoder.createInputSurface());
super.setOutputSize(mConfig.mVideo.width,mConfig.mVideo.height);
mVideoEncoder.start();
isEncodeStarted=true;
} catch (IOException e) {
e.printStackTrace();
}
}
AvLog.d(TAG,"openVideoEncoder endTime-->");
}
示例3: createMediaCodec
import android.media.MediaCodec; //導入方法依賴的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;
}
示例4: setup
import android.media.MediaCodec; //導入方法依賴的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();
}
示例5: checkMediaCodecAudioEncoderSupport
import android.media.MediaCodec; //導入方法依賴的package包/類
@TargetApi(MIN_API_LEVEL_AUDIO)
public static int checkMediaCodecAudioEncoderSupport(){
if(getApiLevel()<MIN_API_LEVEL_AUDIO){
Log.d(TAG, "checkMediaCodecAudioEncoderSupport: Min API is 16");
return CODEC_REQ_API_NOT_SATISFIED;
}
final MediaFormat audioFormat = MediaFormat.createAudioFormat(MIME_TYPE_AUDIO, TEST_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, TEST_AUDIO_BIT_RATE);
audioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
MediaCodec mediaCodec;
try {
mediaCodec = MediaCodec.createEncoderByType(MIME_TYPE_AUDIO);
mediaCodec.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
mediaCodec.stop();
mediaCodec.release();
mediaCodec = null;
} catch (Exception ex) {
Log.e(TAG, "Failed on creation of codec #", ex);
return CODEC_ERROR;
}
return CODEC_SUPPORTED;
}
示例6: AudioEncoderCore
import android.media.MediaCodec; //導入方法依賴的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;
}
}
示例7: startRecord
import android.media.MediaCodec; //導入方法依賴的package包/類
public void startRecord() throws IOException {
synchronized (REC_LOCK){
isRecordStarted=true;
MediaFormat audioFormat=mConfig.getAudioFormat();
mAudioEncoder=MediaCodec.createEncoderByType(audioFormat.getString(MediaFormat.KEY_MIME));
mAudioEncoder.configure(audioFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
MediaFormat videoFormat=mConfig.getVideoFormat();
mVideoEncoder=MediaCodec.createEncoderByType(videoFormat.getString(MediaFormat.KEY_MIME));
//此處不能用mOutputSurface,會configure失敗
mVideoEncoder.configure(videoFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncodeSurface=mVideoEncoder.createInputSurface();
mAudioEncoder.start();
mVideoEncoder.start();
mMuxer=new MediaMuxer(mOutputPath,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
mRecordBufferSize = AudioRecord.getMinBufferSize(mRecordSampleRate,
mRecordChannelConfig, mRecordAudioFormat)*2;
// buffer=new byte[bufferSize];
mAudioRecord=new AudioRecord(MediaRecorder.AudioSource.MIC,mRecordSampleRate,mRecordChannelConfig,
mRecordAudioFormat,mRecordBufferSize);
mAudioThread=new Thread(new Runnable() {
@Override
public void run() {
mAudioRecord.startRecording();
while (!audioEncodeStep(isTryStopAudio)){};
mAudioRecord.stop();
}
});
mAudioThread.start();
isRecordAudioStarted=true;
}
}
示例8: getAudioMediaCodec
import android.media.MediaCodec; //導入方法依賴的package包/類
@TargetApi(18)
public static MediaCodec getAudioMediaCodec(){
MediaFormat format = MediaFormat.createAudioFormat(
Options.getInstance().audio.mime,
Options.getInstance().audio.frequency,
Options.getInstance().audio.channelCount);
if(Options.getInstance().audio.mime.equals(Options.DEFAULT_MIME)) {
format.setInteger(MediaFormat.KEY_AAC_PROFILE, Options.getInstance().audio.aacProfile);
}
format.setInteger(MediaFormat.KEY_BIT_RATE, Options.getInstance().audio.maxBps * 1024);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, Options.getInstance().audio.frequency);
int maxInputSize = getRecordBufferSize();
format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, Options.getInstance().audio.channelCount);
MediaCodec mediaCodec = null;
try {
mediaCodec = MediaCodec.createEncoderByType(Options.getInstance().audio.mime);
mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (Exception e) {
e.printStackTrace();
if (mediaCodec != null) {
mediaCodec.stop();
mediaCodec.release();
mediaCodec = null;
}
}
return mediaCodec;
}
示例9: createMediaCodecEncoder
import android.media.MediaCodec; //導入方法依賴的package包/類
public static MediaCodec createMediaCodecEncoder(String mime) throws IOException {
return MediaCodec.createEncoderByType(mime);
/*String hardwareEncoderName = findHardwareEncodeeName(mime);
if(null == hardwareEncoderName || hardwareEncoderName.equals("AACEncoder")){
return MediaCodec.createEncoderByType(mime);
}
else{
Log.i(TAG,"hardwareEncoderName = " + hardwareEncoderName);
return MediaCodec.createByCodecName(hardwareEncoderName);
}*/
}
示例10: getMediaCodec
import android.media.MediaCodec; //導入方法依賴的package包/類
public static MediaCodec getMediaCodec () {
MediaCodec encoder = null;
try {
encoder = MediaCodec.createEncoderByType(AccessConstants.FORMAT);
} catch (IOException e) {
e.printStackTrace();
}
encoder.configure(DeEncodecCommon.getFormat(), null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
return encoder;
}
示例11: H264Encoder
import android.media.MediaCodec; //導入方法依賴的package包/類
/**
*
* @param eglCtx can be EGL10.EGL_NO_CONTEXT or outside context
*/
public H264Encoder(final StreamPublisher.StreamPublisherParam params, final EglContextWrapper eglCtx) throws IOException {
// MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, params.width, params.height);
MediaFormat format = params.createVideoMediaFormat();
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, params.videoBitRate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, params.frameRate);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, params.iframeInterval);
mEncoder = MediaCodec.createEncoderByType(params.videoMIMEType);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
mediaCodecInputStream = new MediaCodecInputStream(mEncoder, new MediaCodecInputStream.MediaFormatCallback() {
@Override
public void onChangeMediaFormat(MediaFormat mediaFormat) {
params.setVideoOutputMediaFormat(mediaFormat);
}
});
offScreenCanvas = new EncoderCanvas(params.width, params.height, eglCtx);
}
示例12: MpegEncoder
import android.media.MediaCodec; //導入方法依賴的package包/類
/** Constructs a new {@link MpegEncoder} */
private MpegEncoder(@NonNull Builder builder, @NonNull MediaFormat format,
@NonNull String path) throws IOException {
checkState();
mFrameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
mEncoder = MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME));
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mSurface = mEncoder.createInputSurface();
mInputSurface =
InputSurface.create (
mSurface,
builder.inputBuffer,
builder.width,
builder.height
)
.autoSwap()
.build();
mMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
if (builder.mLocation != null) {
mMuxer.setLocation(builder.mLocation.x, builder.mLocation.y);
}
if (builder.mOrientation != 0) {
mMuxer.setOrientationHint(builder.mOrientation);
}
mOutputBuffers = start();
}
示例13: clickStart
import android.media.MediaCodec; //導入方法依賴的package包/類
/**
* onClick handler for "start" button.
*
* We create as many codecs as we can and return without releasing them. The codecs
* will remain in use until the next GC.
*/
public void clickStart(@SuppressWarnings("unused") View unused) {
final String MIME_TYPE = "video/avc";
final int WIDTH = 320;
final int HEIGHT = 240;
final int BIT_RATE = 1000000;
final int FRAME_RATE = 15;
final int IFRAME_INTERVAL = 1;
final boolean START_CODEC = true;
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
Log.d(TAG, "format: " + format);
MediaCodec[] codecs = new MediaCodec[MAX_OPEN];
int i;
for (i = 0; i < MAX_OPEN; i++) {
try {
codecs[i] = MediaCodec.createEncoderByType(MIME_TYPE);
codecs[i].configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
if (START_CODEC) {
codecs[i].createInputSurface();
codecs[i].start();
}
} catch (Exception ex) {
Log.i(TAG, "Failed on creation of codec #" + i, ex);
break;
}
}
showCountDialog(i);
}
示例14: prepareEncoder
import android.media.MediaCodec; //導入方法依賴的package包/類
private void prepareEncoder() throws IOException {
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
RecordScreenLogUtil.i(TAG, "created video format: " + format);
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mSurface = mEncoder.createInputSurface();
mEncoder.start();
}
示例15: VideoEncoderCore
import android.media.MediaCodec; //導入方法依賴的package包/類
/**
* Configures encoder and muxer state, and prepares the input Surface.
*/
public VideoEncoderCore(int width, int height, int bitRate, File outputFile)
throws IOException {
mBufferInfo = new MediaCodec.BufferInfo();
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, width, height);
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
if (VERBOSE) Log.d(TAG, "format: " + format);
// Create a MediaCodec encoder, and configure it with our format. Get a Surface
// we can use for input and wrap it with a class that handles the EGL work.
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
// Create a MediaMuxer. We can't add the video track and start() the muxer here,
// because our MediaFormat doesn't have the Magic Goodies. These can only be
// obtained from the encoder after it has started processing data.
//
// We're not actually interested in multiplexing audio. We just want to convert
// the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
mMuxer = new MediaMuxer(outputFile.toString(),
MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
mTrackIndex = -1;
mMuxerStarted = false;
}