Example usage for android.view KeyEvent KEYCODE_MEDIA_PLAY_PAUSE

List of usage examples for android.view KeyEvent KEYCODE_MEDIA_PLAY_PAUSE

Introduction

In this page you can find the example usage for android.view KeyEvent KEYCODE_MEDIA_PLAY_PAUSE.

Prototype

int KEYCODE_MEDIA_PLAY_PAUSE

To view the source code for android.view KeyEvent KEYCODE_MEDIA_PLAY_PAUSE.

Click Source Link

Document

Key code constant: Play/Pause media key.

Usage

From source file:github.daneren2005.dsub.util.Notifications.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean expanded,
        boolean playing, boolean remote) {

    // Use the same text for the ticker and the expanded notification
    String title = song.getTitle();
    String arist = song.getArtist();
    String album = song.getAlbum();

    // Set the album art.
    try {//from   w  w w. ja  v  a2  s .  c  o  m
        ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
        Bitmap bitmap = null;
        if (imageLoader != null) {
            bitmap = imageLoader.getCachedImage(context, song, false);
        }
        if (bitmap == null) {
            // set default album art
            rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            rv.setImageViewBitmap(R.id.notification_image, bitmap);
        }
    } catch (Exception x) {
        Log.w(TAG, "Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    // set the text for the notifications
    rv.setTextViewText(R.id.notification_title, title);
    rv.setTextViewText(R.id.notification_artist, arist);
    rv.setTextViewText(R.id.notification_album, album);

    boolean persistent = Util.getPreferences(context)
            .getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false);
    if (persistent) {
        if (expanded) {
            rv.setImageViewResource(R.id.control_pause,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);
        } else {
            rv.setImageViewResource(R.id.control_previous,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);
            rv.setImageViewResource(R.id.control_pause, R.drawable.notification_forward);
            rv.setImageViewResource(R.id.control_next, R.drawable.notification_close);
        }
    }

    // Create actions for media buttons
    PendingIntent pendingIntent;
    int previous = 0, pause = 0, next = 0, close = 0;
    if (persistent && !expanded) {
        pause = R.id.control_previous;
        next = R.id.control_pause;
        close = R.id.control_next;
    } else {
        previous = R.id.control_previous;
        pause = R.id.control_pause;
        next = R.id.control_next;
    }

    if ((remote || persistent) && close == 0 && expanded) {
        close = R.id.notification_close;
        rv.setViewVisibility(close, View.VISIBLE);
    }

    if (previous > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadService.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(previous, pendingIntent);
    }
    if (pause > 0) {
        if (playing) {
            Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
            pauseIntent.setComponent(new ComponentName(context, DownloadService.class));
            pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                    new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
            pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
            rv.setOnClickPendingIntent(pause, pendingIntent);
        } else {
            Intent prevIntent = new Intent("KEYCODE_MEDIA_START");
            prevIntent.setComponent(new ComponentName(context, DownloadService.class));
            prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                    new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
            pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
            rv.setOnClickPendingIntent(pause, pendingIntent);
        }
    }
    if (next > 0) {
        Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
        nextIntent.setComponent(new ComponentName(context, DownloadService.class));
        nextIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
        pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
        rv.setOnClickPendingIntent(next, pendingIntent);
    }
    if (close > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadService.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(close, pendingIntent);
    }
}

From source file:com.geryon.ocraa.Fragment_Player.java

public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
    case KeyEvent.KEYCODE_HEADSETHOOK:
        getActivity().startService(new Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
        return true;
    }/*from   w  w  w.j  a v a 2 s. c om*/
    return onKeyDown(keyCode, event);
}

From source file:org.videolan.vlc.gui.tv.MainTvActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keyCode == KeyEvent.KEYCODE_BUTTON_Y)
            && mSelectedItem instanceof MediaWrapper) {
        MediaWrapper media = (MediaWrapper) mSelectedItem;
        if (media.getType() != MediaWrapper.TYPE_DIR)
            return false;
        Intent intent = new Intent(this, DetailsActivity.class);
        // pass the item information
        intent.putExtra("media", (MediaWrapper) mSelectedItem);
        intent.putExtra("item", new MediaItemDetails(media.getTitle(), media.getArtist(), media.getAlbum(),
                media.getLocation(), media.getArtworkURL()));
        startActivity(intent);//from  w w  w.j  av  a 2 s .  co m
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:net.sf.asap.Player.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (mediaController == null) {
        // error shown
        return super.onKeyDown(keyCode, event);
    }//  w ww.  j a v a2s.c o  m
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        if (isPaused())
            resume();
        else
            audioTrack.pause();
        return true;
    case KeyEvent.KEYCODE_MEDIA_NEXT:
        playNextSong();
        return true;
    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        playPreviousSong();
        return true;
    default:
        return super.onKeyDown(keyCode, event);
    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override//from ww  w. ja va2s  . c om
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                    if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
                        pause();
                    } else {
                        play(currentMedia);
                    }
                    return true;
                }
            }
            return false;
        }
    });
}

From source file:com.oakesville.mythling.MediaPagerActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            if ((getCurrentFocus().getParent() instanceof View
                    && ((View) getCurrentFocus().getParent()).getId() == R.id.button_bar)
                    || getCurrentFocus().getId() == R.id.title_text) {
                findViewById(R.id.pager_position).requestFocus();
                return true;
            }/*from   w  ww.j av  a  2s.c o m*/
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (getCurrentFocus().getId() == R.id.pager_position) {
                if (pager.getCurrentItem() < getListables().size() - 1) {
                    pager.setCurrentItem(pager.getCurrentItem() + 1);
                    positionBar.setProgress(pager.getCurrentItem());
                    return true;
                }
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (getCurrentFocus().getId() == R.id.pager_position) {
                if (pager.getCurrentItem() > 0) {
                    pager.setCurrentItem(pager.getCurrentItem() - 1);
                    positionBar.setProgress(pager.getCurrentItem());
                    return true;
                }
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
            pager.setCurrentItem(getListables().size() - 1);
            positionBar.setProgress(getListables().size() - 1);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_REWIND) {
            pager.setCurrentItem(0);
            positionBar.setProgress(0);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                || event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY) {
            Listable listable = getListables().get(pager.getCurrentItem());
            if (listable instanceof Item) {
                playItem((Item) listable);
                return true;
            }
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.vuze.android.remote.fragment.FilesFragment.java

public View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container,
        Bundle savedInstanceState) {/*from ww w  .j  a  v  a 2 s  . c  o  m*/

    if (AndroidUtils.DEBUG) {
        Log.d(TAG, "onCreateview " + this);
    }

    View view = inflater.inflate(R.layout.frag_torrent_files, container, false);

    FragmentActivity activity = getActivity();

    progressBar = (ProgressBar) activity.findViewById(R.id.details_progress_bar);

    viewAreaToggleEditMode = view.findViewById(R.id.files_area_toggleditmode);
    tvScrollTitle = (TextView) view.findViewById(R.id.files_scrolltitle);

    btnEditMode = (CompoundButton) view.findViewById(R.id.files_editmode);
    if (btnEditMode != null) {
        btnEditMode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (adapter == null) {
                    return;
                }
                adapter.setInEditMode(btnEditMode.isChecked());
            }
        });
    }

    final SwipeRefreshLayoutExtra swipeRefresh = (SwipeRefreshLayoutExtra) view
            .findViewById(R.id.swipe_container);
    if (swipeRefresh != null) {
        swipeRefresh.setExtraLayout(R.layout.swipe_layout_extra);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (sessionInfo == null) {
                    return;
                }
                showProgressBar();
                sessionInfo.executeRpc(new RpcExecuter() {
                    @Override
                    public void executeRpc(TransmissionRPC rpc) {
                        rpc.getTorrentFileInfo(TAG, torrentID, null, new TorrentListReceivedListener() {

                            @Override
                            public void rpcTorrentListReceived(String callID, List<?> addedTorrentMaps,
                                    List<?> removedTorrentIDs) {
                                AndroidUtils.runOnUIThread(FilesFragment.this, new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefresh.setRefreshing(false);
                                    }
                                });
                            }
                        });
                    }
                });

            }
        });
        swipeRefresh.setOnExtraViewVisibilityChange(this);
    }

    listview = (RecyclerView) view.findViewById(R.id.files_list);
    listview.setLayoutManager(new PreCachingLayoutManager(getContext()));
    listview.setAdapter(adapter);

    listview.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            {
                if (event.getAction() != KeyEvent.ACTION_DOWN) {
                    return false;
                }
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_RIGHT: {
                    // expand
                    int i = adapter.getSelectedPosition();
                    FilesAdapterDisplayObject item = adapter.getItem(i);
                    if (item instanceof FilesAdapterDisplayFolder) {
                        if (!((FilesAdapterDisplayFolder) item).expand) {
                            ((FilesAdapterDisplayFolder) item).expand = true;
                            adapter.getFilter().filter("");
                            return true;
                        }
                    }
                    break;
                }

                case KeyEvent.KEYCODE_DPAD_LEFT: {
                    // collapse
                    int i = adapter.getSelectedPosition();
                    FilesAdapterDisplayObject item = adapter.getItem(i);
                    if (item instanceof FilesAdapterDisplayFolder) {
                        if (((FilesAdapterDisplayFolder) item).expand) {
                            ((FilesAdapterDisplayFolder) item).expand = false;
                            adapter.getFilter().filter("");
                            return true;
                        }
                    }
                    break;
                }

                case KeyEvent.KEYCODE_MEDIA_PLAY:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: {
                    launchOrStreamFile();
                    return true;
                }
                }

                return false;
            }
        }
    });

    listview.setOnScrollListener(new RecyclerView.OnScrollListener() {
        int firstVisibleItem = 0;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager lm = (LinearLayoutManager) listview.getLayoutManager();
            int firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();
            if (firstVisibleItem != this.firstVisibleItem) {
                this.firstVisibleItem = firstVisibleItem;
                FilesAdapterDisplayObject itemAtPosition = adapter.getItem(firstVisibleItem);
                //               Log.d(TAG, "itemAt" + firstVisibleItem + " is " + itemAtPosition);
                //               Log.d(TAG, "tvScrollTitle=" + tvScrollTitle);
                //               Log.d(TAG, "viewAreaToggleEditMode=" + viewAreaToggleEditMode);

                if (itemAtPosition == null) {
                    return;
                }
                if (itemAtPosition.parent != null) {
                    if (viewAreaToggleEditMode != null) {
                        viewAreaToggleEditMode.setVisibility(View.GONE);
                    }
                    if (tvScrollTitle != null) {
                        tvScrollTitle.setVisibility(View.VISIBLE);
                        tvScrollTitle.setText(itemAtPosition.parent.folder);
                    }
                } else {
                    if (viewAreaToggleEditMode != null) {
                        viewAreaToggleEditMode.setVisibility(View.VISIBLE);
                    }
                    if (tvScrollTitle != null) {
                        if (viewAreaToggleEditMode != null) {
                            tvScrollTitle.setVisibility(View.INVISIBLE);
                        }
                        tvScrollTitle.setText("");
                    }
                }
            }
        }
    });

    FlexibleRecyclerSelectionListener rs = new FlexibleRecyclerSelectionListener<FilesTreeAdapter, FilesAdapterDisplayObject>() {
        @Override
        public void onItemSelected(FilesTreeAdapter adapter, final int position, boolean isChecked) {
        }

        @Override
        public void onItemClick(FilesTreeAdapter adapter, int position) {
            if (AndroidUtils.usesNavigationControl()) {
                FilesAdapterDisplayObject oItem = adapter.getItem(position);
                if (adapter.isInEditMode()) {
                    adapter.flipWant(oItem);
                    return;
                }
                if (oItem instanceof FilesAdapterDisplayFolder) {
                    FilesAdapterDisplayFolder oFolder = (FilesAdapterDisplayFolder) oItem;
                    oFolder.expand = !oFolder.expand;
                    adapter.getFilter().filter("");
                } else {
                    showFileContextMenu();
                }
            }
        }

        @Override
        public boolean onItemLongClick(FilesTreeAdapter adapter, int position) {
            if (AndroidUtils.usesNavigationControl()) {
                if (showFileContextMenu()) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void onItemCheckedChanged(FilesTreeAdapter adapter, FilesAdapterDisplayObject item,
                boolean isChecked) {

            if (adapter.getCheckedItemCount() == 0) {
                finishActionMode();
            } else {
                // Update the subtitle with file name
                showContextualActions();
            }

            AndroidUtils.invalidateOptionsMenuHC(getActivity(), mActionMode);
        }
    };

    adapter = new FilesTreeAdapter(this.getActivity(), rs);
    adapter.setSessionInfo(sessionInfo);
    adapter.setMultiCheckModeAllowed(false);
    adapter.setCheckOnSelectedAfterMS(100);
    listview.setAdapter(adapter);

    return view;
}

From source file:github.popeen.dsub.util.Notifications.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean expanded,
        boolean playing, boolean remote, boolean isSingleFile, boolean shouldFastForward) {
    // Use the same text for the ticker and the expanded notification
    String title = song.getTitle();
    String arist = song.getArtist();
    String album = song.getAlbum();

    // Set the album art.
    try {/*w w  w .  j a  v a  2 s .  c o m*/
        ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
        Bitmap bitmap = null;
        if (imageLoader != null) {
            bitmap = imageLoader.getCachedImage(context, song, false);
        }
        if (bitmap == null) {
            // set default album art
            rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            imageLoader.setNowPlayingSmall(bitmap);
            rv.setImageViewBitmap(R.id.notification_image, bitmap);
        }
    } catch (Exception x) {
        Log.w(TAG, "Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    // set the text for the notifications
    rv.setTextViewText(R.id.notification_title, title);
    rv.setTextViewText(R.id.notification_artist, arist);
    rv.setTextViewText(R.id.notification_album, album);

    boolean persistent = Util.getPreferences(context)
            .getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false);
    if (persistent) {
        if (expanded) {
            rv.setImageViewResource(R.id.control_pause,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);

            if (shouldFastForward) {
                rv.setImageViewResource(R.id.control_previous, R.drawable.notification_rewind);
                rv.setImageViewResource(R.id.control_next, R.drawable.notification_fastforward);
            } else {
                rv.setImageViewResource(R.id.control_previous, R.drawable.notification_backward);
                rv.setImageViewResource(R.id.control_next, R.drawable.notification_forward);
            }
        } else {
            rv.setImageViewResource(R.id.control_previous,
                    playing ? R.drawable.notification_pause : R.drawable.notification_start);
            if (shouldFastForward) {
                rv.setImageViewResource(R.id.control_pause, R.drawable.notification_fastforward);
            } else {
                rv.setImageViewResource(R.id.control_pause, R.drawable.notification_forward);
            }
            rv.setImageViewResource(R.id.control_next, R.drawable.notification_close);
        }
    } else if (shouldFastForward) {
        rv.setImageViewResource(R.id.control_previous, R.drawable.notification_rewind);
        rv.setImageViewResource(R.id.control_next, R.drawable.notification_fastforward);
    } else {
        // Necessary for switching back since it appears to re-use the same layout
        rv.setImageViewResource(R.id.control_previous, R.drawable.notification_backward);
        rv.setImageViewResource(R.id.control_next, R.drawable.notification_forward);
    }

    // Create actions for media buttons

    int previous = 0, pause = 0, next = 0, close = 0, rewind = 0, fastForward = 0;
    if (expanded) {
        pause = R.id.control_pause;

        if (shouldFastForward) {
            rewind = R.id.control_previous;
            fastForward = R.id.control_next;
        } else {
            previous = R.id.control_previous;
            next = R.id.control_next;
        }

        if (remote || persistent) {
            close = R.id.notification_close;
            rv.setViewVisibility(close, View.VISIBLE);
        }
    } else {
        if (persistent) {
            pause = R.id.control_previous;
            if (shouldFastForward) {
                fastForward = R.id.control_pause;
            } else {
                next = R.id.control_pause;
            }
            close = R.id.control_next;
        } else {
            if (shouldFastForward) {
                rewind = R.id.control_previous;
                fastForward = R.id.control_next;
            } else {
                previous = R.id.control_previous;
                next = R.id.control_next;
            }

            pause = R.id.control_pause;
        }
    }

    if (isSingleFile) {
        if (previous > 0) {
            rv.setViewVisibility(previous, View.GONE);
            previous = 0;
        }
        if (rewind > 0) {
            rv.setViewVisibility(rewind, View.GONE);
            rewind = 0;
        }

        if (next > 0) {
            rv.setViewVisibility(next, View.GONE);
            next = 0;
        }

        if (fastForward > 0) {
            rv.setViewVisibility(fastForward, View.GONE);
            fastForward = 0;
        }
    }

    PendingIntent pendingIntent;
    if (previous > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadService.class));

        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(previous, pendingIntent);
    }
    if (rewind > 0) {
        Intent rewindIntent = new Intent("KEYCODE_MEDIA_REWIND");
        rewindIntent.setComponent(new ComponentName(context, DownloadService.class));
        rewindIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_REWIND));
        pendingIntent = PendingIntent.getService(context, 0, rewindIntent, 0);
        rv.setOnClickPendingIntent(rewind, pendingIntent);
    }
    if (pause > 0) {
        if (playing) {
            Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
            pauseIntent.setComponent(new ComponentName(context, DownloadService.class));

            pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                    new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
            pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
            rv.setOnClickPendingIntent(pause, pendingIntent);
        } else {
            Intent prevIntent = new Intent("KEYCODE_MEDIA_START");
            prevIntent.setComponent(new ComponentName(context, DownloadService.class));

            prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                    new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
            pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
            rv.setOnClickPendingIntent(pause, pendingIntent);
        }
    }
    if (next > 0) {
        Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
        nextIntent.setComponent(new ComponentName(context, DownloadService.class));

        nextIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
        pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
        rv.setOnClickPendingIntent(next, pendingIntent);
    }
    if (fastForward > 0) {
        Intent fastForwardIntent = new Intent("KEYCODE_MEDIA_FAST_FORWARD");
        fastForwardIntent.setComponent(new ComponentName(context, DownloadService.class));
        fastForwardIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD));
        pendingIntent = PendingIntent.getService(context, 0, fastForwardIntent, 0);
        rv.setOnClickPendingIntent(fastForward, pendingIntent);
    }
    if (close > 0) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadService.class));

        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(close, pendingIntent);
    }
}

From source file:com.marvin.rocklock.RockLockActivity.java

/**
 * Pick up back button and volume up and down
 *//*from  w  w  w .jav a2 s .  c  o  m*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        mPlayer.togglePlayPause();
        return true;
    case KeyEvent.KEYCODE_MEDIA_NEXT:
        mPlayer.nextTrack(false);
        return true;
    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        mPlayer.prevTrack(false);
        return true;
    case KeyEvent.KEYCODE_VOLUME_UP:
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
                AudioManager.FLAG_PLAY_SOUND);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
                AudioManager.FLAG_PLAY_SOUND);
        return true;
    case KeyEvent.KEYCODE_BACK:
        onBackPressed();
        return true;
    default:
        return super.onKeyDown(keyCode, event);
    }
}

From source file:com.example.android.mediarouter.player.MainActivity.java

public boolean handleMediaKey(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: {
            Log.d(TAG, "Received Play/Pause event from RemoteControlClient");
            mPaused = !mPaused;/* w  w w. ja v a2 s  .  c om*/
            if (mPaused) {
                mSessionManager.pause();
            } else {
                mSessionManager.resume();
            }
            return true;
        }
        case KeyEvent.KEYCODE_MEDIA_PLAY: {
            Log.d(TAG, "Received Play event from RemoteControlClient");
            if (mPaused) {
                mPaused = false;
                mSessionManager.resume();
            }
            return true;
        }
        case KeyEvent.KEYCODE_MEDIA_PAUSE: {
            Log.d(TAG, "Received Pause event from RemoteControlClient");
            if (!mPaused) {
                mPaused = true;
                mSessionManager.pause();
            }
            return true;
        }
        case KeyEvent.KEYCODE_MEDIA_STOP: {
            Log.d(TAG, "Received Stop event from RemoteControlClient");
            mPaused = false;
            mSessionManager.stop();
            return true;
        }
        default:
            break;
        }
    }
    return false;
}