@Override public void onDestroy() { if (D) Logger.d(TAG, "Destroying service"); super.onDestroy(); final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(audioEffectsIntent); mAlarmManager.cancel(mShutdownIntent); mPlayerHandler.removeCallbacksAndMessages(null); mPlayer.release(); mPlayer = null; mAudioManager.abandonAudioFocus(mAudioFocusListener); mMediaSession.release(); mPlayerHandler.removeCallbacksAndMessages(null); unregisterReceiver(mIntentReceiver); mWakeLock.release(); }
public void pause() { if (D) Log.d(TAG, "Pausing playback"); synchronized (this) { mPlayerHandler.removeMessages(FADEUP); if (mIsSupposedToBePlaying) { final Intent intent = new Intent( AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(intent); mPlayer.pause(); notifyChange(META_CHANGED); setIsSupposedToBePlaying(false, true); } } }
/** * Temporarily pauses playback. */ public void pause() { if (mPlayerHandler == null) return; LogUtils.d(TAG, "Pausing playback"); synchronized (this) { if (mPlayerHandler != null) { mPlayerHandler.removeMessages(MusicServiceConstants.FADEUP); } if (mIsSupposedToBePlaying) { final Intent intent = new Intent( AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(intent); if (mPlayer != null) { mPlayer.pause(); } setIsSupposedToBePlaying(false, true); stopShakeDetector(false); } } }
public static void openEqualizer(@NonNull final Activity activity) { final int sessionId = MusicPlayerRemote.getAudioSessionId(); if (sessionId == AudioEffect.ERROR_BAD_VALUE) { Toast.makeText(activity, activity.getResources().getString(R.string.no_audio_ID), Toast.LENGTH_LONG).show(); } else { try { final Intent effects = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); effects.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, sessionId); effects.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC); activity.startActivityForResult(effects, 0); } catch (@NonNull final ActivityNotFoundException notFound) { Toast.makeText(activity, activity.getResources().getString(R.string.no_equalizer), Toast.LENGTH_SHORT).show(); } } }
private void startEffectsPanel() { try { final Intent effects = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getContext().getPackageName()); effects.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, MusicUtils.getAudioSessionId()); effects.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC); startActivityForResult(effects, REQUEST_EQ); } catch (final ActivityNotFoundException ignored) { Toast.makeText(getActivity(), "No system equalizer found", Toast.LENGTH_SHORT).show(); } }
/** * @param path The path of the file, or the http/rtsp URL of the stream * you want to play * return True if the <code>player</code> has been prepared and is * ready to play, false otherwise */ void setDataSource(final String path) { Logger.d(TAG, "setDataSourceImpl, path: " + path); try { mCurrentMediaPlayer.reset(); mCurrentMediaPlayer.setOnPreparedListener(this); mCurrentMediaPlayer.setDataSource(path); mCurrentMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mCurrentMediaPlayer.prepareAsync(); preparing = true; mService.get().mIsSupposedToBePlaying = false; } catch (final IOException e) { e.printStackTrace(); } mCurrentMediaPlayer.setOnCompletionListener(this); mCurrentMediaPlayer.setOnErrorListener(this); mCurrentMediaPlayer.setOnBufferingUpdateListener(this); final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, mService.get().getPackageName()); mService.get().sendBroadcast(intent); //return true; resetBufferPercent(); mService.get().notifyChange(PLAYSTATE_CHANGED); }
/** * @param player The {@link MediaPlayer} to use * @param path The path of the file, or the http/rtsp URL of the stream * you want to play * @return True if the <code>player</code> has been prepared and is * ready to play, false otherwise */ private boolean setDataSourceImpl(@NonNull final MediaPlayer player, @NonNull final String path) { if (context == null) { return false; } try { player.reset(); player.setOnPreparedListener(null); if (path.startsWith("content://")) { player.setDataSource(context, Uri.parse(path)); } else { player.setDataSource(path); } player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.prepare(); } catch (Exception e) { return false; } player.setOnCompletionListener(this); player.setOnErrorListener(this); final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, context.getPackageName()); intent.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC); context.sendBroadcast(intent); return true; }
@TargetApi(18) private static boolean isAcousticEchoCancelerExcludedByUUID() { for (Descriptor d : getAvailableEffects()) { if (d.type.equals(AudioEffect.EFFECT_TYPE_AEC) && d.uuid.equals(AOSP_ACOUSTIC_ECHO_CANCELER)) { return true; } } return false; }
@TargetApi(18) private static boolean isNoiseSuppressorExcludedByUUID() { for (Descriptor d : getAvailableEffects()) { if (d.type.equals(AudioEffect.EFFECT_TYPE_NS) && d.uuid.equals(AOSP_NOISE_SUPPRESSOR)) { return true; } } return false; }
@TargetApi(18) private boolean effectTypeIsVoIP(UUID type) { if (!WebRtcAudioUtils.runningOnJellyBeanMR2OrHigher()) return false; return (AudioEffect.EFFECT_TYPE_AEC.equals(type) && isAcousticEchoCancelerSupported()) || (AudioEffect.EFFECT_TYPE_NS.equals(type) && isNoiseSuppressorSupported()); }
private static Descriptor[] getAvailableEffects() { if (cachedEffects != null) { return cachedEffects; } // The caching is best effort only - if this method is called from several // threads in parallel, they may end up doing the underlying OS call // multiple times. It's normally only called on one thread so there's no // real need to optimize for the multiple threads case. cachedEffects = AudioEffect.queryEffects(); return cachedEffects; }
@Override public AudioEffect create(int audioSession) { try { return new LoudnessEnhancer(audioSession); } catch (RuntimeException e) { // NOTE: some devices doesn't support LoudnessEnhancer class and may throw an exception // (ME176C throws IllegalArgumentException) Log.w(TAG, "Failed to instantiate loudness enhancer class", e); } return null; }
@Override public void onDestroy() { super.onDestroy(); Extras.getInstance().setwidgetPosition(100); audioWidget.cleanUp(); audioWidget = null; Equalizers.EndEq(); BassBoosts.EndBass(); Virtualizers.EndVirtual(); Loud.EndLoudnessEnhancer(); Reverb.EndReverb(); receiverCleanup(); Extras.getInstance().eqSwitch(false); audioManager.abandonAudioFocus(this); removeProgress(); fastplay = false; isPlaying = false; paused = false; stopMediaplayer(); if (!Extras.getInstance().hideLockscreen()) { if (mediaSessionLockscreen != null) { mediaSessionLockscreen.release(); mediaSessionLockscreen = null; } } Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); if (Helper.isActivityPresent(this, i)) { i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSession()); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, this.getPackageName()); sendBroadcast(i); } else { Log.d(TAG, "no activity found"); } if (!Extras.getInstance().hideNotify()) { removeNotification(); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: FragmentManager fm = getSupportFragmentManager(); if (fm.getBackStackEntryCount() > 0) { fm.popBackStack(); } else { fragmentLoader(setContainerId(), setFragment()); } return true; case R.id.system_eq: Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); if (intent.getAction() != null && Helper.isActivityPresent(MainActivity.this, intent)){ intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, musicXService.audioSession()); startActivityForResult(intent, EQ); }else { Toast.makeText(this, "No app found to handle equalizer", Toast.LENGTH_SHORT).show(); } break; case R.id.play_save_queue: multiQueuePlay(); break; } return super.onOptionsItemSelected(item); }
@Override public void onDestroy() { // Check that we're not being destroyed while something is still // playing. if (chatHead != null) windowManager.removeView(chatHead); if (isPlaying()) { Log.e(LOGTAG, "Service being destroyed while still playing."); } if (mShakeAction != 0) mSensorManager.unregisterListener(this); // release all MediaPlayer resources, including the native player and // wakelocks Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(i); pause(); mPlayer.release(); mPlayer = null; mAudioManager.abandonAudioFocus(mAudioFocusListener); setRemoteControlClient(); // make sure there aren't any other messages coming mDelayedStopHandler.removeCallbacksAndMessages(null); mCurrentMediaPlayerHandler.removeCallbacksAndMessages(null); unregisterReceiver(mIntentReceiver); dismissAllNotifications(); super.onDestroy(); }
public void setDataSource(String path) { try { mOnlineDuration = 0; mCurrentMediaPlayer.reset(); if (path.startsWith("content://")) { mCurrentMediaPlayer.setDataSource( MediaPlaybackService.this, Uri.parse(path)); } else { mCurrentMediaPlayer.setDataSource(path); } mCurrentMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mCurrentMediaPlayer.prepareAsync(); mCurrentMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mOnlineDuration = mCurrentMediaPlayer.getDuration(); mp.setOnCompletionListener(listener); mp.setOnErrorListener(errorListener); setOnInfoListener(); Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(i); mIsInitialized = true; notifyChange(META_CHANGED); notifyChange(END_BUFFERING); } }); } catch (IOException ex) { mIsInitialized = false; return; } }
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.starred, menu); MenuInflater inflater2 = getMenuInflater(); inflater2.inflate(R.menu.share, menu); Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); if (getPackageManager().resolveActivity(i, 0) != null) { MenuInflater inflater1 = getMenuInflater(); inflater1.inflate(R.menu.equalizer, menu); } MenuInflater inflater3 = getMenuInflater(); inflater3.inflate(R.menu.settings, menu); if (!MusicUtils.getBooleanPref(this, "radiomode", false)) { SubMenu menu1 = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0, R.string.add_to_playlist); if (!MusicUtils.getBooleanPref(this, "radiomode", false)) { MusicUtils.makePlaylistMenuOnline(this, menu1); } menu.add(1, SEARCH_LYRICS, 0, R.string.search_lyrics_menu_short); } menu.add(1, ABOUT + 2, 0, R.string.carmode_menu_short); if (mSettings.getBoolean(PreferencesActivity.POPUP_ON, false)) menu.add(1, ABOUT + 1, 0, R.string.go_popup); menu.add(1, ABOUT, 0, R.string.about_menu_short); menu.add(1, EXIT, 0, R.string.exit_menu); return true; }
@Override public void onDestroy() { if (D) Log.d(TAG, "Destroying service"); super.onDestroy(); // Remove any sound effects final Intent audioEffectsIntent = new Intent( AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(audioEffectsIntent); mAlarmManager.cancel(mShutdownIntent); mPlayerHandler.removeCallbacksAndMessages(null); if (TimberUtils.isJellyBeanMR2()) mHandlerThread.quitSafely(); else mHandlerThread.quit(); mPlayer.release(); mPlayer = null; mAudioManager.abandonAudioFocus(mAudioFocusListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.release(); getContentResolver().unregisterContentObserver(mMediaStoreObserver); closeCursor(); unregisterReceiver(mIntentReceiver); if (mUnmountReceiver != null) { unregisterReceiver(mUnmountReceiver); mUnmountReceiver = null; } mWakeLock.release(); }
@Override public void onDestroy() { super.onDestroy(); instance = null; if(currentPlaying != null) currentPlaying.setPlaying(false); lifecycleSupport.onDestroy(); try { Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(i); } catch(Throwable e) { // Froyo or lower } mediaPlayer.release(); if(nextMediaPlayer != null) { nextMediaPlayer.release(); } mediaPlayerLooper.quit(); shufflePlayBuffer.shutdown(); effectsController.release(); if(bufferTask != null) { bufferTask.cancel(); bufferTask = null; } if(nextPlayingTask != null) { nextPlayingTask.cancel(); nextPlayingTask = null; } if(proxy != null) { proxy.stop(); proxy = null; } Notifications.hidePlayingNotification(this, this, handler); Notifications.hideDownloadingNotification(this, this, handler); }
/** * Respond to clicks on actionbar options */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: onSearchRequested(); break; case R.id.action_settings: startActivityForResult(new Intent(this, SettingsHolder.class),0); break; case R.id.action_eqalizer: final Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); if (getPackageManager().resolveActivity(intent, 0) == null) { startActivity(new Intent(this, SimpleEq.class)); } else{ intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, MusicUtils.getCurrentAudioId()); startActivity(intent); } break; case R.id.action_shuffle_all: shuffleAll(); break; default: return super.onOptionsItemSelected(item); } return true; }
@Override public void onDestroy() { // Check that we're not being destroyed while something is still // playing. if (mIsSupposedToBePlaying) { Log.e(LOGTAG, "Service being destroyed while still playing."); } // release all MediaPlayer resources, including the native player and // wakelocks Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(i); mPlayer.release(); mPlayer = null; mAudioManager.abandonAudioFocus(mAudioFocusListener); mAudioManager.unregisterRemoteControlClient(mRemoteControlClient); // make sure there aren't any other messages coming mDelayedStopHandler.removeCallbacksAndMessages(null); mMediaplayerHandler.removeCallbacksAndMessages(null); if (mCursor != null) { mCursor.close(); mCursor = null; } updateAlbumBitmap(); unregisterReceiver(mIntentReceiver); if (mUnmountReceiver != null) { unregisterReceiver(mUnmountReceiver); mUnmountReceiver = null; } mWakeLock.release(); super.onDestroy(); }
/** * Stops media playback */ public synchronized void stop() { // Check if a player exists otherwise there is nothing to do. if (mCurrentMediaPlayer != null) { // Check if the player for the next song exists already if (mNextMediaPlayer != null) { // Remove the next player from the currently playing one. mCurrentMediaPlayer.setNextMediaPlayer(null); // Release the MediaPlayer, not usable after this command mNextMediaPlayer.release(); // Reset variables to clean internal state mNextMediaPlayer = null; mSecondPrepared = false; mSecondPreparing = false; } // Check if the currently active player is ready if (mCurrentPrepared) { /* * Signal android desire to close audio effect session */ Intent audioEffectIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); audioEffectIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mCurrentMediaPlayer.getAudioSessionId()); audioEffectIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, mPlaybackService.getPackageName()); mPlaybackService.sendBroadcast(audioEffectIntent); Log.v(TAG, "Closing effect for session: " + mCurrentMediaPlayer.getAudioSessionId()); } // Release the current player mCurrentMediaPlayer.release(); // Reset variables to clean internal state mCurrentMediaPlayer = null; mCurrentPrepared = false; } }
private void dumpAudioEffectsState() { AudioEffect.Descriptor effects[] = AudioEffect.queryEffects(); Log.v(TAG,"Found audio effects: " + effects.length); for(AudioEffect.Descriptor effect : effects) { Log.v(TAG,"AudioEffect: " + effect.name + " connect mode: " + effect.connectMode + " implementor: " + effect.implementor); } }
/** @see android.media.audiofx.AudioEffect.Descriptor#uuid */ protected boolean hasFeature(UUID uuid) { // Build of array of UUIDs to search through. if (null == uuids || uuids.length == 0) { uuids = new UUID[descriptors.length]; int i = -1; for (AudioEffect.Descriptor descriptor : descriptors) uuids[++i] = descriptor.uuid; } return (Arrays.binarySearch(uuids, uuid) >= 0); }
/** @see android.media.audiofx.AudioEffect.Descriptor#name */ protected String[] getDescriptorNames() { // Build of array of UUIDs to search through. String[] names = new String[descriptors.length]; int i = -1; for (AudioEffect.Descriptor descriptor : descriptors) names[++i] = descriptor.name; return names; }
private void release(AudioEffect effect) { if (null != effect) { effect.setControlStatusListener(null); effect.setEnableStatusListener(null); if (effect instanceof Equalizer) { ((Equalizer) effect).setParameterListener(null); } else if (effect instanceof BassBoost) { ((BassBoost) effect).setParameterListener(null); } else if (effect instanceof Virtualizer) { ((Virtualizer) effect).setParameterListener(null); } effect.release(); } }
public static Intent getSystemEqIntent(Context c) { Intent systemEq = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); systemEq.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, c.getPackageName()); ActivityInfo info = systemEq.resolveActivityInfo(c.getPackageManager(), 0); if (info != null && !info.name.startsWith("com.android.musicfx")) { return systemEq; } else { return null; } }
/** * Checks whether the current device is capable of instantiating and using an * {@link android.media.audiofx.Equalizer} * @return True if an Equalizer may be used at runtime */ public static boolean hasEqualizer() { for (AudioEffect.Descriptor effect : AudioEffect.queryEffects()) { if (EQUALIZER_UUID.equals(effect.type)) { return true; } } return false; }
private void sendPlayingStatusChangedBroadcast(boolean isPlaying){ if(isPlaying){ final Intent audioEffectIntent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); audioEffectIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.mediaPlayer.getAudioSessionId()); audioEffectIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(audioEffectIntent); } buildNotification(); Intent statusChangedIntent = new Intent(); statusChangedIntent.setAction(ACTION_PLAYING_STATUS_CHANGED); statusChangedIntent.putExtra(RadioPlaybackService.KEY_IS_PLAYING, isPlaying); sendBroadcast(statusChangedIntent); }
/** * Respond to clicks on actionbar options */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: onSearchRequested(); break; case R.id.action_settings: startActivityForResult(new Intent(this, SettingsHolder.class),0); break; case R.id.action_eqalizer: Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, MusicUtils.getCurrentAudioId()); startActivityForResult(i, 0); break; case R.id.action_shuffle_all: shuffleAll(); break; default: return super.onOptionsItemSelected(item); } return true; }