本文整理匯總了Java中android.support.v4.media.session.MediaSessionCompat.setFlags方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaSessionCompat.setFlags方法的具體用法?Java MediaSessionCompat.setFlags怎麽用?Java MediaSessionCompat.setFlags使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.support.v4.media.session.MediaSessionCompat
的用法示例。
在下文中一共展示了MediaSessionCompat.setFlags方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
示例2: 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();
}
示例3: createMediaSession
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
private MediaSessionCompat createMediaSession() {
Context context = getContext();
MediaSessionCompat mediaSession =
new MediaSessionCompat(context, context.getString(R.string.app_name),
new ComponentName(context, getButtonReceiverClass()), 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;
}
示例4: 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());
}
示例5: 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);
}
示例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: 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);
}
示例8: 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");
}
示例9: 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;
}
示例10: onCreate
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
mPlayPauseButton = (ImageButton) findViewById(R.id.videoPlayPauseButton);
mSurfaceView = (SurfaceView) findViewById(R.id.videoSurfaceView);
Intent callingIntent = this.getIntent();
if(callingIntent != null) {
mVideoUri = callingIntent.getData();
}
mSession = new MediaSessionCompat(this, TAG);
mSession.setCallback(new MediaSessionCallback(this));
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mPBuilder = new PlaybackStateCompat.Builder();
mController = new MediaControllerCompat(this, mSession);
mControllerTransportControls = mController.getTransportControls();
}
示例11: doOpenSession
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
private void doOpenSession() {
final ComponentName mediaButtonReceiver = new ComponentName(mContext,
MediaButtonReceiver.class);
final PendingIntent broadcastIntent = PendingIntent
.getBroadcast(mContext, 1, new Intent(mContext, MediaButtonReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT);
final MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, TAG,
mediaButtonReceiver, broadcastIntent);
mediaSession.setCallback(new MediaSessionCallback(mContext));
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
mediaSession.setSessionActivity(PendingIntent.getActivity(mContext, 1,
new Intent(mContext, NowPlayingActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
mMediaSession = mediaSession;
Handlers.runOnIoThread(() -> reportMediaAndState(mediaSession));
}
示例12: initMediaSession
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
void initMediaSession() {
// NOTE: all this is so that when you press pause/play in the app, we can capture the
// media control event, so that other apps DON'T (ie, google play music, plex, etc).
// ideally we could do something useful with this, but for not, just eat it.
try {
ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
mediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "SAGETVMINICLIENT", mediaButtonReceiver, null);
mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
log.debug("Audio Session Callback Handler: Command {}", command);
}
});
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
mediaSessionCompat.setMediaButtonReceiver(pendingIntent);
mediaSessionCompat.setActive(true);
log.debug("Media Session is setup to capture pause/play. session: "+mediaSessionCompat.getSessionToken());
} catch (Throwable t) {
log.error("Failed to capture the media session", t);
}
}
示例13: MediaSessionWrapper
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
/**
* Wrapper used to encapsulate {@link android.support.v4.media.session.MediaSessionCompat} behaviour
* as well as a remote control client for lock screen on pre Lollipop.
*
* @param context holding context.
* @param callback callback used to catch media session or lock screen events.
* @param audioManager audio manager used to request the focus.
*/
public MediaSessionWrapper(Context context, MediaSessionWrapperCallback callback, AudioManager audioManager) {
mContext = context;
mCallback = callback;
mAudioManager = audioManager;
mRuntimePackageName = context.getPackageName();
initLockScreenRemoteControlClient(context);
initPlaybackStateBuilder();
mMediaSession = new MediaSessionCompat(context, TAG);
mMediaSession.setCallback(new MediaSessionCallback());
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}
示例14: MediaSessionConnector
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
/**
* Creates an instance. Must be called on the same thread that is used to construct the player
* instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}.
*
* @param mediaSession The {@link MediaSessionCompat} to connect to.
* @param playbackController A {@link PlaybackController} for handling playback actions, or {@code
* null} if the connector should handle playback actions directly.
* @param doMaintainMetadata Whether the connector should maintain the metadata of the session. If
* {@code false}, you need to maintain the metadata of the media session yourself (provide at
* least the duration to allow clients to show a progress bar).
* @param metadataExtrasPrefix A string to prefix extra keys which are propagated from the active
* queue item to the session metadata.
*/
public MediaSessionConnector(
MediaSessionCompat mediaSession,
PlaybackController playbackController,
boolean doMaintainMetadata,
@Nullable String metadataExtrasPrefix) {
this.mediaSession = mediaSession;
this.playbackController = playbackController != null ? playbackController
: new DefaultPlaybackController();
this.metadataExtrasPrefix = metadataExtrasPrefix != null ? metadataExtrasPrefix : "";
this.handler = new Handler(Looper.myLooper() != null ? Looper.myLooper()
: Looper.getMainLooper());
this.doMaintainMetadata = doMaintainMetadata;
mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS);
mediaController = mediaSession.getController();
mediaSessionCallback = new MediaSessionCallback();
exoPlayerEventListener = new ExoPlayerEventListener();
customActionMap = Collections.emptyMap();
commandMap = new HashMap<>();
registerCommandReceiver(playbackController);
}
示例15: onCreate
import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
@Override
public void onCreate() {
super.onCreate();
mSession = new MediaSessionCompat(this, "session tag");
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new CustomMediaSession(getApplicationContext(), new CustomMediaSession.Callback() {
@Override
public void onPlaybackStarted(JsonChannel channel) {
showNotification(channel);
}
@Override
public void onPlaybackEnded() {
stopForeground(true);
removeNotification();
}
}));
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}