Example usage for android.media.audiofx AudioEffect EXTRA_AUDIO_SESSION

List of usage examples for android.media.audiofx AudioEffect EXTRA_AUDIO_SESSION

Introduction

In this page you can find the example usage for android.media.audiofx AudioEffect EXTRA_AUDIO_SESSION.

Prototype

String EXTRA_AUDIO_SESSION

To view the source code for android.media.audiofx AudioEffect EXTRA_AUDIO_SESSION.

Click Source Link

Document

Contains the ID of the audio session the effects should be applied to.

Usage

From source file:github.daneren2005.dsub.service.DownloadServiceImpl.java

@Override
public void onCreate() {
    super.onCreate();

    new Thread(new Runnable() {
        public void run() {
            Looper.prepare();/*from   w w w  .j  av  a 2s . c o  m*/

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setWakeMode(DownloadServiceImpl.this, PowerManager.PARTIAL_WAKE_LOCK);

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
                    handleError(new Exception("MediaPlayer error: " + what + " (" + more + ")"));
                    return false;
                }
            });

            try {
                Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
                i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getAudioSessionId());
                i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
                sendBroadcast(i);
            } catch (Throwable e) {
                // Froyo or lower
            }

            mediaPlayerLooper = Looper.myLooper();
            mediaPlayerHandler = new Handler(mediaPlayerLooper);
            Looper.loop();
        }
    }).start();

    Util.registerMediaButtonEventReceiver(this);

    if (mRemoteControl == null) {
        // Use the remote control APIs (if available) to set the playback state
        mRemoteControl = RemoteControlClientHelper.createInstance();
        ComponentName mediaButtonReceiverComponent = new ComponentName(getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        mRemoteControl.register(this, mediaButtonReceiverComponent);
    }

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.setReferenceCounted(false);

    SharedPreferences prefs = Util.getPreferences(this);
    try {
        timerDuration = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION, "5"));
    } catch (Throwable e) {
        timerDuration = 5;
    }
    sleepTimer = null;

    keepScreenOn = prefs.getBoolean(Constants.PREFERENCES_KEY_KEEP_SCREEN_ON, false);

    instance = this;
    lifecycleSupport.onCreate();

    if (prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false)) {
        getEqualizerController();
    }
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@SuppressLint("NewApi")
@Override//from   ww w. ja va2s  .  com
public void onCreate() {
    super.onCreate();

    new Thread(new Runnable() {
        @Override
        public void run() {
            Thread.currentThread().setName("DownloadServiceImpl");

            Looper.prepare();

            if (mediaPlayer != null) {
                mediaPlayer.release();
            }

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setWakeMode(DownloadServiceImpl.this, PowerManager.PARTIAL_WAKE_LOCK);

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
                    handleError(new Exception(String.format("MediaPlayer error: %d (%d)", what, more)));
                    return false;
                }
            });

            try {
                Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
                i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getAudioSessionId());
                i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
                sendBroadcast(i);
            } catch (Throwable e) {
                // Froyo or lower
            }

            mediaPlayerLooper = Looper.myLooper();
            mediaPlayerHandler = new Handler(mediaPlayerLooper);
            Looper.loop();
        }
    }).start();

    audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    setUpRemoteControlClient();

    if (equalizerAvailable) {
        equalizerController = new EqualizerController(this, mediaPlayer);
        if (!equalizerController.isAvailable()) {
            equalizerController = null;
        } else {
            equalizerController.loadSettings();
        }
    }
    if (visualizerAvailable) {
        visualizerController = new VisualizerController(mediaPlayer);
        if (!visualizerController.isAvailable()) {
            visualizerController = null;
        }
    }

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.setReferenceCounted(false);

    instance = this;
    lifecycleSupport.onCreate();
}

From source file:github.daneren2005.dsub.service.DownloadServiceImpl.java

@Override
public void onDestroy() {
    super.onDestroy();
    instance = null;// w  w w .java  2s  .  c o  m

    if (currentPlaying != null)
        currentPlaying.setPlaying(false);
    if (sleepTimer != null) {
        sleepTimer.cancel();
        sleepTimer.purge();
    }
    lifecycleSupport.onDestroy();

    try {
        Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
        i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getAudioSessionId());
        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();
    if (equalizerController != null) {
        equalizerController.release();
    }
    if (visualizerController != null) {
        visualizerController.release();
    }
    if (mRemoteControl != null) {
        mRemoteControl.unregister(this);
        mRemoteControl = null;
    }

    if (bufferTask != null) {
        bufferTask.cancel();
    }
    if (nextPlayingTask != null) {
        nextPlayingTask.cancel();
    }
    Util.hidePlayingNotification(this, this, handler);
}

From source file:github.daneren2005.dsub.service.DownloadService.java

@Override
public void onCreate() {
    super.onCreate();

    final SharedPreferences prefs = Util.getPreferences(this);
    new Thread(new Runnable() {
        public void run() {
            Looper.prepare();/*  w ww. j a  v a2s. com*/

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setWakeMode(DownloadService.this, PowerManager.PARTIAL_WAKE_LOCK);

            audioSessionId = -1;
            Integer id = prefs.getInt(Constants.CACHE_AUDIO_SESSION_ID, -1);
            if (id != -1) {
                try {
                    audioSessionId = id;
                    mediaPlayer.setAudioSessionId(audioSessionId);
                } catch (Throwable e) {
                    audioSessionId = -1;
                }
            }

            if (audioSessionId == -1) {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    audioSessionId = mediaPlayer.getAudioSessionId();
                    prefs.edit().putInt(Constants.CACHE_AUDIO_SESSION_ID, audioSessionId).commit();
                } catch (Throwable t) {
                    // Froyo or lower
                }
            }

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
                    handleError(new Exception("MediaPlayer error: " + what + " (" + more + ")"));
                    return false;
                }
            });

            try {
                Intent i = new Intent(AudioEffect.ACTION_OPEN_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
            }

            effectsController = new AudioEffectsController(DownloadService.this, audioSessionId);
            if (prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false)) {
                getEqualizerController();
            }

            mediaPlayerLooper = Looper.myLooper();
            mediaPlayerHandler = new Handler(mediaPlayerLooper);

            if (runListenersOnInit) {
                onSongsChanged();
                onSongProgress();
                onStateUpdate();
            }

            Looper.loop();
        }
    }, "DownloadService").start();

    Util.registerMediaButtonEventReceiver(this);

    if (mRemoteControl == null) {
        // Use the remote control APIs (if available) to set the playback state
        mRemoteControl = RemoteControlClientHelper.createInstance();
        ComponentName mediaButtonReceiverComponent = new ComponentName(getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        mRemoteControl.register(this, mediaButtonReceiverComponent);
    }

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.setReferenceCounted(false);

    try {
        timerDuration = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION, "5"));
    } catch (Throwable e) {
        timerDuration = 5;
    }
    sleepTimer = null;

    keepScreenOn = prefs.getBoolean(Constants.PREFERENCES_KEY_KEEP_SCREEN_ON, false);

    mediaRouter = new MediaRouteManager(this);

    instance = this;
    shufflePlayBuffer = new ShufflePlayBuffer(this);
    artistRadioBuffer = new ArtistRadioBuffer(this);
    lifecycleSupport.onCreate();
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@Override
public void onDestroy() {
    super.onDestroy();

    try {/*from w ww  . ja va  2 s.  com*/
        instance = null;
        lifecycleSupport.onDestroy();
        mediaPlayer.release();

        if (nextMediaPlayer != null) {
            nextMediaPlayer.release();
        }

        mediaPlayerLooper.quit();
        shufflePlayBuffer.shutdown();

        if (equalizerController != null) {
            equalizerController.release();
        }

        if (visualizerController != null) {
            visualizerController.release();
        }

        if (bufferTask != null) {
            bufferTask.cancel();
        }

        if (nextPlayingTask != null) {
            nextPlayingTask.cancel();
        }

        Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
        i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getAudioSessionId());
        i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
        sendBroadcast(i);

        audioManager.unregisterRemoteControlClient(remoteControlClient);
        clearRemoteControl();

        wakeLock.release();
    } catch (Throwable ignored) {
    }
}

From source file:com.rks.musicx.ui.activities.MainActivity.java

@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();// w w w . j  av  a 2 s .c  om
        } 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);
}

From source file:github.daneren2005.dsub.service.DownloadService.java

@Override
public void onDestroy() {
    super.onDestroy();
    instance = null;//from  w ww.j av  a2  s.  co  m

    if (currentPlaying != null)
        currentPlaying.setPlaying(false);
    if (sleepTimer != null) {
        sleepTimer.cancel();
        sleepTimer.purge();
    }
    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 (mRemoteControl != null) {
        mRemoteControl.unregister(this);
        mRemoteControl = null;
    }

    if (bufferTask != null) {
        bufferTask.cancel();
        bufferTask = null;
    }
    if (nextPlayingTask != null) {
        nextPlayingTask.cancel();
        nextPlayingTask = null;
    }
    if (remoteController != null) {
        remoteController.stop();
        remoteController.shutdown();
    }
    if (proxy != null) {
        proxy.stop();
        proxy = null;
    }
    mediaRouter.destroy();
    Notifications.hidePlayingNotification(this, this, handler);
    Notifications.hideDownloadingNotification(this, this, handler);
}

From source file:org.mariotaku.harmony.activity.MusicPlaybackActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mService == null)
        return true;
    switch (item.getItemId()) {
    case MENU_ADD_TO_PLAYLIST: {
        final Intent intent = new Intent(INTENT_ADD_TO_PLAYLIST);
        long[] list_to_be_added = new long[1];
        //list_to_be_added[0] = MusicUtils.getCurrentAudioId();
        intent.putExtra(INTENT_KEY_LIST, list_to_be_added);
        startActivity(intent);//from w ww .  j  a  va  2  s  .c  o m
        break;
    }
    case EQUALIZER: {
        final Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
        intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
        intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mService.getAudioSessionId());
        intent.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC);
        startActivityForResult(intent, REQUEST_EQUALIZER);
        break;
    }
    case MENU_SLEEP_TIMER: {
        final Intent intent = new Intent(INTENT_SLEEP_TIMER);
        startActivity(intent);
        break;
    }
    case DELETE_ITEMS: {
        final Intent intent = new Intent(INTENT_DELETE_ITEMS);
        Bundle bundle = new Bundle();
        //            bundle.putString(
        //                  INTENT_KEY_PATH,
        //                  Uri.withAppendedPath(Audio.Media.EXTERNAL_CONTENT_URI,
        //                        Uri.encode(String.valueOf(MusicUtils.getCurrentAudioId())))
        //                        .toString());
        intent.putExtras(bundle);
        startActivity(intent);
        break;
    }
    case SETTINGS: {
        final Intent intent = new Intent(INTENT_APPEARANCE_SETTINGS);
        startActivity(intent);
        break;
    }
    case MENU_HOME: {
        overridePendingTransition(R.anim.music_playback_activity_enter, R.anim.music_playback_activity_exit);
        NavUtils.navigateUpTo(this, new Intent(this, MusicBrowserActivity.class));
        break;
    }
    case ADD_TO_FAVORITES: {
        toggleFavorite();
        break;
    }
    case MENU_SHUFFLE_MODE_NONE: {
        mService.setShuffleMode(SHUFFLE_MODE_NONE);
        break;
    }
    case MENU_SHUFFLE_MODE_ALL: {
        mService.setShuffleMode(SHUFFLE_MODE_ALL);
        break;
    }
    case MENU_REPEAT_MODE_NONE: {
        mService.setRepeatMode(REPEAT_MODE_NONE);
        break;
    }
    case MENU_REPEAT_MODE_ALL: {
        mService.setRepeatMode(REPEAT_MODE_ALL);
        break;
    }
    case MENU_REPEAT_MODE_CURRENT: {
        mService.setRepeatMode(REPEAT_MODE_CURRENT);
        break;
    }
    }
    return true;
}

From source file:com.naman14.timber.musicplayer.MusicService.java

@Override
public void onDestroy() {
    if (D)/*from  w  w  w.j  ava2 s  .  c o  m*/
        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();
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MediaPlaybackService.java

@Override
public void onDestroy() {
    // Check that we're not being destroyed while something is still playing.
    if (isPlaying()) {
        Log.e(LOGTAG, "Service being destroyed while still playing.");
    }/*ww  w.jav a  2 s.c  om*/
    // 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;
    }

    unregisterReceiver(mIntentReceiver);
    if (mUnmountReceiver != null) {
        unregisterReceiver(mUnmountReceiver);
        mUnmountReceiver = null;
    }
    mWakeLock.release();
    super.onDestroy();
}