本文整理汇总了Java中android.support.v4.media.session.MediaButtonReceiver类的典型用法代码示例。如果您正苦于以下问题:Java MediaButtonReceiver类的具体用法?Java MediaButtonReceiver怎么用?Java MediaButtonReceiver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MediaButtonReceiver类属于android.support.v4.media.session包,在下文中一共展示了MediaButtonReceiver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的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();
}
示例2: from
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的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;
}
示例3: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
if (startIntent != null) {
String action = startIntent.getAction();
String command = startIntent.getStringExtra(CMD_NAME);
if (ACTION_CMD.equals(action)) {
if (CMD_PAUSE.equals(command)) {
mMediaController.getTransportControls().pause();
}
} else {
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mMediaSession, startIntent);
}
}
return START_STICKY;
}
示例4: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
if (startIntent != null) {
String action = startIntent.getAction();
String command = startIntent.getStringExtra(CMD_NAME);
if (ACTION_CMD.equals(action)) {
if (CMD_PAUSE.equals(command)) {
mPlaybackManager.handlePauseRequest();
} else if (CMD_STOP_CASTING.equals(command)) {
VideoCastManager.getInstance().disconnect();
}
} else {
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
}
// Reset the delay handler to enqueue a message to stop the service if
// nothing is playing.
mDelayedStopHandler.removeCallbacksAndMessages(null);
mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY);
return START_STICKY;
}
示例5: doOpenSession
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的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));
}
示例6: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
/**
* (non-Javadoc)
* @see android.app.Service#onStartCommand(android.content.Intent, int, int)
*/
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
if (startIntent != null) {
String action = startIntent.getAction();
String command = startIntent.getStringExtra(CMD_NAME);
if (ACTION_CMD.equals(action)) {
if (CMD_PAUSE.equals(command)) {
mPlaybackManager.handlePauseRequest();
} else if (CMD_STOP_CASTING.equals(command)) {
CastContext.getSharedInstance(this).getSessionManager().endCurrentSession(true);
}
} else {
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
}
// Reset the delay handler to enqueue a message to stop the service if
// nothing is playing.
mDelayedStopHandler.removeCallbacksAndMessages(null);
mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY);
return START_STICKY;
}
示例7: initMediaSession
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的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);
}
}
示例8: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timber.i("onStartCommand called");
super.onStartCommand(intent, flags, startId);
if (intent != null && MediaStoreUtil.hasPermission(this)) {
mBeQuiet = intent.getBooleanExtra(EXTRA_START_SILENT, false);
if (intent.hasExtra(Intent.EXTRA_KEY_EVENT)) {
MediaButtonReceiver.handleIntent(musicPlayer.getMediaSession(), intent);
Timber.i(intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT).toString());
} else if (ACTION_STOP.equals(intent.getAction())) {
stop();
}
}
return START_STICKY;
}
示例9: notificationBuilder
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
private NotificationCompat.Builder notificationBuilder(PlayerService context, MediaSessionCompat session) {
MediaControllerCompat controller = session.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(mediaMetadata.getBitmap(MediaMetadataCompat.METADATA_KEY_ART))
.setContentIntent(clickIntent)
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(context, receiverName, PlaybackStateCompat.ACTION_STOP))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
return builder;
}
示例10: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(L) Log.i(LOG_TAG, "Starting service");
if (intent != null && intent.getAction() != null) {
switch (intent.getAction()) {
case ACTION_PLAY:
if(L) Log.i(LOG_TAG, "Calling play");
mediaController.getTransportControls().play();
break;
case ACTION_STOP:
if(L) Log.i(LOG_TAG, "Calling stop");
mediaController.getTransportControls().stop();
break;
}
}
// receives and handles all media button key events and forwards
// to the media session which deals with them in its callback methods
MediaButtonReceiver.handleIntent(mediaSession, intent);
return super.onStartCommand(intent, flags, startId);
}
示例11: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case ACTION_STOP:
stop();
break;
case ACTION_PAUSE:
pause();
break;
case ACTION_RESUME:
resume();
break;
}
}
MediaButtonReceiver.handleIntent(mediaSession, intent);
}
return super.onStartCommand(intent, flags, startId);
}
示例12: onCreate
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的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);
}
}
示例13: createStopNotification
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
public static void createStopNotification(MediaSessionCompat mediaSession, Service context, Class<?> serviceClass, int NOTIFICATION_ID) {
PendingIntent stopIntent = PendingIntent
.getService(context, 0, getIntent(MediaRecorderService.REQUEST_TYPE_STOP, context, serviceClass),
PendingIntent.FLAG_CANCEL_CURRENT);
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
// Start foreground service to avoid unexpected kill
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setDeleteIntent(stopIntent)
// Add a pause button
.addAction(new android.support.v7.app.NotificationCompat.Action(
R.drawable.ic_stop_black_24dp, context.getString(R.string.stop),
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP)))
.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0)
.setShowCancelButton(true)
.setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP)))
.setSmallIcon(R.drawable.ic_album_black_24dp)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
context.startForeground(NOTIFICATION_ID, notification);
}
示例14: onStartCommand
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
castSession = castSessionManager.getCurrentCastSession();
if (sessionManagerListener == null) {
sessionManagerListener = new SessionManagerListenerImpl();
castSessionManager.addSessionManagerListener(sessionManagerListener);
}
String requestType = intent.getStringExtra(MediaRecorderService.REQUEST_TYPE);
Log.d(TAG, "onStartCommand received request: " + requestType);
if (requestType != null && requestType.equals(MediaRecorderService.REQUEST_TYPE_START)) {
Log.i(TAG, "Started service");
// After binding with activity, will hear setActivity() to start recording
}
else if (requestType != null && requestType.equals(MediaRecorderService.REQUEST_TYPE_STOP)) {
stop();
}
else {
MediaButtonReceiver.handleIntent(mediaSession, intent);
}
return START_STICKY;
}
示例15: getNotification
import android.support.v4.media.session.MediaButtonReceiver; //导入依赖的package包/类
private Notification getNotification(int playbackState) {
NotificationCompat.Builder builder = MediaStyleHelper.from(this, mediaSession);
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, getString(R.string.previous), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
if (playbackState == PlaybackStateCompat.STATE_PLAYING)
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
else
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, getString(R.string.play), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, getString(R.string.next), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));
builder.setStyle(new MediaStyle()
.setShowActionsInCompactView(1)
.setShowCancelButton(true)
.setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_STOP))
.setMediaSession(mediaSession.getSessionToken())); // setMediaSession требуется для Android Wear
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); // The whole background (in MediaStyle), not just icon background
builder.setShowWhen(false);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setOnlyAlertOnce(true);
builder.setChannelId(NOTIFICATION_DEFAULT_CHANNEL_ID);
return builder.build();
}