Java Code Examples for android.content.Intent#ACTION_MEDIA_BUTTON
The following examples show how to use
android.content.Intent#ACTION_MEDIA_BUTTON .
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: a File: ReadAloudService.java License: GNU General Public License v3.0 | 6 votes |
/** * 初始化MediaSession */ private void initMediaSession() { ComponentName mComponent = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mComponent); PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT); mediaSessionCompat = new MediaSessionCompat(this, TAG, mComponent, mediaButtonReceiverPendingIntent); mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() { @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { return MediaButtonIntentReceiver.handleIntent(ReadAloudService.this, mediaButtonEvent); } }); mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent); }
Example 2
Source Project: AcDisplay File: MediaController2.java License: GNU General Public License v2.0 | 6 votes |
/** * Emulates hardware buttons' click via broadcast system. * * @see android.view.KeyEvent */ public static void broadcastMediaAction(@NonNull Context context, int action) { int keyCode; switch (action) { case ACTION_PLAY_PAUSE: keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE; break; case ACTION_STOP: keyCode = KeyEvent.KEYCODE_MEDIA_STOP; break; case ACTION_SKIP_TO_NEXT: keyCode = KeyEvent.KEYCODE_MEDIA_NEXT; break; case ACTION_SKIP_TO_PREVIOUS: keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; break; default: throw new IllegalArgumentException(); } Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode); context.sendOrderedBroadcast(intent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown), null); context.sendOrderedBroadcast(intent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp), null); }
Example 3
Source Project: HaoReader File: ReadAloudService.java License: GNU General Public License v3.0 | 6 votes |
/** * 初始化MediaSession */ private void initMediaSession() { ComponentName mComponent = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mComponent); PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT); mediaSessionCompat = new MediaSessionCompat(this, TAG, mComponent, mediaButtonReceiverPendingIntent); mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() { @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { return MediaButtonIntentReceiver.handleIntent(mediaButtonEvent); } }); mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent); }
Example 4
Source Project: Noyze File: AudioHelper.java License: Apache License 2.0 | 6 votes |
/** * android.media.IAudioService#dispatchMediaKeyEvent(KeyEvent), except if this * method fails for any reason, fall back on broadcasting an event. */ public boolean dispatchMediaKeyEvent(Context mContext, int keyCode) { // We'll try the public API for API 19+, then the reflected API, then // finally we'll resort to broadcasting the action ourselves! if (isHTC(mContext) || !_dispatchMediaKeyEvent(mManager, keyCode)) { long eventtime = SystemClock.uptimeMillis(); KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0); KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP); Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown); mContext.sendOrderedBroadcast(keyIntent, null); keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp); mContext.sendOrderedBroadcast(keyIntent, null); return false; } return true; }
Example 5
Source Project: VCL-Android File: PlaybackService.java License: Apache License 2.0 | 6 votes |
/** * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void changeRemoteControlClient(AudioManager am, boolean acquire) { if (acquire) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); // create and register the remote control client mRemoteControlClient = new RemoteControlClient(mediaPendingIntent); am.registerRemoteControlClient(mRemoteControlClient); mRemoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP); } else { am.unregisterRemoteControlClient(mRemoteControlClient); mRemoteControlClient = null; } }
Example 6
Source Project: Popeens-DSub File: RemoteControlClientICS.java License: GNU General Public License v3.0 | 6 votes |
public void register(final Context context, final ComponentName mediaButtonReceiverComponent) { downloadService = (DownloadService) context; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // build the PendingIntent for the remote control client Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0); // create and register the remote control client mRemoteControl = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(mRemoteControl); mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); mRemoteControl.setTransportControlFlags(getTransportFlags()); imageLoader = SubsonicActivity.getStaticImageLoader(context); }
Example 7
Source Project: IdealMedia File: PlayerService.java License: Apache License 2.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) void registerRemoteControl(ComponentName rcvMedia) { mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(rcvMedia); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); remoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS ); mAudioManager.registerRemoteControlClient(remoteControlClient); }
Example 8
Source Project: android File: VideoCastManager.java License: Apache License 2.0 | 5 votes |
@SuppressLint("InlinedApi") private void setUpRemoteControl(final MediaInfo info) { if (!isFeatureEnabled(BaseCastManager.FEATURE_LOCKSCREEN)) { return; } LOGD(TAG, "setupRemoteControl() was called"); mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); ComponentName eventReceiver = new ComponentName( mContext, VideoIntentReceiver.class.getName()); mAudioManager.registerMediaButtonEventReceiver(eventReceiver); if (mRemoteControlClientCompat == null) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat( PendingIntent.getBroadcast(mContext, 0, intent, 0)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); } mRemoteControlClientCompat.addToMediaRouter(mMediaRouter); mRemoteControlClientCompat.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE); if (null == info) { mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); return; } else { mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } // Update the remote control's image updateLockScreenImage(info); // update the remote control's metadata updateLockScreenMetadata(); }
Example 9
Source Project: Cheerleader File: MediaSessionWrapper.java License: Apache License 2.0 | 5 votes |
/** * Initialize the remote control client on the lock screen. * * @param context holding context. */ @SuppressWarnings("deprecation") private void initLockScreenRemoteControlClient(Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { mMediaButtonReceiverComponent = new ComponentName( mRuntimePackageName, MediaSessionReceiver.class.getName()); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent); if (mRemoteControlClientCompat == null) { Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); remoteControlIntent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat( PendingIntent.getBroadcast(context, 0, remoteControlIntent, 0)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); } mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE); registerLockScreenReceiver(context); } }
Example 10
Source Project: Bop File: ServicePlayMusic.java License: Apache License 2.0 | 5 votes |
private void initMediaSession() { ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); mMediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "Tag", mediaButtonReceiver, null); mMediaSessionCompat.setCallback(mMediaSessionCallback); mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setClass(this, MediaButtonReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); mMediaSessionCompat.setMediaButtonReceiver(pendingIntent); setSessionToken(mMediaSessionCompat.getSessionToken()); }
Example 11
Source Project: screenstandby File: Core.java License: GNU General Public License v2.0 | 5 votes |
private void sendMediaButton(int keycode) { long eventtime = SystemClock.uptimeMillis(); Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keycode, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); context.sendOrderedBroadcast(downIntent, null); Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, keycode, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); context.sendOrderedBroadcast(upIntent, null); }
Example 12
Source Project: MediaNotification File: ActionReceiver.java License: Apache License 2.0 | 5 votes |
public void sendKeyPressBroadcastParcelable(Context context, int action, int keycode, String packageName) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(0, 0, action, keycode, 0)); if (packageName != null) intent.setPackage(packageName); context.sendOrderedBroadcast(intent, null); }
Example 13
Source Project: Prodigal File: PlayerService.java License: Apache License 2.0 | 5 votes |
private void initMediaSession() { ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); mediaSession = new MediaSessionCompat(getApplicationContext(), "PrdPlayer", mediaButtonReceiver, null); // mediaSession.setCallback(mMediaSessionCallback); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS ); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setClass(this, MediaButtonReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); mediaSession.setMediaButtonReceiver(pendingIntent); setSessionToken(mediaSession.getSessionToken()); }
Example 14
Source Project: OpenFit File: MediaController.java License: MIT License | 5 votes |
private static void sendKeyevent(KeyEvent keyEvent) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // use AudioManager.dispatchKeyEvent() from API 19 (KitKat) // http://stackoverflow.com/questions/19890643/android-4-4-play-default-music-player audioManager.dispatchMediaKeyEvent(keyEvent); } else { // use broadcasting fake intent method // http://stackoverflow.com/questions/12925896/android-emulate-send-action-media-button-intent Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON); i.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); context.sendOrderedBroadcast(i, null); } }
Example 15
Source Project: microbit File: RemoteControlPlugin.java License: Apache License 2.0 | 5 votes |
/** * General method to send media key event with provides event code and action to perform. * * @param action Key event action. * @param code Event code. */ private void sendMediaKeyEvent(final int action, final int code) { Intent mediaEvent = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent event = new KeyEvent(action, code); mediaEvent.putExtra(Intent.EXTRA_KEY_EVENT, event); MBApp.getApp().sendOrderedBroadcast(mediaEvent, null); }
Example 16
Source Project: Noyze File: AudioHelper.java License: Apache License 2.0 | 5 votes |
/** Use to send {@link android.content.Intent#ACTION_MEDIA_BUTTON} to this application. */ public static void dispatchMediaKeyEventSelf(Context mContext, int keyCode) { long eventtime = SystemClock.uptimeMillis(); KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0); KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP); Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); keyIntent.setPackage(mContext.getPackageName()); keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown); mContext.sendOrderedBroadcast(keyIntent, null); keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp); mContext.sendOrderedBroadcast(keyIntent, null); }
Example 17
Source Project: Popeens-DSub File: RemoteControlClientLP.java License: GNU General Public License v3.0 | 5 votes |
@Override public void register(Context context, ComponentName mediaButtonReceiverComponent) { downloadService = (DownloadService) context; mediaSession = new MediaSessionCompat(downloadService, "DSub MediaSession"); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0); mediaSession.setMediaButtonReceiver(mediaPendingIntent); Intent activityIntent = new Intent(context, SubsonicFragmentActivity.class); activityIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true); activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0); mediaSession.setSessionActivity(activityPendingIntent); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mediaSession.setCallback(new EventCallback()); mediaSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC); mediaSession.setActive(true); Bundle sessionExtras = new Bundle(); sessionExtras.putBoolean(WEAR_BACKGROUND_THEME, true); sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_PREVIOUS, true); sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_NEXT, true); sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_PREVIOUS, true); sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_NEXT, true); mediaSession.setExtras(sessionExtras); imageLoader = SubsonicActivity.getStaticImageLoader(context); }
Example 18
Source Project: Noyze File: Utils.java License: Apache License 2.0 | 5 votes |
/** * Gets the list of available media receivers. * @return The list of {@code ResolveInfo} for different media button receivers. */ public static List<ResolveInfo> getMediaReceivers(PackageManager packageManager) { if (null == packageManager) return new ArrayList<ResolveInfo>(0); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); return packageManager.queryBroadcastReceivers(mediaButtonIntent, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER); }
Example 19
Source Project: android_9.0.0_r45 File: MediaSessionRecord.java License: Apache License 2.0 | 4 votes |
private Intent createMediaButtonIntent(KeyEvent keyEvent) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); return mediaButtonIntent; }
Example 20
Source Project: RetroMusicPlayer File: MusicService.java License: GNU General Public License v3.0 | 4 votes |
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, "RetroMusic", 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); }