List of usage examples for android.content Intent ACTION_MEDIA_BUTTON
String ACTION_MEDIA_BUTTON
To view the source code for android.content Intent ACTION_MEDIA_BUTTON.
Click Source Link
From source file:com.geryon.ocraa.MusicService.java
/** * Starts playing the next song. If manualUrl is null, the next song will be randomly selected * from our Media Retriever (that is, it will be a random song in the user's device). If * manualUrl is non-null, then it specifies the URL or path to the song that will be played * next.//from w ww. jav a2 s . co m */ void playNextSong(int pos) { position = pos; mState = State.Stopped; progressBarHandler.removeCallbacks(mUpdateTimeTask); MusicRetriever.Item playingItem = null; relaxResources(false); // release everything except MediaPlayer try { if ((position == -1) || (repeat == false && shuffle == true)) { Random mRandom = new Random(); int random = mRandom.nextInt(mRetriever.ItemSize()); playingItem = mRetriever.getItem(random); Log.d("Randomize:", String.valueOf(random)); position = random; Log.d("Position:", String.valueOf(random)); if (playingItem == null) { Toast.makeText(this, "No available music to play. Place some music on your external storage " + "device (e.g. your SD card) and try again.", Toast.LENGTH_LONG).show(); processStopRequest(true); // stop everything! return; } createMediaPlayerIfNeeded(); mPlayer.setDataSource(playingItem.getURI().toString()); } else if (position != -1) { if (repeat == false) { if (shuffle == false) { if (position == mRetriever.ItemSize() - 1) { position = 0; } else { position++; } } } playingItem = mRetriever.getItem(position); createMediaPlayerIfNeeded(); mPlayer.setDataSource(playingItem.getURI().toString()); } mSongTitle = playingItem.getTitle(); mState = State.Preparing; setUpAsForeground(mSongTitle + " (loading)"); // Use the media button APIs (if available) to register ourselves for media button // events MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent); // Use the remote control APIs (if available) to set the playback state if (mRemoteControlClientCompat == null) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast( this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/)); 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); // Update the remote controls mRemoteControlClientCompat.editMetadata(true) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, playingItem.getArtist()) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, playingItem.getAlbum()) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, playingItem.getTitle()) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, playingItem.getDuration()) // TODO: fetch real item artwork .putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, mDummyAlbumArt) .apply(); // starts preparing the media player in the background. When it's done, it will call // our OnPreparedListener (that is, the onPrepared() method on this class, since we set // the listener to 'this'). // // Until the media player is prepared, we *cannot* call start() on it! mPlayer.prepareAsync(); updateProgressBar(); // If we are streaming from the internet, we want to hold a Wifi lock, which prevents // the Wifi radio from going to sleep while the song is playing. If, on the other hand, // we are *not* streaming, we want to release the lock if we were holding it before. if (mIsStreaming) mWifiLock.acquire(); else if (mWifiLock.isHeld()) mWifiLock.release(); } catch (IOException ex) { // Log.e("MusicService", "IOException playing next song: " + ex.getMessage()); // ex.printStackTrace(); } }
From source file:net.sourceforge.servestream.service.MediaPlaybackService.java
@Override public void onCreate() { super.onCreate(); int imageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_notification_size); ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(this, NOTIFICATION_IMAGE_CACHE_DIR); cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory mNotificationImageFetcher = new DatabaseImageResizer(this, imageThumbSize); mNotificationImageFetcher.addImageCache(cacheParams); cacheParams = new ImageCache.ImageCacheParams(this, LOCK_SCREEN_IMAGE_CACHE_DIR); cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory mLockScreenImageFetcher = new DatabaseImageResizer(this, 600); mLockScreenImageFetcher.addImageCache(cacheParams); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mMediaButtonReceiverComponent = new ComponentName(this, MediaButtonIntentReceiver.class); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent); // Use the remote control APIs (if available) to set the playback state if (mRemoteControlClientCompat == null) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); }//w w w . ja va 2 s .co m mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClientCompat.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClientCompat.FLAG_KEY_MEDIA_PLAY | RemoteControlClientCompat.FLAG_KEY_MEDIA_PAUSE | RemoteControlClientCompat.FLAG_KEY_MEDIA_NEXT | RemoteControlClientCompat.FLAG_KEY_MEDIA_STOP); mPreferences = PreferenceManager.getDefaultSharedPreferences(this); mPreferences.registerOnSharedPreferenceChangeListener(this); final boolean lockingWifi = mPreferences.getBoolean(PreferenceConstants.WIFI_LOCK, true); mConnectivityManager = new ConnectivityReceiver(this, lockingWifi); mRetrieveShoutCastMetadata = mPreferences.getBoolean(PreferenceConstants.RETRIEVE_SHOUTCAST_METADATA, false); mMediaButtonReceiverComponent = new ComponentName(this, MediaButtonIntentReceiver.class); mCardId = MusicUtils.getCardId(this); registerExternalStorageListener(); // Needs to be done in this thread, since otherwise ApplicationContext.getPowerManager() crashes. mPlayer = new MultiPlayer(this); mPlayer.setHandler(mMediaplayerHandler); reloadQueue(); notifyChange(QUEUE_CHANGED); notifyChange(META_CHANGED); IntentFilter commandFilter = new IntentFilter(); commandFilter.addAction(SERVICECMD); commandFilter.addAction(TOGGLEPAUSE_ACTION); commandFilter.addAction(PAUSE_ACTION); commandFilter.addAction(NEXT_ACTION); commandFilter.addAction(PREVIOUS_ACTION); registerReceiver(mIntentReceiver, commandFilter); // If the service was idle, but got killed before it stopped itself, the // system will relaunch it. Make sure it gets stopped again in that case. Message msg = mDelayedStopHandler.obtainMessage(); mDelayedStopHandler.sendMessageDelayed(msg, IDLE_DELAY); }
From source file:me.spadival.podmode.PodModeService.java
@Override public void onCreate() { IntentFilter usbFilter = new IntentFilter(); usbFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); usbFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, usbFilter); // mProvider = RemoteMetadataProvider.getInstance(this); // mProvider.setOnMetadataChangeListener(mMetadataListner); // mProvider.acquireRemoteControls(); LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this); IntentFilter notifyFilter = new IntentFilter(); notifyFilter.addAction(NOTIFYACTION); bManager.registerReceiver(mNotifyReceiver, notifyFilter); mSerialHost = new FTDriver((UsbManager) getSystemService(Context.USB_SERVICE)); mSerialDevice = new FT311UARTInterface(this, null); // Get the local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // Get a set of currently paired devices Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); mBluetoothDevice = null;//from ww w.j av a2s. c o m /* // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().equals("PodModeBT")) mBluetoothDevice = device; } } if (mBluetoothDevice != null) { mBTDevice = new BluetoothSerialService(this, mHandler); mBTDevice.connect(mBluetoothDevice); } */ // Create the Wifi lock (this does not acquire the lock, this just // creates it) mWifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE)) .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock"); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); // Create the retriever and start an asynchronous task that will prepare // it. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); mRetriever = new MusicRetriever(getContentResolver(), getApplicationContext(), true, prefs); // mRetriever.switchToMainPlaylist(); prefs.registerOnSharedPreferenceChangeListener((OnSharedPreferenceChangeListener) this); mNowPlaying = prefs.getInt("nowplaying", 0); String mBaudrate = prefs.getString("baud_rate", "57600"); if (mBaudrate.equals("57600")) mSerialBaudRate = FTDriver.BAUD57600; else if (mBaudrate.equals("38400")) mSerialBaudRate = FTDriver.BAUD38400; else if (mBaudrate.equals("19200")) mSerialBaudRate = FTDriver.BAUD19200; (new PrepareMusicRetrieverTask(mRetriever, this)).execute(); // create the Audio Focus Helper, if the Audio Focus feature is // available (SDK 8 or above) if (android.os.Build.VERSION.SDK_INT >= 8) mAudioFocusHelper = new AudioFocusHelper(getApplicationContext(), this); else mAudioFocus = AudioFocus.Focused; // no focus feature, so we always // "have" audio focus boolean wakeLockPreferred = prefs.getBoolean("wakelock", false); if (podWakeLock == null && wakeLockPreferred) { PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE); podWakeLock = powerMgr.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKETAG); podWakeLock.acquire(); } PackageManager pxm = getPackageManager(); Intent mediaIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); List<ResolveInfo> mAppsInfo = pxm.queryBroadcastReceivers(mediaIntent, 0); mSimpleRemoteApp = prefs.getString("selectapp", null); mAdvancedRemoteApp = prefs.getString("selectadvancedapp", PACKAGENAME); // Make sure App preferences are still valid and Apps haven't been // uninstalled. if (mAppsInfo.size() > 0) { CharSequence[] entryValues = new CharSequence[mAppsInfo.size()]; CharSequence[] advEntryValues = new CharSequence[mAppsInfo.size() + 1]; advEntryValues[0] = PACKAGENAME; int i = 0; for (ResolveInfo info : mAppsInfo) { entryValues[i] = (String) info.activityInfo.packageName; advEntryValues[i + 1] = (String) info.activityInfo.packageName; i++; } boolean entryFound = false; if (mSimpleRemoteApp != null) { for (i = 0; i < entryValues.length; i++) { if (mSimpleRemoteApp.equals(entryValues[i])) { entryFound = true; } } } if (!entryFound) { SharedPreferences.Editor editor = prefs.edit(); editor.putString("selectapp", (String) entryValues[0]); editor.commit(); mSimpleRemoteApp = (String) entryValues[0]; } entryFound = false; if (mAdvancedRemoteApp != null) { for (i = 0; i < advEntryValues.length; i++) { if (mAdvancedRemoteApp.equals(advEntryValues[i])) { entryFound = true; } } } if (!entryFound) { SharedPreferences.Editor editor = prefs.edit(); editor.putString("selectadvancedapp", (String) advEntryValues[0]); editor.commit(); mAdvancedRemoteApp = (String) advEntryValues[0]; } } else { SharedPreferences.Editor editor = prefs.edit(); editor.putString("selectadvancedapp", PACKAGENAME); editor.commit(); mAdvancedRemoteApp = PACKAGENAME; } mAccessoryName = prefs.getString("accessoryName", null); mAccessoryMnf = prefs.getString("accessoryMnf", null); mAccessoryModel = prefs.getString("accessoryModel", null); }
From source file:com.iamplus.musicplayer.MusicService.java
private void startPlayback() { try {//w ww . j av a2 s .c o m MediaItem playingItem = null; mIsStreaming = false; // playing a locally available song playingItem = MusicRetriever.getInstance().getCurrentSong(); if (playingItem == null) { //Get all songs if the current list is empty MusicRetriever.getInstance().setSonglist(getAllSongs(), 0); MusicRetriever.getInstance().setPlayMode(e_play_mode.e_play_mode_shuffle); playingItem = MusicRetriever.getInstance().getCurrentSong(); if (playingItem == null) { processStopRequest(true); // stop everything! return; } } mCurrentPlayingItem = playingItem; // set the source of the media player a a content URI createMediaPlayerIfNeeded(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDataSource(getApplicationContext(), playingItem.getURI()); // starts preparing the media player in the background. When it's done, it will call // our OnPreparedListener (that is, the onPrepared() method on this class, since we set // the listener to 'this'). // // Until the media player is prepared, we *cannot* call start() on it! mPlayer.prepareAsync(); // If we are streaming from the internet, we want to hold a Wifi lock, which prevents // the Wifi radio from going to sleep while the song is playing. If, on the other hand, // we are *not* streaming, we want to release the lock if we were holding it before. // if (mIsStreaming) mWifiLock.acquire(); // else if (mWifiLock.isHeld()) mWifiLock.release(); } catch (IOException ex) { Log.e(TAG, "IOException playing first song: " + ex.getMessage()); //ex.printStackTrace(); Toast.makeText(getApplicationContext(), R.string.invalid_file_track, Toast.LENGTH_LONG).show(); //Stop Playback processStopRequest(); return; } //mSongTitle = mCurrentPlayingItem.getTitle(); //setUpAsForeground(mSongTitle + " (loading)"); // Use the media button APIs (if available) to register ourselves for media button // events MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent); // // Use the remote control APIs (if available) to set the playback state // if (mRemoteControlClientCompat == null) { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); } mRemoteControlClientCompat.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP); updateRemoteControlPlayingState(); }
From source file:me.spadival.podmode.PodModeService.java
public void broadcastMediaButtons(int keyCode, String app) { 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); if (app != null) downIntent.setPackage(app);//from w ww . j a va 2s . c o m sendOrderedBroadcast(downIntent, null); eventtime = SystemClock.uptimeMillis(); 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); if (app != null) upIntent.setPackage(app); sendOrderedBroadcast(upIntent, null); }
From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void registerRemoteControlClient(ComponentName componentName) { audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); remoteControlIntent.setComponent(componentName); RemoteControlClient localRemoteControlClient = new RemoteControlClient( PendingIntent.getBroadcast(context, 0, remoteControlIntent, 0)); localRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE); audioManager.registerRemoteControlClient(localRemoteControlClient); this.remoteControlClient = localRemoteControlClient; }
From source file:com.xperia64.timidityae.MusicService.java
@Override public void onCreate() { super.onCreate(); if (serviceReceiver != null) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(getResources().getString(R.string.msrv_rec)); intentFilter.addAction(Intent.ACTION_HEADSET_PLUG); intentFilter.addAction(Intent.ACTION_MEDIA_BUTTON); registerReceiver(serviceReceiver, intentFilter); }//from w w w. j ava 2s .com pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timidity AE"); wl.setReferenceCounted(false); if (shouldDoWidget) ids = AppWidgetManager.getInstance(getApplication()) .getAppWidgetIds(new ComponentName(getApplication(), TimidityAEWidgetProvider.class)); TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); } //foreground=false; if (wl.isHeld()) wl.release(); stopForeground(true); }
From source file:me.spadival.podmode.PodModeService.java
public void run() { podCommand pCommand = null;// w w w . j a v a 2 s . c o m podResponse pResponse = null; byte[] respBytes = null; long eventtime; Intent downIntent = null; KeyEvent downEvent = null; Intent upIntent = null; KeyEvent upEvent = null; // serialWrite(new byte[] { (byte) 0xFF, 0x55, 0x02, 0x00, 0x00, // (byte) 0xFE }); while (mPodRunning) { pCommand = readCommand(); if (pCommand == null) continue; if (pCommand.mode == 0x03) mPodStatus = podStat.DISPLAYREMOTE; if (mAccessoryName != null && (mPodStatus == podStat.SIMPLEREMOTE || mPodStatus == podStat.DISPLAYREMOTE || mPodStatus == podStat.ADVANCEDREMOTE || mPodStatus == podStat.ADVANCEDHACK) && mHTTPsend) { mHTTPsend = false; mHttpThread.start(); } if (mLaunchFirstTime && (pCommand.mode == 02 || (pCommand.mode == 04 && !mAdvancedRemoteApp.equals(PACKAGENAME)))) { String launchApp = null; if (pCommand.mode == 02) { mBanner = getString(R.string.simple_remote); launchApp = mSimpleRemoteApp; } if (pCommand.mode == 04) { mPodStatus = podStat.ADVANCEDREMOTE; if (!mAdvancedRemoteApp.equals(PACKAGENAME)) mPodStatus = podStat.ADVANCEDHACK; mBanner = getString(R.string.advanced_remote); launchApp = mAdvancedRemoteApp; } if (launchApp != null) { PackageManager pm = getPackageManager(); Intent LaunchIntent = pm.getLaunchIntentForPackage(launchApp); startActivity(LaunchIntent); ResolveInfo info = pm.resolveActivity(LaunchIntent, PackageManager.MATCH_DEFAULT_ONLY); mSongTitle = (String) info.loadLabel(pm); mElapsedTime = 0; if (pCommand.mode == 04) mRetriever.changeApp(false, mSongTitle, getString(R.string.advanced_remote), 999); mAlbumArtUri = "android.resource://" + launchApp + "/" + String.valueOf(info.getIconResource()); setUpAsForeground(mBanner); localBroadcast(false); } mLaunchFirstTime = false; } if (pCommand.mode == 02) { mPodStatus = podStat.SIMPLEREMOTE; switch (pCommand.command) { case RemoteRelease: respBytes = new byte[] { (byte) 0x01, 0x00, 0x00 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case RemotePlayPause: eventtime = SystemClock.uptimeMillis(); downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); break; case RemoteJustPlay: eventtime = SystemClock.uptimeMillis(); downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); break; case RemoteJustPause: eventtime = SystemClock.uptimeMillis(); downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); break; case RemoteSkipFwd: eventtime = SystemClock.uptimeMillis(); if (downEvent != null) { if (downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_NEXT || downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) { if ((eventtime - downEvent.getEventTime()) > 1000) { downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); if (mSimpleRemoteApp != null) upIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(upIntent, null); } } } else { downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); } break; case RemoteSkipRwd: eventtime = SystemClock.uptimeMillis(); if (downEvent != null) { if (downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PREVIOUS || downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_REWIND) { if ((eventtime - downEvent.getEventTime()) > 1000) { downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_REWIND, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_REWIND, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); if (mSimpleRemoteApp != null) upIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(upIntent, null); } } } else { downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); } break; case RemoteStop: eventtime = SystemClock.uptimeMillis(); downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); if (mSimpleRemoteApp != null) downIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(downIntent, null); break; case RemoteButtonRel: eventtime = SystemClock.uptimeMillis(); if (downIntent != null) { if (downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_NEXT || downEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PREVIOUS) sendOrderedBroadcast(downIntent, null); if (downEvent.getKeyCode() != KeyEvent.KEYCODE_MEDIA_FAST_FORWARD && downEvent.getKeyCode() != KeyEvent.KEYCODE_MEDIA_REWIND) { upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); upEvent = new KeyEvent(downEvent.getDownTime(), eventtime, KeyEvent.ACTION_UP, downEvent.getKeyCode(), 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); if (mSimpleRemoteApp != null) upIntent.setPackage(mSimpleRemoteApp); sendOrderedBroadcast(upIntent, null); } } downIntent = null; downEvent = null; upIntent = null; upEvent = null; break; default: break; } } else { switch (pCommand.command) { case AppCmd: byte[] appNameBytes = new byte[pCommand.params.length - 3]; System.arraycopy(pCommand.params, 2, appNameBytes, 0, appNameBytes.length); String appName = ""; try { appName = new String(appNameBytes, "UTF8"); Log.d("PodMode", "AppCmd " + appName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } break; case AppAck: break; case GetUpdateFlag: respBytes = new byte[] { 0x00, 0x0A, mUpdateFlag }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SetUpdateFlag: mUpdateFlag = pCommand.params[0]; respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SwitchToMainPlaylist: if (mPodStatus == podStat.ADVANCEDHACK) { mNowPlaying = 0; mPrevPlaying = 0; } else mNowPlaying = 0; mRetriever.switchToMainPlaylist(); respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SwitchToItem: int itemNo = 0; itemNo = pCommand.params[4] & 0xFF; itemNo += ((pCommand.params[3] & 0xFF) << 8); itemNo += ((pCommand.params[2] & 0xFF) << 16); itemNo += ((pCommand.params[1] & 0xFF) << 24); if ((mPodStatus == podStat.ADVANCEDHACK && mNotifyHack)) { mNotifyHack = false; } else { if (mRetriever.switchToItem((int) pCommand.params[0], itemNo)) { if (pCommand.params[0] == (byte) 0x05) { mNowPlaying = itemNo; tryToGetAudioFocus(); playNextSong(null); } } } respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetCountForType: respBytes = new byte[] { 0x00, 0x19, 0x00, 0x00, 0x00, 0x00 }; int num = mRetriever.getCountForType((int) pCommand.params[0]); respBytes[5] = (byte) (num & 0xFF); respBytes[4] = (byte) ((num >> 8) & 0xFF); respBytes[3] = (byte) ((num >> 16) & 0xFF); respBytes[2] = (byte) ((num >> 24) & 0xFF); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetItemNames: int startPos = 0; int count = 0; startPos = pCommand.params[4] & 0xFF; startPos += ((pCommand.params[3] & 0xFF) << 8); startPos += ((pCommand.params[2] & 0xFF) << 16); startPos += ((pCommand.params[1] & 0xFF) << 24); count = pCommand.params[8] & 0xFF; count += ((pCommand.params[7] & 0xFF) << 8); count += ((pCommand.params[6] & 0xFF) << 16); count += ((pCommand.params[5] & 0xFF) << 24); String[] itemNames = mRetriever.GetItemNames((int) pCommand.params[0], startPos, count); if (itemNames != null) { for (int i = 0; i < itemNames.length; i++) { byte[] part1 = { (byte) 0x00, (byte) 0x1B, (byte) (startPos >>> 24), (byte) (startPos >>> 16), (byte) (startPos >>> 8), (byte) startPos }; startPos++; respBytes = new String(new String(part1) + itemNames[i] + '\0').getBytes(); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); } } break; case GetTimeStatus: respBytes = new byte[] { 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; if (mState != State.Preparing && mState != State.Retrieving) { int trackLen = 0; if (mPlayer != null) trackLen = mPlayer.getDuration(); respBytes[2] = (byte) (trackLen >>> 24); respBytes[3] = (byte) (trackLen >>> 16); respBytes[4] = (byte) (trackLen >>> 8); respBytes[5] = (byte) trackLen; int elapsedTime = 0; if (mPlayer != null) elapsedTime = mPlayer.getCurrentPosition(); respBytes[6] = (byte) (elapsedTime >>> 24); respBytes[7] = (byte) (elapsedTime >>> 16); respBytes[8] = (byte) (elapsedTime >>> 8); respBytes[9] = (byte) elapsedTime; switch (mState) { case Stopped: respBytes[10] = (byte) 0x00; break; case Playing: respBytes[10] = (byte) 0x01; break; case Paused: respBytes[10] = (byte) 0x02; break; case Preparing: respBytes[10] = (byte) 0x01; break; case Retrieving: respBytes[10] = (byte) 0x01; break; } } pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetPlaylistPos: respBytes = new byte[] { 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00 }; respBytes[2] = (byte) ((mNowPlaying) >>> 24); respBytes[3] = (byte) ((mNowPlaying) >>> 16); respBytes[4] = (byte) ((mNowPlaying) >>> 8); respBytes[5] = (byte) mNowPlaying; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetSongTitle: byte[] part1 = new byte[] { 0x00, 0x21 }; int index; index = pCommand.params[3] & 0xFF; index += ((pCommand.params[2] & 0xFF) << 8); index += ((pCommand.params[1] & 0xFF) << 16); index += ((pCommand.params[0] & 0xFF) << 24); if (index == -1) index = 0; respBytes = new String(new String(part1) + mRetriever.getTrack(index).title + '\0').getBytes(); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetSongArtist: part1 = new byte[] { 0x00, 0x23 }; index = pCommand.params[3] & 0xFF; index += ((pCommand.params[2] & 0xFF) << 8); index += ((pCommand.params[1] & 0xFF) << 16); index += ((pCommand.params[0] & 0xFF) << 24); if (index == -1) index = 0; respBytes = new String(new String(part1) + mRetriever.getTrack(index).artist + '\0').getBytes(); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetSongAlbum: part1 = new byte[] { 0x00, 0x25 }; index = pCommand.params[3] & 0xFF; index += ((pCommand.params[2] & 0xFF) << 8); index += ((pCommand.params[1] & 0xFF) << 16); index += ((pCommand.params[0] & 0xFF) << 24); if (index == -1) index = 0; respBytes = new String(new String(part1) + mRetriever.getTrack(index).album + '\0').getBytes(); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case PollingMode: respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); mPollSpeed = (byte) pCommand.params[0]; if (pCommand.params[0] == (byte) 0x01 && mUpdateFlag != (byte) 0x01) { mUpdateFlag = pCommand.params[0]; if (mMediaChangeTimer == null) mMediaChangeTimer = new Timer(); mMediaChangeTimer.scheduleAtFixedRate(mMediaChangeTask, 0, 500); } else if (pCommand.params[0] == (byte) 0x00 && mUpdateFlag != (byte) 0x00) { mUpdateFlag = pCommand.params[0]; if (mMediaChangeTimer != null) mMediaChangeTimer.cancel(); } break; case ExecPlaylist: itemNo = pCommand.params[3] & 0xFF; itemNo += ((pCommand.params[2] & 0xFF) << 8); itemNo += ((pCommand.params[1] & 0xFF) << 16); itemNo += ((pCommand.params[0] & 0xFF) << 24); if (itemNo == -1) itemNo = 0; mRetriever.ExecPlaylist(); if (mPodShuffleMode == modeStat.Songs) mRetriever.shuffleTracks(); mNowPlaying = itemNo; tryToGetAudioFocus(); playNextSong(null); respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case PlaybackControl: switch (pCommand.params[0]) { case 0x01: // processStopRequest(); processTogglePlaybackRequest(); break; case 0x02: processStopRequest(); break; case 0x03: processPauseRequest(); processSkipRequest(); break; case 0x04: processPauseRequest(); processSkipRwdRequest(); break; case 0x05: // processSkipRequest(); break; case 0x06: // processRewindRequest(); break; case 0x07: // TODO Add Stop FF/RR function break; } respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetPlayListSongNum: respBytes = new byte[] { 0x00, 0x36, 0x00, 0x00, 0x00, 0x00 }; num = mRetriever.getCount(); respBytes[5] = (byte) (num & 0xFF); respBytes[4] = (byte) ((num >> 8) & 0xFF); respBytes[3] = (byte) ((num >> 16) & 0xFF); respBytes[2] = (byte) ((num >> 24) & 0xFF); pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case JumpToSong: itemNo = pCommand.params[3] & 0xFF; itemNo += ((pCommand.params[2] & 0xFF) << 8); itemNo += ((pCommand.params[1] & 0xFF) << 16); itemNo += ((pCommand.params[0] & 0xFF) << 24); mNowPlaying = itemNo; tryToGetAudioFocus(); playNextSong(null); respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case FrankPlaylist: respBytes = new byte[] { 0x00, 0x4F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; num = mRetriever.getPlaylistNum(); if (num != -1) { respBytes[5] = (byte) (num & 0xFF); respBytes[4] = (byte) ((num >> 8) & 0xFF); respBytes[3] = (byte) ((num >> 16) & 0xFF); respBytes[2] = (byte) ((num >> 24) & 0xFF); } pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case StartID: respBytes = new byte[] { 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetPodProtocols: // start respBytes = new byte[] { 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); if (pCommand.rawBytes[13] == (byte) 0x00 && pCommand.rawBytes[14] == (byte) 0x00 && pCommand.rawBytes[15] == (byte) 0x00 && pCommand.rawBytes[16] == (byte) 0x00) { // respBytes = new byte[] { 0x00 }; // pResponse = new podResponse(pCommand, respBytes); // serialWrite(pResponse.getBytes()); } else { respBytes = new byte[] { 0x14 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); } break; case DeviceAuthInfo: if (pCommand.length == 4) { respBytes = new byte[] { 0x16 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); respBytes = new byte[] { 0x17, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); } else { if (pCommand.rawBytes[7] != pCommand.rawBytes[8]) { respBytes = new byte[] { 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); } else { respBytes = new byte[] { 0x16 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); respBytes = new byte[] { 0x17, 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); } } break; case DeviceAuthSig: respBytes = new byte[] { 0x19 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetPodOptions: // start if (pCommand.rawBytes[5] == 0x00) respBytes = new byte[] { 0x4C }; else respBytes = new byte[] { (byte) 0x02, 0x04 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetPodOption: // start respBytes = new byte[] { 0x25 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SetIdTokens: respBytes = new byte[] { 0x3A }; pResponse = new podResponse(pCommand, respBytes); if (mAccessoryName == null) { mAccessoryName = pResponse.accessoryName; mAccessoryMnf = pResponse.accessoryMnf; mAccessoryModel = pResponse.accessoryModel; mHTTPsend = true; } serialWrite(pResponse.getBytes()); break; case EndID: respBytes = new byte[] { 0x3C }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); respBytes = new byte[] { 0x14 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetProtoVersion: respBytes = new byte[] { 0x0F }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case DeviceDetails: respBytes = new byte[] { 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case DevName: respBytes = new byte[] { 0x00, 0x15, 0x50, 0x6F, 0x64, 0x4D, 0x6F, 0x64, 0x65, 0x00 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case DevTypeSize: pResponse = new podResponse(pCommand, DEVTYPESIZE); serialWrite(pResponse.getBytes()); break; case StateInfo: respBytes = new byte[] { 0x0D }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case RemoteNotify: respBytes = new byte[] { 0x02 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SwitchRemote: mPodStatus = podStat.SIMPLEREMOTE; break; case ReqAdvRemote: respBytes = new byte[] { 0x04, 0x00 }; if (mPodStatus == podStat.ADVANCEDREMOTE || mPodStatus == podStat.ADVANCEDHACK) respBytes[1] = 0x04; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case StartAdvRemote: mPodStatus = podStat.ADVANCEDREMOTE; if (!mAdvancedRemoteApp.equals(PACKAGENAME)) mPodStatus = podStat.ADVANCEDHACK; respBytes = new byte[] { 0x2 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case EndAdvRemote: mPodStatus = podStat.WAITING; respBytes = new byte[] { 0x2 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetSoftVersion: respBytes = new byte[] { 0x0A }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetSerialNum: respBytes = new byte[] { 0x0C }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case DevModel: respBytes = new byte[] { 0x0E }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SwitchAdvanced: mPodStatus = podStat.ADVANCEDREMOTE; if (!mAdvancedRemoteApp.equals(PACKAGENAME)) mPodStatus = podStat.ADVANCEDHACK; break; case SetRepeatMode: if (pCommand.params[0] == (byte) 0x00) mPodRepeatMode = modeStat.Off; else if (pCommand.params[0] == (byte) 0x01) mPodRepeatMode = modeStat.Songs; else mPodRepeatMode = modeStat.Albums; respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetRepeatMode: respBytes = new byte[] { 0x00, 0x30, 0x00 }; if (mPodRepeatMode == modeStat.Songs) respBytes[2] = 0x01; if (mPodRepeatMode == modeStat.Albums) respBytes[2] = 0x02; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case SetShuffleMode: if (pCommand.params[0] == (byte) 0x00) mPodShuffleMode = modeStat.Off; else if (pCommand.params[0] == (byte) 0x01) mPodShuffleMode = modeStat.Songs; else mPodShuffleMode = modeStat.Albums; respBytes = new byte[] { 0x00, 0x01 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetShuffleMode: respBytes = new byte[] { 0x00, 0x2D, 0x00 }; if (mPodShuffleMode == modeStat.Songs) respBytes[2] = 0x01; if (mPodShuffleMode == modeStat.Albums) respBytes[2] = 0x02; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; case GetScreenSize: respBytes = new byte[] { 0x00, 0x34 }; pResponse = new podResponse(pCommand, respBytes); serialWrite(pResponse.getBytes()); break; default: break; } } } }
From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java
@SuppressLint("NewApi") public void setUpRemoteControlClient() { if (!Util.isLockScreenEnabled(this)) return;// w ww .j av a 2 s . c o m ComponentName componentName = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()); if (remoteControlClient == null) { final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(componentName); PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteControlClient = new RemoteControlClient(broadcast); audioManager.registerRemoteControlClient(remoteControlClient); // Flags for the media transport control that this client supports. int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE; remoteControlClient .setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() { @Override public long onGetPlaybackPosition() { return mediaPlayer.getCurrentPosition(); } }); remoteControlClient.setPlaybackPositionUpdateListener( new RemoteControlClient.OnPlaybackPositionUpdateListener() { @Override public void onPlaybackPositionUpdate(long newPositionMs) { seekTo((int) newPositionMs); } }); } remoteControlClient.setTransportControlFlags(flags); } }
From source file:com.rks.musicx.services.MusicXService.java
@Override public void mediaLockscreen() { mediaSessionLockscreen = new MediaSessionCompat(this, TAG); mediaSessionLockscreen.setCallback(new MediaSessionCompat.Callback() { @Override/*from ww w . ja v a 2 s .c o m*/ 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); }