List of usage examples for android.media MediaMetadataRetriever METADATA_KEY_DURATION
int METADATA_KEY_DURATION
To view the source code for android.media MediaMetadataRetriever METADATA_KEY_DURATION.
Click Source Link
From source file:com.adityarathi.muo.services.AudioPlaybackService.java
/** * This method combines the current cursor with the specified playlist cursor. * @param newCursor//from www .j a v a 2s . c om */ public void enqueuePlaylistCursor(Cursor newCursor) { String[] matrixCursorColumns = { DBAccessHelper.SONG_ARTIST, DBAccessHelper.SONG_ALBUM, DBAccessHelper.SONG_TITLE, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_DURATION, DBAccessHelper.SONG_GENRE, DBAccessHelper.SONG_ID, DBAccessHelper.SONG_ALBUM_ART_PATH, DBAccessHelper.SONG_SOURCE }; //Create an empty matrix getCursor() with the specified columns. MatrixCursor mMatrixCursor = new MatrixCursor(matrixCursorColumns); //Make a copy of the old getCursor() and copy it's contents over to the matrix getCursor(). Cursor tempCursor = getCursor(); tempCursor.moveToFirst(); MediaMetadataRetriever mMMDR = new MediaMetadataRetriever(); for (int i = 0; i < tempCursor.getCount(); i++) { tempCursor.moveToPosition(i); //Check which type of getCursor() the service currently has. if (getCursor().getColumnIndex(DBAccessHelper.SONG_FILE_PATH) == -1) { //We'll have to manually extract the info from the audio file. /* String songFilePath = tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH)); try { mMMDR.setDataSource(songFilePath); } catch (Exception e) { //Skip the song if there's a problem with reading it. continue; }*/ String songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); String songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); String songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); String songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE); mMatrixCursor .addRow(new Object[] { songArtist, songAlbum, songTitle, "", songDuration, songGenre }); } else { mMatrixCursor.addRow( new Object[] { tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ARTIST)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_TITLE)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_FILE_PATH)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_DURATION)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_GENRE)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ID)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM_ART_PATH)), tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_SOURCE)) }); } } tempCursor.close(); //Copy the contents of the new getCursor() over to the MatrixCursor. if (newCursor.getCount() > 0) { String songArtist = ""; String songAlbum = ""; String songTitle = ""; String filePath = ""; String songDuration = ""; for (int j = 0; j < newCursor.getCount(); j++) { /* newCursor.moveToPosition(j); filePath = newCursor.getString(newCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH)); try { mMMDR.setDataSource(filePath); } catch (Exception e) { continue; }*/ //Get the metadata from the song file. songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE); mMatrixCursor.addRow( new Object[] { songArtist, songAlbum, songTitle, filePath, songDuration, songGenre }); } } mEnqueuePerformed = true; newCursor.close(); mCursor = (Cursor) mMatrixCursor; mMatrixCursor.close(); }
From source file:github.daneren2005.dsub.fragments.SubsonicFragment.java
public void displaySongInfo(final Entry song) { Integer duration = null;/*from ww w . j a va2 s . c o m*/ Integer bitrate = null; String format = null; long size = 0; if (!song.isDirectory()) { try { DownloadFile downloadFile = new DownloadFile(context, song, false); File file = downloadFile.getCompleteFile(); if (file.exists()) { MediaMetadataRetriever metadata = new MediaMetadataRetriever(); metadata.setDataSource(file.getAbsolutePath()); String tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); duration = Integer.parseInt((tmp != null) ? tmp : "0") / 1000; format = FileUtil.getExtension(file.getName()); size = file.length(); // If no duration try to read bitrate tag if (duration == null) { tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); bitrate = Integer.parseInt((tmp != null) ? tmp : "0") / 1000; } else { // Otherwise do a calculation for it // Divide by 1000 so in kbps bitrate = (int) (size / duration) / 1000 * 8; } if (Util.isOffline(context)) { song.setGenre(metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); String year = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR); song.setYear(Integer.parseInt((year != null) ? year : "0")); } } } catch (Exception e) { Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver"); } } if (duration == null) { duration = song.getDuration(); } List<Integer> headers = new ArrayList<>(); List<String> details = new ArrayList<>(); if (!song.isDirectory()) { headers.add(R.string.details_title); details.add(song.getTitle()); } if (song instanceof PodcastEpisode) { headers.add(R.string.details_podcast); details.add(song.getArtist()); headers.add(R.string.details_status); details.add(((PodcastEpisode) song).getStatus()); } else if (!song.isVideo()) { if (song.getArtist() != null && !"".equals(song.getArtist())) { headers.add(R.string.details_artist); details.add(song.getArtist()); } if (song.getAlbum() != null && !"".equals(song.getAlbum())) { headers.add(R.string.details_album); details.add(song.getAlbum()); } } if (song.getTrack() != null && song.getTrack() != 0) { headers.add(R.string.details_track); details.add(Integer.toString(song.getTrack())); } if (song.getGenre() != null && !"".equals(song.getGenre())) { headers.add(R.string.details_genre); details.add(song.getGenre()); } if (song.getYear() != null && song.getYear() != 0) { headers.add(R.string.details_year); details.add(Integer.toString(song.getYear())); } if (!Util.isOffline(context) && song.getSuffix() != null) { headers.add(R.string.details_server_format); details.add(song.getSuffix()); if (song.getBitRate() != null && song.getBitRate() != 0) { headers.add(R.string.details_server_bitrate); details.add(song.getBitRate() + " kbps"); } } if (format != null && !"".equals(format)) { headers.add(R.string.details_cached_format); details.add(format); } if (bitrate != null && bitrate != 0) { headers.add(R.string.details_cached_bitrate); details.add(bitrate + " kbps"); } if (size != 0) { headers.add(R.string.details_size); details.add(Util.formatLocalizedBytes(size, context)); } if (duration != null && duration != 0) { headers.add(R.string.details_length); details.add(Util.formatDuration(duration)); } if (song.getBookmark() != null) { headers.add(R.string.details_bookmark_position); details.add(Util.formatDuration(song.getBookmark().getPosition() / 1000)); } if (song.getRating() != 0) { headers.add(R.string.details_rating); details.add(song.getRating() + " stars"); } headers.add(R.string.details_starred); details.add(Util.formatBoolean(context, song.isStarred())); if (song instanceof PodcastEpisode) { headers.add(R.string.details_description); details.add(song.getAlbum()); } int title; if (song.isDirectory()) { title = R.string.details_title_album; } else if (song instanceof PodcastEpisode) { title = R.string.details_title_podcast; } else { title = R.string.details_title_song; } Util.showDetailsDialog(context, title, headers, details); }
From source file:github.popeen.dsub.fragments.SubsonicFragment.java
public void displaySongInfo(final Entry song) { Integer duration = null;//www .j a v a2 s. c om Integer bitrate = null; String format = null; long size = 0; if (!song.isDirectory()) { try { DownloadFile downloadFile = new DownloadFile(context, song, false); File file = downloadFile.getCompleteFile(); if (file.exists()) { MediaMetadataRetriever metadata = new MediaMetadataRetriever(); metadata.setDataSource(file.getAbsolutePath()); String tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); duration = Integer.parseInt((tmp != null) ? tmp : "0") / 1000; format = FileUtil.getExtension(file.getName()); size = file.length(); // If no duration try to read bitrate tag if (duration == null) { tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); bitrate = Integer.parseInt((tmp != null) ? tmp : "0") / 1000; } else { // Otherwise do a calculation for it // Divide by 1000 so in kbps bitrate = (int) (size / duration) / 1000 * 8; } if (Util.isOffline(context)) { song.setGenre(metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); String year = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR); song.setYear(Integer.parseInt((year != null) ? year : "0")); } } } catch (Exception e) { Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver"); } } if (duration == null) { duration = song.getDuration(); } List<Integer> headers = new ArrayList<>(); List<String> details = new ArrayList<>(); if (!song.isDirectory()) { headers.add(R.string.details_title); details.add(song.getTitle()); } if (song instanceof PodcastEpisode) { headers.add(R.string.details_podcast); details.add(song.getArtist()); headers.add(R.string.details_status); details.add(((PodcastEpisode) song).getStatus()); } else if (!song.isVideo()) { if (song.getArtist() != null && !"".equals(song.getArtist())) { headers.add(R.string.details_artist); details.add(song.getArtist()); } if (song.getAlbum() != null && !"".equals(song.getAlbum())) { headers.add(R.string.details_album); details.add(song.getAlbum()); } } if (song.getTrack() != null && song.getTrack() != 0) { headers.add(R.string.details_track); details.add(Integer.toString(song.getTrack())); } if (song.getGenre() != null && !"".equals(song.getGenre())) { headers.add(R.string.details_genre); details.add(song.getGenre()); } if (song.getYear() != null && song.getYear() != 0) { headers.add(R.string.details_year); details.add(Integer.toString(song.getYear())); } if (!Util.isOffline(context) && song.getSuffix() != null) { headers.add(R.string.details_server_format); details.add(song.getSuffix()); if (song.getBitRate() != null && song.getBitRate() != 0) { headers.add(R.string.details_server_bitrate); details.add(song.getBitRate() + " kbps"); } } if (format != null && !"".equals(format)) { headers.add(R.string.details_cached_format); details.add(format); } if (bitrate != null && bitrate != 0) { headers.add(R.string.details_cached_bitrate); details.add(bitrate + " kbps"); } if (size != 0) { headers.add(R.string.details_size); details.add(Util.formatLocalizedBytes(size, context)); } if (duration != null && duration != 0) { headers.add(R.string.details_length); details.add(Util.formatDuration(duration)); } if (song.getBookmark() != null) { headers.add(R.string.details_bookmark_position); details.add(Util.formatDuration(song.getBookmark().getPosition() / 1000)); } if (song.getRating() != 0) { headers.add(R.string.details_rating); details.add(song.getRating() + " stars"); } headers.add(R.string.details_starred); details.add(Util.formatBoolean(context, song.isStarred())); try { Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song); if (dates != null && dates[0] != null && dates[0] > 0) { headers.add(R.string.details_last_played); details.add(Util.formatDate((dates[1] != null && dates[1] > dates[0]) ? dates[1] : dates[0])); } } catch (Exception e) { Log.e(TAG, "Failed to get last played", e); } if (song instanceof PodcastEpisode) { headers.add(R.string.details_description); details.add(song.getAlbum()); } int title; if (song.isDirectory()) { title = R.string.details_title_album; } else if (song instanceof PodcastEpisode) { title = R.string.details_title_podcast; } else { title = R.string.details_title_song; } Util.showDetailsDialog(context, title, headers, details); }
From source file:org.tomahawk.tomahawk_android.services.PlaybackService.java
/** * Update the playback controls/views which are being shown on the lockscreen *///w ww .j a v a 2 s. co m @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void updateLockscreenControls() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { Log.d(TAG, "updateLockscreenControls()"); // Use the media button APIs (if available) to register ourselves for media button // events MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent); if (mRemoteControlClientCompat == null) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat( PendingIntent.getBroadcast(PlaybackService.this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); } // Use the remote control APIs (if available) to set the playback state if (isPlaying()) { mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } else { mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); } int flags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE; if (hasNextEntry()) { flags |= RemoteControlClient.FLAG_KEY_MEDIA_NEXT; } if (hasPreviousEntry()) { flags |= RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS; } mRemoteControlClientCompat.setTransportControlFlags(flags); // Update the remote controls synchronized (this) { RemoteControlClientCompat.MetadataEditorCompat editor = mRemoteControlClientCompat .editMetadata(true); editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, getCurrentQuery().getArtist().getPrettyName()) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, getCurrentQuery().getArtist().getPrettyName()) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getCurrentQuery().getPrettyName()) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getCurrentQuery().getPreferredTrack().getDuration()); if (!TextUtils.isEmpty(getCurrentQuery().getAlbum().getPrettyName())) { editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, getCurrentQuery().getAlbum().getPrettyName()); } editor.apply(); Log.d(TAG, "Setting lockscreen metadata to: " + getCurrentQuery().getArtist().getPrettyName() + ", " + getCurrentQuery().getPrettyName()); } Picasso.with(TomahawkApp.getContext()).cancelRequest(mLockscreenTarget); ImageUtils.loadImageIntoBitmap(TomahawkApp.getContext(), getCurrentQuery().getImage(), mLockscreenTarget, Image.getLargeImageSize(), getCurrentQuery().hasArtistImage()); } }
From source file:com.andrew.apollo.MusicPlaybackService.java
private static void changeRemoteControlClientTask(MusicPlaybackService musicPlaybackService, int playState, long position) { // background portion Bitmap albumArt = musicPlaybackService.getAlbumArt(); // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need // to make sure not to hand out our cache copy Bitmap.Config config = null;/*ww w. j a v a 2 s . c o m*/ if (albumArt != null) { config = albumArt.getConfig(); } if (config == null) { config = Bitmap.Config.ARGB_8888; } Bitmap bmpCopy = null; try { if (albumArt != null) { bmpCopy = albumArt.copy(config, false); } } catch (OutOfMemoryError e) { // ignore, can't do anything meaningful here } final Bitmap albumArtCopy = bmpCopy; final String artistName = musicPlaybackService.getArtistName(); final String albumName = musicPlaybackService.getAlbumName(); final String trackName = musicPlaybackService.getTrackName(); final String albumArtistName = musicPlaybackService.getAlbumArtistName(); final long duration = musicPlaybackService.duration(); // MusicPlayerHandler thread portion, we can't put this as a PostContextTask // in Async.async. final WeakReference<MusicPlaybackService> musicPlaybackServiceRef = Ref.weak(musicPlaybackService); Runnable postExecute = () -> { if (!Ref.alive(musicPlaybackServiceRef)) { return; } MusicPlaybackService musicPlaybackService1 = musicPlaybackServiceRef.get(); try { RemoteControlClient.MetadataEditor editor = musicPlaybackService1.mRemoteControlClient .editMetadata(true).putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artistName) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, albumArtistName) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, albumName) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, trackName) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration); if (albumArtCopy != null) { editor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, albumArtCopy); } editor.apply(); } catch (Throwable t) { // possible NPE on android.media.RemoteControlClient$MetadataEditor.apply() } musicPlaybackService1.mRemoteControlClient.setPlaybackState(playState, position, 1.0f); }; musicPlaybackService.mPlayerHandler.post(postExecute); }
From source file:com.firefly.sample.castcompanionlibrary.cast.VideoCastManager.java
private void updateLockScreenMetadata() { if (null == mRemoteControlClientCompat || !isFeatureEnabled(FEATURE_LOCKSCREEN)) { return;/*from ww w . j a v a 2 s .c om*/ } try { // Update the remote controls MediaInfo info = getRemoteMediaInformation(); if (null == info) { return; } final MediaMetadata mm = info.getMetadata(); mRemoteControlClientCompat.editMetadata(false) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mContext.getResources().getString(R.string.casting_to_device, getDeviceName())) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, info.getStreamDuration()).apply(); } catch (NotFoundException e) { LOGE(TAG, "Failed to update RCC due to resource not found", e); } catch (TransientNetworkDisconnectionException e) { LOGE(TAG, "Failed to update RCC due to network issues", e); } catch (NoConnectionException e) { LOGE(TAG, "Failed to update RCC due to network issues", e); } }
From source file:air.com.snagfilms.cast.chromecast.VideoChromeCastManager.java
private void updateLockScreenMetadata() { if (null == mRemoteControlClientCompat || !isFeatureEnabled(FEATURE_LOCKSCREEN)) { return;//from w w w . j a v a 2s . com } try { // Update the remote controls MediaInfo info = getRemoteMediaInformation(); if (null == info) { return; } final MediaMetadata mm = info.getMetadata(); mRemoteControlClientCompat.editMetadata(false) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mContext.getResources().getString(R.string.casting_to_device, getDeviceName())) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, info.getStreamDuration()).apply(); } catch (NotFoundException e) { Log.e(TAG, "Failed to update RCC due to resource not found", e); } catch (TransientNetworkDisconnectionException e) { Log.e(TAG, "Failed to update RCC due to network issues", e); } catch (NoConnectionException e) { Log.e(TAG, "Failed to update RCC due to network issues", e); } }
From source file:com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager.java
private void updateLockScreenMetadata() { if ((mRemoteControlClientCompat == null) || !isFeatureEnabled(FEATURE_LOCKSCREEN)) { return;// w w w . ja v a 2 s . c o m } try { // Update the remote controls MediaInfo info = getRemoteMediaInformation(); if (info == null) { return; } final MediaMetadata mm = info.getMetadata(); mRemoteControlClientCompat.editMetadata(false) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mContext.getResources().getString(R.string.ccl_casting_to_device, getDeviceName())) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, info.getStreamDuration()).apply(); } catch (NotFoundException e) { LOGE(TAG, "Failed to update RCC due to resource not found", e); } catch (TransientNetworkDisconnectionException | NoConnectionException e) { LOGE(TAG, "Failed to update RCC due to network issues", e); } }