本文整理汇总了Java中android.support.v4.media.session.MediaSessionCompat.setCallback方法的典型用法代码示例。如果您正苦于以下问题:Java MediaSessionCompat.setCallback方法的具体用法?Java MediaSessionCompat.setCallback怎么用?Java MediaSessionCompat.setCallback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.support.v4.media.session.MediaSessionCompat
的用法示例。
在下文中一共展示了MediaSessionCompat.setCallback方法的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: 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());
}
示例6: 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;
}
示例7: 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");
}
示例8: 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);
}
示例9: 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();
}
示例10: 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));
}
示例11: 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);
}
}
示例12: 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);
}
示例13: 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);
}
示例14: createMediaSession
import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private MediaSessionCompat createMediaSession(Context context) {
// create a media session
MediaSessionCompat session = new MediaSessionCompat(context, LOG_TAG);
session.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
session.setPlaybackState(getPlaybackState());
session.setCallback(new MediaSessionCallback());
if (mStation != null) {
session.setMetadata(getMetadata(context, mStation, null));
}
setSessionToken(session.getSessionToken());
return session;
}
示例15: onCreate
import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
@Override
public void onCreate() {
Timber.i("debug: Creating service");
mHandler = new ServiceHandler(this);
final Context appContext = getApplicationContext();
mWifiLock = ((WifiManager) appContext.getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "QuranAudioLock");
mNotificationManager = (NotificationManager) appContext.getSystemService(NOTIFICATION_SERVICE);
// create the Audio Focus Helper, if the Audio Focus feature is available
mAudioFocusHelper = new AudioFocusHelper(appContext, this);
mBroadcastManager = LocalBroadcastManager.getInstance(appContext);
ComponentName receiver = new ComponentName(this, MediaButtonReceiver.class);
mMediaSession = new MediaSessionCompat(appContext, "QuranMediaSession", receiver, null);
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setCallback(new MediaSessionCallback());
mNotificationColor = ContextCompat.getColor(this, R.color.audio_notification_color);
try {
// for Android Wear, use a 1x1 Bitmap with the notification color
mDisplayIcon = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDisplayIcon);
canvas.drawColor(mNotificationColor);
} catch (OutOfMemoryError oom) {
Crashlytics.logException(oom);
}
}