本文整理汇总了Java中android.support.v4.media.session.MediaSessionCompat类的典型用法代码示例。如果您正苦于以下问题:Java MediaSessionCompat类的具体用法?Java MediaSessionCompat怎么用?Java MediaSessionCompat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MediaSessionCompat类属于android.support.v4.media.session包,在下文中一共展示了MediaSessionCompat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildNotification
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
public void buildNotification(Context context, final String albumName, final String artistName,
final String trackName, final Long albumId, final Bitmap albumArt,
final boolean isPlaying, MediaSessionCompat.Token mediaSessionToken) {
if (Utils.hasOreo()){
mNotificationManager.createNotificationChannel(AppNotificationChannels.getAudioChannel(context));
}
// Notification Builder
mNotificationBuilder = new NotificationCompat.Builder(mService, AppNotificationChannels.AUDIO_CHANNEL_ID)
.setShowWhen(false)
.setSmallIcon(R.drawable.itunes)
.setContentTitle(artistName)
.setContentText(trackName)
.setContentIntent(getOpenIntent(context))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.cover))
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new MediaStyle()
.setMediaSession(mediaSessionToken)
.setShowCancelButton(true)
.setShowActionsInCompactView(0, 1, 2)
.setCancelButtonIntent(retreivePlaybackActions(4)))
.addAction(new android.support.v4.app.NotificationCompat.Action(R.drawable.page_first, ""
, retreivePlaybackActions(3)))
.addAction(new android.support.v4.app.NotificationCompat.Action(isPlaying ? R.drawable.pause : R.drawable.play, ""
, retreivePlaybackActions(1)))
.addAction(new android.support.v4.app.NotificationCompat.Action(R.drawable.page_last, ""
, retreivePlaybackActions(2)));
mService.startForeground(APOLLO_MUSIC_SERVICE, mNotificationBuilder.build());
}
示例2: setUpRemoteControlClient
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
private void setUpRemoteControlClient() {
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mMediaButtonReceiverComponent);
mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
mMediaSession = new MediaSessionCompat(getApplication(), "TAG", mMediaButtonReceiverComponent, null);
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_SEEK_TO |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_STOP
)
.setState(isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED, position(), 1.0f)
.build();
mMediaSession.setPlaybackState(playbackStateCompat);
mMediaSession.setCallback(mMediaSessionCallback);
mMediaSession.setActive(true);
updateRemoteControlClient(META_CHANGED);
mTransportController = mMediaSession.getController().getTransportControls();
}
示例3: initializeMediaSession
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
private void initializeMediaSession() {
mMediaSession = new MediaSessionCompat(getContext(), TAG);
mMediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setMediaButtonReceiver(null);
mStateBuilder = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_PLAY_PAUSE);
mMediaSession.setPlaybackState(mStateBuilder.build());
mMediaSession.setCallback(new MySessionCallback());
mMediaSession.setActive(true);
}
示例4: onCreate
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.setAcceptsDelayedFocusGain(false)
.setWillPauseWhenDucked(true)
.setAudioAttributes(audioAttributes)
.build();
}
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaSession = new MediaSessionCompat(this, "PlayerService");
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(mediaSessionCallback);
Context appContext = getApplicationContext();
Intent activityIntent = new Intent(appContext, MainActivity.class);
mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0));
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class);
mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0));
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
exoPlayer.addListener(exoPlayerListener);
DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)), null);
Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max
this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
this.extractorsFactory = new DefaultExtractorsFactory();
}
示例5: initializeMediaSession
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
private void initializeMediaSession() {
mSession = new MediaSessionCompat(this, TAG);
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setActive(true);
MediaControllerCompat.setMediaController(this, mSession.getController());
MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle())
.build();
mSession.setMetadata(metadata);
MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView);
mSession.setCallback(mMediaSessionCallback);
int state =
mMovieView.isPlaying()
? PlaybackStateCompat.STATE_PLAYING
: PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState(
state,
MEDIA_ACTIONS_ALL,
mMovieView.getCurrentPosition(),
mMovieView.getVideoResourceId());
}
示例6: onCreate
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
castSessionManager = CastContext.getSharedInstance(this).getSessionManager();
// Create a MediaSessionCompat
mediaSession = new MediaSessionCompat(this, TAG);
// Enable callbacks from MediaButtons and TransportControls
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
stateBuilder = new PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
// MySessionCallback() has methods that handle callbacks from a media controller
mediaSession.setCallback(mediaSessionCallback);
// Set the session's token so that client activities can communicate with it.
setSessionToken(mediaSession.getSessionToken());
}
示例7: from
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
/**
* Build a notification using the information from the given media session. Makes heavy use
* of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
*
* @param context Context used to construct the notification.
* @param mediaSession Media session to get information.
* @return A pre-built notification with information from the given media session.
*/
static NotificationCompat.Builder from(
Context context, MediaSessionCompat mediaSession) {
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setContentIntent(controller.getSessionActivity())
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
return builder;
}
示例8: onSkipToNext
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onSkipToNext() {
// Update the media to skip to the next video.
Bundle bundle = new Bundle();
bundle.putBoolean(AUTO_PLAY, true);
int nextIndex = ++mQueueIndex;
if (nextIndex < mQueue.size()) {
MediaSessionCompat.QueueItem item = mQueue.get(nextIndex);
String mediaId = item.getDescription().getMediaId();
getActivity().getMediaController()
.getTransportControls()
.playFromMediaId(mediaId, bundle);
} else {
getActivity().onBackPressed(); // Return to details presenter.
}
}
示例9: onSkipToPrevious
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onSkipToPrevious() {
// Update the media to skip to the previous video.
setPlaybackState(PlaybackState.STATE_SKIPPING_TO_PREVIOUS);
Bundle bundle = new Bundle();
bundle.putBoolean(AUTO_PLAY, true);
int prevIndex = --mQueueIndex;
if (prevIndex >= 0) {
MediaSessionCompat.QueueItem item = mQueue.get(prevIndex);
String mediaId = item.getDescription().getMediaId();
getActivity().getMediaController()
.getTransportControls()
.playFromMediaId(mediaId, bundle);
} else {
getActivity().onBackPressed(); // Return to details presenter.
}
}
示例10: createMediaSession
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
private MediaSessionCompat createMediaSession() {
MediaSessionCompat mediaSession = new MediaSessionCompat(
mContext,
mContext.getString(R.string.app_name),
new ComponentName(mContext.getPackageName(),
getButtonReceiverClassName()),
null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(mMediaSessionCallback);
// TODO(mlamouri): the following code is to work around a bug that hopefully
// MediaSessionCompat will handle directly. see b/24051980.
try {
mediaSession.setActive(true);
} catch (NullPointerException e) {
// Some versions of KitKat do not support AudioManager.registerMediaButtonIntent
// with a PendingIntent. They will throw a NullPointerException, in which case
// they should be able to activate a MediaSessionCompat with only transport
// controls.
mediaSession.setActive(false);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
}
return mediaSession;
}
示例11: handleMessage
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
public void handleMessage(Message msg) {
if (this.mCallbacksMessengerRef != null) {
Bundle data = msg.getData();
data.setClassLoader(MediaSessionCompat.class.getClassLoader());
switch (msg.what) {
case 1:
this.mCallbackImpl.onServiceConnected((Messenger) this.mCallbacksMessengerRef.get(), data.getString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID), (Token) data.getParcelable(MediaBrowserProtocol.DATA_MEDIA_SESSION_TOKEN), data.getBundle(MediaBrowserProtocol.DATA_ROOT_HINTS));
return;
case 2:
this.mCallbackImpl.onConnectionFailed((Messenger) this.mCallbacksMessengerRef.get());
return;
case 3:
this.mCallbackImpl.onLoadChildren((Messenger) this.mCallbacksMessengerRef.get(), data.getString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID), data.getParcelableArrayList(MediaBrowserProtocol.DATA_MEDIA_ITEM_LIST), data.getBundle(MediaBrowserProtocol.DATA_OPTIONS));
return;
default:
Log.w(MediaBrowserCompat.TAG, "Unhandled message: " + msg + "\n Client version: " + 1 + "\n Service version: " + msg.arg1);
return;
}
}
}
示例12: onCreate
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
playbackManager.setServiceCallback(this);
playbackManager.setUpdateListener(this);
mediaSession=new MediaSessionCompat(getApplicationContext(),TAG);
mediaSession.setCallback(playbackManager.getMediaSessionCallback());
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
setSessionToken(mediaSession.getSessionToken());
Context context = getApplicationContext();
Intent intent = new Intent(context, TrackActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 99,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSession.setSessionActivity(pi);
notification=new TrackNotification(this);
playbackManager.updatePlaybackState(PlaybackStateCompat.STATE_NONE);
}
示例13: onCreate
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
// Create a new MediaSession.
mSession = new MediaSessionCompat(this, "MusicService");
mCallback = new MediaSessionCallback();
mSession.setCallback(mCallback);
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
setSessionToken(mSession.getSessionToken());
mMediaNotificationManager = new MediaNotificationManager(this);
mPlayback = new MediaPlayerAdapter(this, new MediaPlayerListener());
Log.d(TAG, "onCreate: MusicService creating MediaSession, and MediaNotificationManager");
}
示例14: setupMediaSession
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
private void setupMediaSession() {
ComponentName mediaButtonReceiverComponentName = new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mediaButtonReceiverComponentName);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
mediaSession = new MediaSessionCompat(this, "RetroMusicPlayer", mediaButtonReceiverComponentName, mediaButtonReceiverPendingIntent);
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onPlay() {
play();
}
@Override
public void onPause() {
pause();
}
@Override
public void onSkipToNext() {
playNextSong(true);
}
@Override
public void onSkipToPrevious() {
back(true);
}
@Override
public void onStop() {
quit();
}
@Override
public void onSeekTo(long pos) {
seek((int) pos);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
return MediaButtonIntentReceiver.handleIntent(MusicService.this, mediaButtonEvent);
}
});
mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
| MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
示例15: update
import android.support.v4.media.session.MediaSessionCompat; //导入依赖的package包/类
public static void update(Station station, int stationID, String stationMetadata, MediaSessionCompat session) {
// session can be null on update
if (session != null) {
mSession = session;
}
// metadata can be null on update
if (stationMetadata != null) {
mStationMetadata = stationMetadata;
}
// build notification
mNotification = getNotificationBuilder(station, stationID, mStationMetadata).build();
// display updated notification
NotificationManager notificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(PLAYER_SERVICE_NOTIFICATION_ID, mNotification);
if (!station.getPlaybackState()) {
// make notification swipe-able
mService.stopForeground(false);
}
}