android.media.MediaMetadataRetriever Java Examples
The following examples show how to use
android.media.MediaMetadataRetriever.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AudioFileCoverFetcher.java From Phonograph with GNU General Public License v3.0 | 7 votes |
@Override public InputStream loadData(final Priority priority) throws Exception { final MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(model.filePath); byte[] picture = retriever.getEmbeddedPicture(); if (picture != null) { stream = new ByteArrayInputStream(picture); } else { stream = AudioFileCoverUtils.fallback(model.filePath); } } finally { retriever.release(); } return stream; }
Example #2
Source File: PlayActivity.java From AudioAnchor with GNU General Public License v3.0 | 7 votes |
void setAlbumCover() { // Set the album cover to the ImageView and the album title to the TextView Point size = new Point(); WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display; int reqSize = getResources().getDimensionPixelSize(R.dimen.default_device_width); if (wm != null) { display = wm.getDefaultDisplay(); display.getSize(size); reqSize = java.lang.Math.max(size.x, size.y); } if (mCoverFromMetadata) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(mAudioFile.getPath()); byte[] coverData = mmr.getEmbeddedPicture(); // Convert the byte array to a bitmap if (coverData != null) { Bitmap bitmap = BitmapFactory.decodeByteArray(coverData, 0, coverData.length); mCoverIV.setImageBitmap(bitmap); } else { BitmapUtils.setImage(mCoverIV, mAudioFile.getCoverPath(), reqSize); } } else { BitmapUtils.setImage(mCoverIV, mAudioFile.getCoverPath(), reqSize); } mCoverIV.setAdjustViewBounds(true); }
Example #3
Source File: Util.java From audioview with Boost Software License 1.0 | 6 votes |
public static String getTrackTitle(Context context, Object source) { MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); try { if (source instanceof String) metaRetriever.setDataSource((String) source); if (source instanceof Uri) metaRetriever.setDataSource(context, (Uri) source); if (source instanceof FileDescriptor) metaRetriever.setDataSource((FileDescriptor) source); } catch (IllegalArgumentException ignored) { } String artist = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); String title = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); metaRetriever.release(); if (artist != null && !TextUtils.isEmpty(artist) && title != null && !TextUtils.isEmpty(title)) return artist + " - " + title; if ((artist == null || TextUtils.isEmpty(artist)) && title != null && !TextUtils.isEmpty(title)) return title; if (artist == null || TextUtils.isEmpty(artist)) return context.getString(R.string.no_title); return artist; }
Example #4
Source File: Metadata.java From intent_radio with MIT License | 6 votes |
@Override protected String doInBackground(Void... args) { try { log("Metadata start: ", url); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); log("Metadata 1."); retriever.setDataSource(context,Uri.parse(url)); // FIXME: // This is broken! // Never reaches here! log("Metadata 2."); String title = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); log("Metadata done."); return title != null ? title : null; } catch (Exception e) { return null; } }
Example #5
Source File: AndroidUtils.java From iGap-Android with GNU Affero General Public License v3.0 | 6 votes |
public static String getAudioArtistName(String filePath) throws IllegalArgumentException { MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); Uri uri; File file = new File(filePath); if (file.exists()) { uri = Uri.fromFile(file); try { metaRetriever.setDataSource(G.context, uri); return metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); } catch (Exception e) { } } return ""; }
Example #6
Source File: MediaUtils.java From q-municate-android with Apache License 2.0 | 6 votes |
public static MetaData getMetaData(String localPath) { int height = 0; int width = 0; int durationSec = 0; MediaMetadataRetriever mmr = getMediaMetadataRetriever(localPath); String heightStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); String widthStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); if (!TextUtils.isEmpty(heightStr)) { height = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); } if (!TextUtils.isEmpty(widthStr)) { width = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); } if (!TextUtils.isEmpty(durationStr)) { int durationMs = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); durationSec = (int) TimeUnit.MILLISECONDS.toSeconds(durationMs); } return new MetaData(height, width, durationSec); }
Example #7
Source File: VideoMediaStoreUtils.java From mobilecloud-15 with Apache License 2.0 | 6 votes |
/** * Gets the Video from MediaMetadataRetriever * by a given path of the video file. * * @param context * @param filePath of video * @return Video having the given filePath */ public static Video getVideo(Context context, String filePath) { // Get the MediaMetadataRetriever for retrieving // meta data from an input media file. final MediaMetadataRetriever retriever = new MediaMetadataRetriever(); //Set the dataSource to the video file path. retriever.setDataSource(filePath); // Get the video video file name. final String title = new File(filePath).getName(); //Get the duration of the Video. final long duration = Long.parseLong (retriever.extractMetadata (MediaMetadataRetriever.METADATA_KEY_DURATION)); // Create a new Video containing the meta-data. return new Video(title, duration); }
Example #8
Source File: VideoSelectAdapter.java From Android-Video-Trimmer with Apache License 2.0 | 6 votes |
@Override public void bindView(View view, Context context, final Cursor cursor) { final VideoGridViewHolder holder = (VideoGridViewHolder) view.getTag(); final String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)); if (!checkDataValid(cursor)) { return; } final String duration = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); holder.durationTv.setText(DateUtil.convertSecondsToTime(Integer.parseInt(duration) / 1000)); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.videoCover.getLayoutParams(); params.width = videoCoverSize; params.height = videoCoverSize; holder.videoCover.setLayoutParams(params); Glide.with(context) .load(getVideoUri(cursor)) .centerCrop() .override(videoCoverSize, videoCoverSize) .into(holder.videoCover); holder.videoItemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { VideoTrimmerActivity.call((FragmentActivity) mContext, path); } }); }
Example #9
Source File: ExtractPicturesWorker.java From GifAssistant with Apache License 2.0 | 6 votes |
public static Bitmap extractBitmap(String videoPath, int second) { if (TextUtils.isEmpty(videoPath)) { logd("extractBitmap empty video path"); return null; } MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(videoPath); // 取得视频的长度(单位为毫秒) String time = retriever .extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); // 取得视频的长度(单位为秒) int total = Integer.valueOf(time) / 1000; if (second < 0 || second > total) { loge("unavalible second(" + second + "), total(" + total + ")"); return null; } Bitmap bitmap = retriever.getFrameAtTime(second * 1000 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); return bitmap; }
Example #10
Source File: MediaMoviePlayer.java From AudioVideoPlayerSample with Apache License 2.0 | 6 votes |
protected void updateMovieInfo() { mVideoWidth = mVideoHeight = mRotation = mBitrate = 0; mDuration = 0; mFrameRate = 0; String value = mMetadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); if (!TextUtils.isEmpty(value)) { mVideoWidth = Integer.parseInt(value); } value = mMetadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); if (!TextUtils.isEmpty(value)) { mVideoHeight = Integer.parseInt(value); } value = mMetadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); if (!TextUtils.isEmpty(value)) { mRotation = Integer.parseInt(value); } value = mMetadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); if (!TextUtils.isEmpty(value)) { mBitrate = Integer.parseInt(value); } value = mMetadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); if (!TextUtils.isEmpty(value)) { mDuration = Long.parseLong(value) * 1000; } }
Example #11
Source File: FileCreator.java From Saiy-PS with GNU Affero General Public License v3.0 | 6 votes |
/** * Used to get information about the written file */ private void getFileMeta() { if (absolutePath != null) { try { final MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(absolutePath); final String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); mmr.release(); if (DEBUG) { MyLog.i(CLS_NAME, "recording duration: " + duration); } } catch (final RuntimeException e) { if (DEBUG) { MyLog.w(CLS_NAME, "RuntimeException: completeFileWrite"); e.printStackTrace(); } } } }
Example #12
Source File: AndroidFileMetaAccessor.java From java-unified-sdk with Apache License 2.0 | 6 votes |
public Map<String, Object> mediaInfo(File file) { Map<String, Object> meta = new HashMap<String, Object>(); try { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(file.getAbsolutePath()); String durationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); String format = getMimeType(file.getAbsolutePath()); double duration = Long.parseLong(durationString) / 1000.0d; meta.put(AVIMFileMessage.FORMAT, format); meta.put(AVIMFileMessage.DURATION, duration); } catch (Exception e) { meta.put(AVIMFileMessage.DURATION, 0l); meta.put(AVIMFileMessage.FORMAT, ""); } return meta; }
Example #13
Source File: VideoThumbnailFetcher.java From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public InputStream loadData(Priority priority) throws Exception { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { Log.d("VideoThumbnailFetcher", "model.path= " + model.path); retriever.setDataSource(model.path, new HashMap<String, String>()); Bitmap bitmap = retriever.getFrameAtTime(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] picture = stream.toByteArray(); if (picture != null) { return new ByteArrayInputStream(picture); } else { return fallback(model.path); } } finally { retriever.release(); } }
Example #14
Source File: MediaUtils.java From PictureSelector with Apache License 2.0 | 6 votes |
/** * 获取旋转角度 * * @param uri * @return */ public static int getVideoOrientationForUri(Context context, Uri uri) { try { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(context, uri); int orientation = ValueOf.toInt(mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)); switch (orientation) { case 90: return ExifInterface.ORIENTATION_ROTATE_90; case 270: return ExifInterface.ORIENTATION_ROTATE_270; default: return 0; } } catch (Exception e) { e.printStackTrace(); return 0; } }
Example #15
Source File: FilmstripItemUtils.java From Camera2 with Apache License 2.0 | 6 votes |
/** * Loads the thumbnail of a video. * * @param path The path to the video file. * @return {@code null} if the loading failed. */ public static Bitmap loadVideoThumbnail(String path) { Bitmap bitmap = null; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(path); byte[] data = retriever.getEmbeddedPicture(); if (data != null) { bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } if (bitmap == null) { bitmap = retriever.getFrameAtTime(); } } catch (IllegalArgumentException e) { Log.e(TAG, "MediaMetadataRetriever.setDataSource() fail:" + e.getMessage()); } retriever.release(); return bitmap; }
Example #16
Source File: InternalMetaData.java From J2ME-Loader with Apache License 2.0 | 6 votes |
public void updateMetaData(MediaMetadataRetriever retriever) { metakeys.clear(); metadata.clear(); String key, value; for (Integer keyCode : androidMetaKeys) { value = retriever.extractMetadata(keyCode); if (value != null) { key = androidMetaToMIDP.get(keyCode); metakeys.add(key); metadata.put(key, value); } } }
Example #17
Source File: AudioCoverUtil.java From MediaLoader with Apache License 2.0 | 6 votes |
/** * get audio cover * @param filePath * @return */ public static Bitmap createAlbumArt(String filePath) { Bitmap bitmap = null; //能够获取多媒体文件元数据的类 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(filePath); //设置数据源 byte[] bytes = retriever.getEmbeddedPicture(); //得到字节型数据 if(bytes!=null){ bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); //转换为图片 } } catch (Exception e) { e.printStackTrace(); } finally { try { retriever.release(); } catch (Exception e2) { e2.printStackTrace(); } } return bitmap; }
Example #18
Source File: MediaStoreUtils.java From DevUtils with Apache License 2.0 | 6 votes |
/** * 获取本地视频宽高 * @param filePath 文件路径 * @param isAndroidQ 是否兼容 Android Q ( 私有目录传入 false ) * @return 本地视频宽高 0 = 宽, 1 = 高 */ public static int[] getVideoSize(final String filePath, final boolean isAndroidQ) { int[] size = new int[2]; try { if (isAndroidQ && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { return getVideoSize(ContentResolverUtils.getMediaUri(filePath)); } MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(filePath); size[0] = ConvertUtils.toInt(mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); size[1] = ConvertUtils.toInt(mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getVideoSize"); } return size; }
Example #19
Source File: MediaExtractorPlugin.java From media-for-mobile with Apache License 2.0 | 6 votes |
@Override public int getRotation() { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); if (path != null) { retriever.setDataSource(path); } else if (fileDescriptor != null) { retriever.setDataSource(fileDescriptor); } else if (uri != null) { retriever.setDataSource(context, android.net.Uri.parse(uri.getString())); } else { throw new IllegalStateException("File not set"); } String rotation = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); retriever.release(); return Integer.parseInt(rotation); }
Example #20
Source File: MicroPlayer.java From J2ME-Loader with Apache License 2.0 | 6 votes |
@Override public synchronized void prefetch() throws MediaException { checkClosed(); if (state == UNREALIZED) { realize(); } if (state == REALIZED) { try { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(source.getLocator()); metadata.updateMetaData(retriever); retriever.release(); } catch (Exception e) { e.printStackTrace(); } state = PREFETCHED; } }
Example #21
Source File: MediaStoreUtils.java From DevUtils with Apache License 2.0 | 6 votes |
/** * 获取本地视频时长 * @param filePath 文件路径 * @param isAndroidQ 是否兼容 Android Q ( 私有目录传入 false ) * @return 本地视频时长 */ public static long getVideoDuration(final String filePath, final boolean isAndroidQ) { if (TextUtils.isEmpty(filePath)) return 0; try { if (isAndroidQ && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { return getVideoDuration(ContentResolverUtils.getMediaUri(filePath)); } MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(filePath); return Long.parseLong(mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_DURATION)); } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getVideoDuration"); return 0; } }
Example #22
Source File: Util.java From Camera-Roll-Android-App with Apache License 2.0 | 6 votes |
public static int[] getVideoDimensions(String path) throws FileNotFoundException { File file = new File(path); if (!file.exists()) { throw new FileNotFoundException(); } MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(path); Bitmap bitmap = retriever.getFrameAtTime(); int[] dimensions = new int[2]; if (bitmap != null) { dimensions[0] = bitmap.getWidth() > 0 ? bitmap.getWidth() : 1; dimensions[1] = bitmap.getHeight() > 0 ? bitmap.getHeight() : 1; } return dimensions; } catch (Exception e) { e.printStackTrace(); } return new int[]{1, 1}; }
Example #23
Source File: LocalVideoThumbnailProducer.java From fresco with MIT License | 6 votes |
@Nullable private static Bitmap createThumbnailFromContentProvider( ContentResolver contentResolver, Uri uri) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { try { ParcelFileDescriptor videoFile = contentResolver.openFileDescriptor(uri, "r"); MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.setDataSource(videoFile.getFileDescriptor()); return mediaMetadataRetriever.getFrameAtTime(-1); } catch (FileNotFoundException e) { return null; } } else { return null; } }
Example #24
Source File: AttachmentDatabase.java From Silence with GNU General Public License v3.0 | 6 votes |
private ThumbnailData generateVideoThumbnail(MasterSecret masterSecret, AttachmentId attachmentId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { Log.w(TAG, "Video thumbnails not supported..."); return null; } File mediaFile = getAttachmentDataFile(attachmentId, DATA); if (mediaFile == null) { Log.w(TAG, "No data file found for video thumbnail..."); return null; } EncryptedMediaDataSource dataSource = new EncryptedMediaDataSource(masterSecret, mediaFile); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(dataSource); Bitmap bitmap = retriever.getFrameAtTime(1000); Log.w(TAG, "Generated video thumbnail..."); return new ThumbnailData(bitmap); }
Example #25
Source File: MaskProgressLayout.java From AlbumCameraRecorder with MIT License | 6 votes |
@Override public void addAudioCover(String file) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(file); String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); // ms,时长 MultiMediaView multiMediaView = new MultiMediaView(MultimediaTypes.AUDIO); multiMediaView.setPath(file); multiMediaView.setViewHolder(this); // 显示音频播放控件,当点击播放的时候,才正式下载并且进行播放 mViewHolder.playView.setVisibility(View.VISIBLE); isShowRemovceRecorder(); RecordingItem recordingItem = new RecordingItem(); recordingItem.setFilePath(file); recordingItem.setLength(Integer.valueOf(duration)); mViewHolder.playView.setData(recordingItem, audioProgressColor); }
Example #26
Source File: MediaStoreUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 获取本地视频时长 * @param uri Video Uri content:// * @return 本地视频时长 */ public static long getVideoDuration(final Uri uri) { if (uri == null) return 0; try { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(DevUtils.getContext(), uri); return Long.parseLong(mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_DURATION)); } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getVideoDuration"); return 0; } }
Example #27
Source File: AlphaMovieView.java From alpha-movie with Apache License 2.0 | 5 votes |
public void setVideoByUrl(String url) { reset(); try { mediaPlayer.setDataSource(url); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(url, new HashMap<String, String>()); onDataSourceSet(retriever); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } }
Example #28
Source File: AudioFileCoverFetcher.java From RetroMusicPlayer with GNU General Public License v3.0 | 5 votes |
@Override public InputStream loadData(Priority priority) throws Exception { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(model.filePath); byte[] picture = retriever.getEmbeddedPicture(); if (picture != null) { return new ByteArrayInputStream(picture); } else { return fallback(model.filePath); } } finally { retriever.release(); } }
Example #29
Source File: VideoTimelineView.java From Telegram with GNU General Public License v2.0 | 5 votes |
public void setVideoPath(String path) { destroy(); mediaMetadataRetriever = new MediaMetadataRetriever(); progressLeft = 0.0f; progressRight = 1.0f; try { mediaMetadataRetriever.setDataSource(path); String duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); videoLength = Long.parseLong(duration); } catch (Exception e) { FileLog.e(e); } invalidate(); }
Example #30
Source File: VideoFilterAddAction.java From SimpleVideoEditor with Apache License 2.0 | 5 votes |
private boolean checkRational() { if (mInputFile != null && mInputFile.exists() && mInputFile.isFile() && mOutputFile != null && mFromMs >= 0 && mDurationMs >= 0) { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.setDataSource(mInputFile.getAbsolutePath()); long duration = Long.valueOf(mediaMetadataRetriever. extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); mediaMetadataRetriever.release(); if (mFromMs + mDurationMs > duration) { Logger.e(TAG, "Video selected section of out of duration!"); return false; } if (mOutputFile.exists()) { Logger.w(TAG, "WARNING: Output file: " + mOutputFile + " already exists, we will override it!"); } return true; } return false; }