android.media.MediaMuxer.addTrack()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中android.media.MediaMuxer.addTrack()方法的一些代码示例,展示了MediaMuxer.addTrack()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MediaMuxer.addTrack()方法的具体详情如下:
包路径:android.media.MediaMuxer
类名称:MediaMuxer
方法名:addTrack

MediaMuxer.addTrack介绍

暂无

代码示例

代码示例来源:origin: TeamNewPipe/NewPipe

muxer.addTrack(tracks[0].getTrackFormat(0)),
    muxer.addTrack(tracks[1].getTrackFormat(0))
};

代码示例来源:origin: guardianproject/haven

mTrackIndex = mMuxer.addTrack(newFormat);
mMuxer.start();
mMuxerStarted = true;

代码示例来源:origin: robolectric/robolectric

format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
int trackIndex = muxer.addTrack(format);
muxer.start();

代码示例来源:origin: guoxiaoxing/phoenix

private void onSetOutputFormat() {
  if (mVideoFormat == null || mAudioFormat == null) return;
  mListener.onDetermineOutputFormat();
  mVideoTrackIndex = mMuxer.addTrack(mVideoFormat);
  Log.v(TAG, "Added track #" + mVideoTrackIndex + " with " + mVideoFormat.getString(MediaFormat.KEY_MIME) + " to muxer");
  mAudioTrackIndex = mMuxer.addTrack(mAudioFormat);
  Log.v(TAG, "Added track #" + mAudioTrackIndex + " with " + mAudioFormat.getString(MediaFormat.KEY_MIME) + " to muxer");
  mMuxer.start();
  mStarted = true;
  if (mByteBuffer == null) {
    mByteBuffer = ByteBuffer.allocate(0);
  }
  mByteBuffer.flip();
  Log.v(TAG, "Output format determined, writing " + mSampleInfoList.size() +
      " samples / " + mByteBuffer.limit() + " bytes to muxer.");
  MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
  int offset = 0;
  for (SampleInfo sampleInfo : mSampleInfoList) {
    sampleInfo.writeToBufferInfo(bufferInfo, offset);
    mMuxer.writeSampleData(getTrackIndexForSampleType(sampleInfo.mSampleType), mByteBuffer, bufferInfo);
    offset += sampleInfo.mSize;
  }
  mSampleInfoList.clear();
  mByteBuffer = null;
}

代码示例来源:origin: WangShuo1143368701/WSLiveDemo

/**
 * assign encoder to muxer
 * @param format
 * @return minus value indicate error
 */
/*package*/ synchronized int addTrack(final MediaFormat format) {
  if (mIsStarted)
    throw new IllegalStateException("muxer already started");
  final int trackIx = mMediaMuxer.addTrack(format);
  if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format);
  return trackIx;
}

代码示例来源:origin: FacePlusPlus/MegviiFacepp-Android-SDK

/**
 * assign encoder to muxer
 * @param format
 * @return minus value indicate error
 */
/*package*/ synchronized int addTrack(final MediaFormat format) {
  if (mIsStarted)
    throw new IllegalStateException("muxer already started");
  final int trackIx = mMediaMuxer.addTrack(format);
  if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format);
  return trackIx;
}

代码示例来源:origin: saki4510t/AudioVideoRecordingSample

/**
 * assign encoder to muxer
 * @param format
 * @return minus value indicate error
 */
/*package*/ synchronized int addTrack(final MediaFormat format) {
  if (mIsStarted)
    throw new IllegalStateException("muxer already started");
  final int trackIx = mMediaMuxer.addTrack(format);
  if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format);
  return trackIx;
}

代码示例来源:origin: saki4510t/ScreenRecordingSample

/**
 * assign encoder to muxer
 * @param format
 * @return minus value indicate error
 */
/*package*/ synchronized int addTrack(final MediaFormat format) {
  if (mIsStarted)
    throw new IllegalStateException("muxer already started");
  final int trackIx = mMediaMuxer.addTrack(format);
  if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format);
  return trackIx;
}

代码示例来源:origin: saki4510t/libcommon

@Override
public int addTrack(@NonNull final MediaFormat format) {
  return mMuxer.addTrack(format);
}

代码示例来源:origin: dingjikerbo/Android-RTSP

public int addTrack(MediaFormat trackFormat) {
  if (mStarted) {
    throw new IllegalStateException();
  }
  synchronized (mMuxer) {
    int track = mMuxer.addTrack(trackFormat);
    if (++mNumTracks == mExpectedNumTracks) {
      mMuxer.start();
      mStarted = true;
    }
    return track;
  }
}

代码示例来源:origin: windrunnerlihuan/DogCamera

public int addTrack(MediaFormat trackFormat) {
  if (mStarted) {
    throw new IllegalStateException();
  }
  synchronized (mMuxer) {
    int track = mMuxer.addTrack(trackFormat);
    if (++mNumTracks == mExpectedNumTracks) {
      mMuxer.start();
      mStarted = true;
    }
    return track;
  }
}

代码示例来源:origin: MasayukiSuda/CameraRecorder-android

/**
 * assign encoder to muxer
 *
 * @param format
 * @return minus value indicate error
 */
synchronized int addTrack(final MediaFormat format) {
  if (isStarted) {
    throw new IllegalStateException("muxer already started");
  }
  final int trackIx = mediaMuxer.addTrack(format);
  Log.i(TAG, "addTrack:trackNum=" + encoderCount + ",trackIx=" + trackIx + ",format=" + format);
  String mime = format.getString(MediaFormat.KEY_MIME);
  if (!mime.startsWith("video/")) {
    audioTrackIndex = trackIx;
  }
  return trackIx;
}

代码示例来源:origin: MasayukiSuda/GPUVideo-android

/**
 * assign encoder to muxer
 *
 * @param format
 * @return minus value indicate error
 */
synchronized int addTrack(final MediaFormat format) {
  if (isStarted) {
    throw new IllegalStateException("muxer already started");
  }
  final int trackIx = mediaMuxer.addTrack(format);
  Log.i(TAG, "addTrack:trackNum=" + encoderCount + ",trackIx=" + trackIx + ",format=" + format);
  String mime = format.getString(MediaFormat.KEY_MIME);
  if (!mime.startsWith("video/")) {
    audioTrackIndex = trackIx;
  }
  return trackIx;
}

代码示例来源:origin: pedroSG94/rtmp-rtsp-stream-client-java

@Override
public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
 if (recording) {
  if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME
    && !canRecord
    && videoFormat != null
    && audioFormat != null) {
   videoTrack = mediaMuxer.addTrack(videoFormat);
   audioTrack = mediaMuxer.addTrack(audioFormat);
   mediaMuxer.start();
   canRecord = true;
  }
  if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info);
 }
 if (streaming) getH264DataRtp(h264Buffer, info);
}

代码示例来源:origin: pedroSG94/rtmp-rtsp-stream-client-java

@Override
public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
 if (recording) {
  if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME
    && !canRecord
    && videoFormat != null
    && audioFormat != null) {
   videoTrack = mediaMuxer.addTrack(videoFormat);
   audioTrack = mediaMuxer.addTrack(audioFormat);
   mediaMuxer.start();
   canRecord = true;
  }
  if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info);
 }
 if (streaming) getH264DataRtp(h264Buffer, info);
}

代码示例来源:origin: pedroSG94/rtmp-rtsp-stream-client-java

@Override
public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
 if (recording) {
  if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME
    && !canRecord
    && videoFormat != null
    && audioFormat != null) {
   videoTrack = mediaMuxer.addTrack(videoFormat);
   audioTrack = mediaMuxer.addTrack(audioFormat);
   mediaMuxer.start();
   canRecord = true;
  }
  if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info);
 }
 if (streaming) getH264DataRtp(h264Buffer, info);
}

代码示例来源:origin: pedroSG94/rtmp-rtsp-stream-client-java

@Override
public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && recording) {
  if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME
    && !canRecord
    && videoFormat != null
    && audioFormat != null) {
   videoTrack = mediaMuxer.addTrack(videoFormat);
   audioTrack = mediaMuxer.addTrack(audioFormat);
   mediaMuxer.start();
   canRecord = true;
  }
  if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info);
 }
 if (streaming) getH264DataRtp(h264Buffer, info);
}

代码示例来源:origin: WangShuo1143368701/VideoView

private void resetOutputFormat() {
  // should happen before receiving buffers, and should only happen once
  if (mMuxerStarted) {
    throw new IllegalStateException("output format already changed!");
  }
  MediaFormat newFormat = mMediaCodec.getOutputFormat();
  Log.i(TAG, "output format changed.\n new format: " + newFormat.toString());
  mVideoTrackIndex = mMuxer.addTrack(newFormat);
  mMuxer.start();
  mMuxerStarted = true;
  Log.i(TAG, "started media muxer, videoIndex=" + mVideoTrackIndex);
}

代码示例来源:origin: ChillingVan/AndroidInstantVideo

private void addTrackAndReadyToStart(int type) {
  if (trackCnt == 2) {
    return;
  }
  if (videoTrackIndex == null && type == FramePool.Frame.TYPE_VIDEO) {
    MediaFormat videoOutputMediaFormat = params.getVideoOutputMediaFormat();
    videoOutputMediaFormat.getByteBuffer("csd-0");    // SPS
    videoOutputMediaFormat.getByteBuffer("csd-1");    // PPS
    videoTrackIndex = mMuxer.addTrack(videoOutputMediaFormat);
    trackCnt++;
  } else if (audioTrackIndex == null && type == FramePool.Frame.TYPE_AUDIO) {
    MediaFormat audioOutputMediaFormat = params.getAudioOutputMediaFormat();
    audioTrackIndex = mMuxer.addTrack(audioOutputMediaFormat);
    trackCnt++;
  }
  if (trackCnt == 2) {
    frameSender.sendStartMessage();
  }
}

代码示例来源:origin: aserbao/AndroidCamera

public boolean prepareVideo(){
  try {
    mVideoBufferInfo = new MediaCodec.BufferInfo();
    MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
    //1. 设置一些属性。没有指定其中的一些可能会导致MediaCodec.configure()调用抛出一个无用的异常。
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);//比特率(比特率越高,音视频质量越高,编码文件越大)
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMES_PER_SECOND);//设置帧速
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);//设置关键帧间隔时间
    //2.创建一个MediaCodec编码器,并配置格式。获取一个我们可以用于输入的表面,并将其封装到处理EGL工作的类中。
    mVideoMediaCodec = MediaCodec.createEncoderByType("video/avc");
    mVideoMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mInputSurface = mVideoMediaCodec.createInputSurface();
    mVideoMediaCodec.start();
    MediaFormat newFormat = mVideoMediaCodec.getOutputFormat();
    mWriteVideoTrackIndex = mediaMuxer.addTrack(newFormat);
    mediaMuxer.start();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return true;
}

相关文章