當前位置: 首頁>>代碼示例>>Java>>正文


Java Intent.ACTION_MEDIA_BUTTON屬性代碼示例

本文整理匯總了Java中android.content.Intent.ACTION_MEDIA_BUTTON屬性的典型用法代碼示例。如果您正苦於以下問題:Java Intent.ACTION_MEDIA_BUTTON屬性的具體用法?Java Intent.ACTION_MEDIA_BUTTON怎麽用?Java Intent.ACTION_MEDIA_BUTTON使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在android.content.Intent的用法示例。


在下文中一共展示了Intent.ACTION_MEDIA_BUTTON屬性的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: KeyPressDownAndUp

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);
}
 
開發者ID:mmlevin,項目名稱:C500Companion,代碼行數:14,代碼來源:C500Service.java

示例2: setUpRemoteControlClient

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();

}
 
開發者ID:PhoenixDevTeam,項目名稱:Phoenix-for-VK,代碼行數:26,代碼來源:MusicPlaybackService.java

示例3: sendKeyPressBroadcastParcelable

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);
}
 
開發者ID:TheAndroidMaster,項目名稱:MediaNotification,代碼行數:8,代碼來源:ActionReceiver.java

示例4: registerRemoteControl

@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);
}
 
開發者ID:dmllr,項目名稱:IdealMedia,代碼行數:16,代碼來源:PlayerService.java

示例5: onCreate

@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();
}
 
開發者ID:SergeyVinyar,項目名稱:AndroidAudioExample,代碼行數:42,代碼來源:PlayerService.java

示例6: sendMediaKeyEvent

/**
 * 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);
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:12,代碼來源:RemoteControlPlugin.java

示例7: sendMediaButtonEvent

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);
}
 
開發者ID:WrBug,項目名稱:GravityBox,代碼行數:11,代碼來源:ModVolumeKeySkipTrack.java

示例8: simulateMediaKey

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);
}
 
開發者ID:masterjc,項目名稱:bluewatcher,代碼行數:11,代碼來源:MediaKeySimulator.java

示例9: setupMediaSession

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);
}
 
開發者ID:h4h13,項目名稱:RetroMusicPlayer,代碼行數:52,代碼來源:MusicService.java

示例10: getActionIntent

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);
}
 
開發者ID:boybeak,項目名稱:NotificationStyles,代碼行數:6,代碼來源:MainActivity.java

示例11: setupMediaSession

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);
}
 
開發者ID:aliumujib,項目名稱:Orin,代碼行數:51,代碼來源:MusicService.java

示例12: mediaLockscreen

@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);
}
 
開發者ID:RajneeshSingh007,項目名稱:MusicX-music-player,代碼行數:50,代碼來源:MusicXService.java


注:本文中的android.content.Intent.ACTION_MEDIA_BUTTON屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。