导入方法依赖的package包/类
import android.support.v4.media.MediaMetadataCompat;
MediaControllerCompat mController = new MediaControllerCompat(getActivity(),getSessionToken());
MediaMetadataCompat metadata = mController.getMetadata();
PlaybackStateCompat playBackState = mController.getPlaybackState();
示例1: createMetadata
private MediaMetadataCompat createMetadata() {
MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE,
mMediaNotificationInfo.metadata.getTitle());
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE,
mMediaNotificationInfo.origin);
} else {
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE,
mMediaNotificationInfo.metadata.getTitle());
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST,
mMediaNotificationInfo.origin);
}
if (!TextUtils.isEmpty(mMediaNotificationInfo.metadata.getArtist())) {
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST,
mMediaNotificationInfo.metadata.getArtist());
}
if (!TextUtils.isEmpty(mMediaNotificationInfo.metadata.getAlbum())) {
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM,
mMediaNotificationInfo.metadata.getAlbum());
}
return metadataBuilder.build();
}
示例2: getMetadata
public static MediaMetadataCompat getMetadata(Context context, String mediaId) {
MediaMetadataCompat metadataWithoutBitmap = music.get(mediaId);
Bitmap albumArt = getAlbumBitmap(context, mediaId);
// Since MediaMetadataCompat is immutable, we need to create a copy to set the album art.
// We don't set it initially on all items so that they don't take unnecessary memory.
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
for (String key :
new String[]{
MediaMetadataCompat.METADATA_KEY_MEDIA_ID,
MediaMetadataCompat.METADATA_KEY_ALBUM,
MediaMetadataCompat.METADATA_KEY_ARTIST,
MediaMetadataCompat.METADATA_KEY_GENRE,
MediaMetadataCompat.METADATA_KEY_TITLE
}) {
builder.putString(key, metadataWithoutBitmap.getString(key));
}
builder.putLong(
MediaMetadataCompat.METADATA_KEY_DURATION,
metadataWithoutBitmap.getLong(MediaMetadataCompat.METADATA_KEY_DURATION));
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt);
return builder.build();
}
示例3: getMetadata
public static MediaMetadataCompat getMetadata(String mediaId) {
MediaMetadataCompat metadataWithoutBitmap = music.get(mediaId);
Bitmap albumArt = getAlbumBitmap(mediaId);
// Since MediaMetadataCompat is immutable, we need to create a copy to set the album art.
// We don't set it initially on all items so that they don't take unnecessary memory.
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
for (String key :
new String[]{
MediaMetadataCompat.METADATA_KEY_MEDIA_ID,
MediaMetadataCompat.METADATA_KEY_ALBUM,
MediaMetadataCompat.METADATA_KEY_ARTIST,
MediaMetadataCompat.METADATA_KEY_GENRE,
MediaMetadataCompat.METADATA_KEY_TITLE
}) {
builder.putString(key, metadataWithoutBitmap.getString(key));
}
builder.putLong(
MediaMetadataCompat.METADATA_KEY_DURATION,
metadataWithoutBitmap.getLong(MediaMetadataCompat.METADATA_KEY_DURATION));
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt);
return builder.build();
}
示例4: updateMetadata
private void updateMetadata(final Video video) {
final MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, video.id + "");
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, video.title);
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, video.studio);
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION,
video.description);
/*
long duration = Utils.getDuration(video.videoUrl);
metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
*/
long duration = getDuration();
metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
// And at minimum the title and artist for legacy support
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, video.title);
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, video.studio);
Resources res = getResources();
int cardWidth = res.getDimensionPixelSize(R.dimen.playback_overlay_width);
int cardHeight = res.getDimensionPixelSize(R.dimen.playback_overlay_height);
Glide.with(this)
.load(Uri.parse(video.cardImageUrl))
.asBitmap()
.centerCrop()
.into(new SimpleTarget<Bitmap>(cardWidth, cardHeight) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
metadataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap);
mSession.setMetadata(metadataBuilder.build());
}
});
}
示例5: updateMetadata
private void updateMetadata(final Clip clip) {
final MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, clip.getTitle());
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
clip.getDescription());
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI,
clip.getCardImageUrl());
// And at minimum the title and artist for legacy support
metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, clip.getTitle());
metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, clip.getDescription());
Glide.with(this)
.asBitmap()
.load(Uri.parse(clip.getCardImageUrl()))
.into(new SimpleTarget<Bitmap>(500, 500) {
@Override
public void onResourceReady(Bitmap bitmap, Transition transition) {
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap);
mSession.setMetadata(metadataBuilder.build());
}
@Override
public void onLoadFailed(Drawable errorDrawable) {
Log.e(TAG, "onLoadFailed: " + errorDrawable);
mSession.setMetadata(metadataBuilder.build());
}
});
}
示例6: mapToMetadata
/**
* Converts a {@link MediaDescriptionCompat} into {@link MediaMetadataCompat}. Only a title,
* description, and media id will be converted. A subclass should override this method if their
* {@link #map(Object)} method stores more data in the mapped MediaDescriptionCompat.
*
* @param item to be converted.
* @return A converted MediaMetadataCompact from MediaDescriptionCompat.
*/
@NonNull
public MediaMetadataCompat mapToMetadata(MediaDescriptionCompat item) {
MediaMetadataCompat.Builder builder =
new MediaMetadataCompat.Builder()
.putText(MediaMetadataCompat.METADATA_KEY_TITLE, item.getTitle())
.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, item.getTitle())
.putText(
MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION,
item.getDescription())
.putBitmap(
MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, item.getIconBitmap())
.putText(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, item.getMediaId());
if (item.getIconUri() != null) {
builder.putString(
MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI,
item.getIconUri().toString());
}
Bundle extras = item.getExtras();
if (extras != null) {
String backgroundArtUri = extras.getString(EXTRA_BACKGROUND_ART);
if (backgroundArtUri != null) {
builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, backgroundArtUri);
}
}
return builder.build();
}
示例7: updateMediaSessionMetaData
private void updateMediaSessionMetaData() {
final Song song = getCurrentSong();
if (song.id == -1) {
mediaSession.setMetadata(null);
return;
}
final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
}
if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
final Point screenSize = Util.getScreenSize(MusicService.this);
final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
.checkIgnoreMediaStore(MusicService.this)
.asBitmap().build();
if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
request.transform(new BlurTransformation.Builder(MusicService.this).build());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
mediaSession.setMetadata(metaData.build());
}
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
mediaSession.setMetadata(metaData.build());
}
});
}
});
} else {
mediaSession.setMetadata(metaData.build());
}
}
示例8: playFromUri
public void playFromUri(Uri uri, Bundle bundle) {
mPlayOnFocusGain = true;
tryToGetAudioFocus();
registerAudioNoisyReceiver();
mState = PlaybackStateCompat.STATE_STOPPED;
relaxResources(true);
try {
createMediaPlayerIfNeeded();
mState = PlaybackStateCompat.STATE_BUFFERING;
mCurrentPosition = 0;
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(uri.toString());
// Starts preparing the media player in the background. When
// it's done, it will call our OnPreparedListener (that is,
// the onPrepared() method on this class, since we set the
// listener to 'this'). Until the media player is prepared,
// we *cannot* call start() on it!
mMediaPlayer.prepareAsync();
// If we are streaming from the internet, we want to hold a
// Wifi lock, which prevents the Wifi radio from going to
// sleep while the song is playing.
mWifiLock.acquire();
if (mCallback != null) {
mCallback.onPlaybackStateChanged(mState);
MediaMetadataCompat.Builder metaBuilder = new MediaMetadataCompat.Builder();
metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, bundle.getString(MediaMetadataCompat.METADATA_KEY_ARTIST));
metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, bundle.getString(MediaMetadataCompat.METADATA_KEY_TITLE));
metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, bundle.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI));
mCallback.onMediaMetadataChanged(metaBuilder.build());
}
} catch (IOException ex) {
Log.e(TAG, ex + "Exception playing song");
if (mCallback != null) {
mCallback.onError(ex.getMessage());
}
}
}
示例9: setBitmapForLockScreen
private void setBitmapForLockScreen(MediaInfo video) {
if (video == null || mMediaSessionCompat == null) {
return;
}
Uri imgUrl = null;
Bitmap bm = null;
List<WebImage> images = video.getMetadata().getImages();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (images.size() > 1) {
imgUrl = images.get(1).getUrl();
} else if (images.size() == 1) {
imgUrl = images.get(0).getUrl();
} else if (mContext != null) {
// we don't have a url for image so get a placeholder image from resources
bm = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.album_art_placeholder_large);
}
} else if (!images.isEmpty()) {
imgUrl = images.get(0).getUrl();
} else {
// we don't have a url for image so get a placeholder image from resources
bm = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.album_art_placeholder);
}
if (bm != null) {
MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
MediaMetadataCompat.Builder newBuilder = currentMetadata == null
? new MediaMetadataCompat.Builder()
: new MediaMetadataCompat.Builder(currentMetadata);
mMediaSessionCompat.setMetadata(newBuilder
.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bm)
.build());
} else {
if (mLockScreenFetchTask != null) {
mLockScreenFetchTask.cancel(true);
}
Point screenSize = Utils.getDisplaySize(mContext);
mLockScreenFetchTask = new FetchBitmapTask(screenSize.x, screenSize.y, false) {
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && mMediaSessionCompat != null) {
MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController()
.getMetadata();
MediaMetadataCompat.Builder newBuilder = currentMetadata == null
? new MediaMetadataCompat.Builder()
: new MediaMetadataCompat.Builder(currentMetadata);
mMediaSessionCompat.setMetadata(newBuilder
.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
.build());
}
mLockScreenFetchTask = null;
}
};
mLockScreenFetchTask.execute(imgUrl);
}
}
示例10: notifyAyahChanged
private void notifyAyahChanged() {
if (mAudioRequest != null) {
Intent updateIntent = new Intent(AudioUpdateIntent.INTENT_NAME);
updateIntent.putExtra(AudioUpdateIntent.STATUS, AudioUpdateIntent.PLAYING);
updateIntent.putExtra(AudioUpdateIntent.SURA, mAudioRequest.getCurrentSura());
updateIntent.putExtra(AudioUpdateIntent.AYAH, mAudioRequest.getCurrentAyah());
mBroadcastManager.sendBroadcast(updateIntent);
MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mAudioRequest.getTitle(this));
if (mPlayer.isPlaying()) {
metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, mPlayer.getDuration());
}
if (mDisplayIcon != null) {
metadataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, mDisplayIcon);
}
mMediaSession.setMetadata(metadataBuilder.build());
}
}
示例11: updateArt
private void updateArt(MediaMetadataCompat metadataCompat){
if(metadataCompat==null) return;
String text=Long.toString(metadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER))
+" of "
+Long.toString(metadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS));
trackName.setText(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE));
artist.setText(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_ARTIST));
pages.setText(text);
String image=metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI);
showArt(image);
}
示例12: isMediaIdSame
private boolean isMediaIdSame(MediaMetadataCompat currentMedia,
MediaMetadataCompat newMedia) {
if (currentMedia == null || newMedia == null) {
return false;
}
String newMediaId =
newMedia.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
String currentMediaId =
currentMedia.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
return newMediaId.equals(currentMediaId);
}
示例13: play
@Override
public void play(MediaSessionCompat.QueueItem item) {
mPlayOnFocusGain = true;
getAudioFocus();
registerNoisyReceiver();
String mediaId = item.getDescription().getMediaId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentPosition = 0;
mCurrentMediaId = mediaId;
}
if (mPlaybackState == PlaybackStateCompat.STATE_PAUSED &&
!mediaHasChanged && mPlayer != null) {
configMediaPlayerState();
} else {
mPlaybackState = PlaybackStateCompat.STATE_STOPPED;
releaseResource(false);
MediaMetadataCompat track = mProvider.getMusic(mediaId);
String source = track.getString(MusicProviderSource.CUSTOM_METADATA_MUSIC_FILE_PATH);
try {
createOrResetMediaPlayer();
mPlaybackState = PlaybackStateCompat.STATE_BUFFERING;
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(source);
mPlayer.prepareAsync();
if (mCallback != null) {
mCallback.onPlayStateChanged(mPlaybackState);
}
} catch (IOException e) {
if (mCallback != null) {
mCallback.onError(PlaybackStateCompat.ERROR_CODE_NOT_SUPPORTED, e.getMessage());
}
e.printStackTrace();
}
}
}