Java Code Examples for android.content.Intent.ACTION_MEDIA_BUTTON

The following are Jave code examples for showing how to use ACTION_MEDIA_BUTTON of the android.content.Intent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: C500Companion   File: C500Service.java   View Source Code Vote up 8 votes
public static void KeyPressDownAndUp(int key, Context context) {
    long eventtime = SystemClock.uptimeMillis() - 1;

    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, key, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
    context.sendOrderedBroadcast(downIntent, null);

    eventtime++;
    Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, key, 0);
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
    context.sendOrderedBroadcast(upIntent, null);
}
 
Example 2
Project: Phoenix-for-VK   File: MusicPlaybackService.java   View Source Code Vote up 7 votes
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();

}
 
Example 3
Project: MediaNotification   File: ActionReceiver.java   View Source Code Vote up 7 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 4
Project: IdealMedia   File: PlayerService.java   View Source Code Vote up 7 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 5
Project: AndroidAudioExample   File: PlayerService.java   View Source Code Vote up 6 votes
@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();
}
 
Example 6
Project: microbit   File: RemoteControlPlugin.java   View Source Code Vote up 6 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 7
Project: GravityBox   File: ModVolumeKeySkipTrack.java   View Source Code Vote up 6 votes
private static void sendMediaButtonEvent(Object phoneWindowManager, int code) {
    long eventtime = SystemClock.uptimeMillis();
    Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent keyEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, code, 0);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
    dispatchMediaButtonEvent(keyEvent);

    keyEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
    dispatchMediaButtonEvent(keyEvent);
}
 
Example 8
Project: bluewatcher   File: MediaKeySimulator.java   View Source Code Vote up 6 votes
public static void simulateMediaKey(Activity activity, int key) {
	KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, key);
       Intent intentDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
       intentDown.putExtra(Intent.EXTRA_KEY_EVENT, keyDown);
	activity.sendOrderedBroadcast(intentDown, null);
	
	KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, key);
       Intent intentUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
       intentUp.putExtra(Intent.EXTRA_KEY_EVENT, keyUp);
	activity.sendOrderedBroadcast(intentUp, null);
}
 
Example 9
Project: RetroMusicPlayer   File: MusicService.java   View Source Code Vote up 6 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, "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);
}
 
Example 10
Project: NotificationStyles   File: MainActivity.java   View Source Code Vote up 5 votes
public static PendingIntent getActionIntent (Context context, int mediaKeyEvent) {
    Intent it = new Intent(Intent.ACTION_MEDIA_BUTTON);
    it.setPackage(context.getPackageName());
    it.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, mediaKeyEvent));
    return PendingIntent.getBroadcast(context, mediaKeyEvent, it, 0);
}
 
Example 11
Project: Orin   File: MusicService.java   View Source Code Vote up 5 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, "Phonograph", 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);
}
 
Example 12
Project: MusicX-music-player   File: MusicXService.java   View Source Code Vote up 5 votes
@Override
public void mediaLockscreen() {
    mediaSessionLockscreen = new MediaSessionCompat(this, TAG);
    mediaSessionLockscreen.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onPause() {
            pause();
        }

        @Override
        public void onSkipToNext() {
            playnext(true);
        }

        @Override
        public void onSkipToPrevious() {
            playprev(true);
        }

        @Override
        public void onStop() {
            stopSelf();
        }

        @Override
        public void onSeekTo(long pos) {
            seekto((int) pos);
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            if (mediaButtonReceiver != null) {
                mediaButtonReceiver.onReceive(MusicXService.this, mediaButtonEvent);
            }
            return true;
        }
    });
    mediaSessionLockscreen.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    ComponentName buttonCom = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.setComponent(buttonCom);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSessionLockscreen.setMediaButtonReceiver(pendingIntent);
    mediaSessionLockscreen.setActive(true);
}