android.media.MediaExtractor.getTrackCount()方法的使用及代码示例

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

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

MediaExtractor.getTrackCount介绍

暂无

代码示例

代码示例来源:origin: Naoki2015/CircleDemo

MediaFormat format;
mHasAudio = false;
for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
  format = mediaExtractor.getTrackFormat(i);
  String mime = format.getString(MediaFormat.KEY_MIME);

代码示例来源:origin: pili-engineering/PLDroidShortVideo

public static int findTrack(final MediaExtractor extractor, final String mimeType) {
  if (extractor == null || mimeType == null) {
    Log.e(TAG, "find track error : extractor or mimeType can't be null!");
    return -1;
  }
  final int numTracks = extractor.getTrackCount();
  MediaFormat format;
  String mime;
  for (int i = 0; i < numTracks; i++) {
    format = extractor.getTrackFormat(i);
    mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith(mimeType)) {
      Log.i(TAG, "Extractor found track " + i + " (" + mime + "): " + format);
      return i;
    }
  }
  return -1;
}

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

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;
  }
}

代码示例来源:origin: waynell/VideoListPlayer

mediaExtractor.setDataSource(mContext, mUri, null);
MediaFormat format;
for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
  format = mediaExtractor.getTrackFormat(i);
  String mime = format.getString(MediaFormat.KEY_MIME);

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

public static int findTrackIndex(MediaExtractor extractor, String mimePrefix) {
  int trackNums = extractor.getTrackCount();
  for (int i = 0; i < trackNums; i++) {
    MediaFormat mediaFormat = extractor.getTrackFormat(i);
    String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith(mimePrefix)) {
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: tangpeng/VideoCompressor

@TargetApi(16)
private int selectTrack(MediaExtractor extractor, boolean audio) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (audio) {
      if (mime.startsWith("audio/")) {
        return i;
      }
    } else {
      if (mime.startsWith("video/")) {
        return i;
      }
    }
  }
  return -5;
}

代码示例来源:origin: LLhon/Android-Video-Editor

@TargetApi(16)
private int selectTrack(MediaExtractor extractor, boolean audio) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (audio) {
      if (mime.startsWith("audio/")) {
        return i;
      }
    } else {
      if (mime.startsWith("video/")) {
        return i;
      }
    }
  }
  return -5;
}

代码示例来源:origin: zhantong/Android-VideoToImages

private static int selectTrack(MediaExtractor extractor) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) {
      if (VERBOSE) {
        Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
      }
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: yangjie10930/OpenGL4Android

/**
 * 查找视频轨道
 * @param extractor
 * @return
 */
public static int selectVideoTrack(MediaExtractor extractor) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) {
      Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: RudreshJR/VideoCompression

@TargetApi(16)
private int selectTrack(MediaExtractor extractor, boolean audio) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (audio) {
      if (mime.startsWith("audio/")) {
        return i;
      }
    } else {
      if (mime.startsWith("video/")) {
        return i;
      }
    }
  }
  return -5;
}

代码示例来源:origin: zolad/VideoSlimmer

private int selectTrack(MediaExtractor extractor, boolean audio) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (audio) {
      if (mime.startsWith("audio/")) {
        return i;
      }
    } else {
      if (mime.startsWith("video/")) {
        return i;
      }
    }
  }
  return MEDIATYPE_NOT_AUDIO_VIDEO;
}

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

/**
 * Selects the video track, if any.
 *
 * @return the track index, or -1 if no video track is found.
 */
private int selectTrack(MediaExtractor extractor) {
  // Select the first video track we find, ignore the rest.
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) {
      if (VERBOSE) {
        Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
      }
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: yangjie10930/OpenGL4Android

/**
   * 查找音频轨道
   * @param extractor
   * @return
   */
  public static int selectAudioTrack(MediaExtractor extractor) {
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
      MediaFormat format = extractor.getTrackFormat(i);
      String mime = format.getString(MediaFormat.KEY_MIME);
      if (mime.startsWith("audio/")) {
        Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
        return i;
      }
    }
    return -1;
  }
}

代码示例来源:origin: stackoverflow.com

MediaExtractor extractor= new MediaExtractor();
File file = new File(this.fileName);

extractor.setDataSource(this.fileName);
int tracks = extractor.getTrackCount();

extractor.selectTrack(0); ..

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

private int getAndSelectAudioTrackIndex(MediaExtractor extractor) {
  for (int index = 0; index < extractor.getTrackCount(); ++index) {
    if (VERBOSE) {
      Log.d(TAG, "format for track " + index + " is "
          + getMimeTypeFor(extractor.getTrackFormat(index)));
    }
    if (isAudioFormat(extractor.getTrackFormat(index))) {
      extractor.selectTrack(index);
      return index;
    }
  }
  return -1;
}

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

private int getAndSelectVideoTrackIndex(MediaExtractor extractor) {
  for (int index = 0; index < extractor.getTrackCount(); ++index) {
    if (VERBOSE) {
      Log.d(TAG, "format for track " + index + " is "
          + getMimeTypeFor(extractor.getTrackFormat(index)));
    }
    if (isVideoFormat(extractor.getTrackFormat(index))) {
      extractor.selectTrack(index);
      return index;
    }
  }
  return -1;
}

代码示例来源:origin: stackoverflow.com

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(MEDIA_PATH);//MEDIA_PATH is your http path of mpeg ts file
for (i = 0; i < extractor.getTrackCount(); i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        /*if (mime.startsWith("audio/")) {
          extractor.selectTrack(i);
          decoder = MediaCodec.createDecoderByType(mime);

          if(decoder != null)
          {
            decoder.configure(format, null  /*surface*/ , null  /*crypto*/ , 0  /*flags*/ );
          }

          break;
        }*/ //this commented code may not useful for you. If you wanna you can use..
      }

代码示例来源:origin: lijundacom/AndroidRTSPLib2

public MP4Encorder(Session session){
  mediaExtractor = new MediaExtractor();
  try {
    mediaExtractor.setDataSource(session.getVideoPath());
  } catch (IOException e) {
    e.printStackTrace();
  }
  for(int i = 0; i < mediaExtractor.getTrackCount(); i++) {
    MediaFormat format = mediaExtractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (!mime.startsWith("video/")) {
      continue;
    }
    framerate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
    mediaExtractor.selectTrack(i);
    session.getVideoQuality().setmFrameRate(framerate);
  }
}

代码示例来源:origin: kollerlukas/Camera-Roll-Android-App

public int retrieveFrameRate() {
  MediaExtractor extractor = new MediaExtractor();
  int frameRate = -1;
  try {
    //Adjust data source as per the requirement if file, URI, etc.
    extractor.setDataSource(getPath());
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
      MediaFormat format = extractor.getTrackFormat(i);
      if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
        frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
      }
    }
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    //Release stuff
    extractor.release();
  }
  return frameRate;
}

代码示例来源:origin: stackoverflow.com

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(...);
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
 MediaFormat format = extractor.getTrackFormat(i);
 String mime = format.getString(MediaFormat.KEY_MIME);
 if (weAreInterestedInThisTrack) {
  extractor.selectTrack(i);
 }
}
ByteBuffer inputBuffer = ByteBuffer.allocate(...)
while (extractor.readSampleData(inputBuffer, ...) >= 0) {
 int trackIndex = extractor.getSampleTrackIndex();
 long presentationTimeUs = extractor.getSampleTime();
 ...
 extractor.advance();
}
extractor.release();
extractor = null;

相关文章