Search in sources :

Example 1 with AudioFocusRequest

use of android.media.AudioFocusRequest in project Shuttle by timusus.

the class MusicService method play.

/**
 * Starts playback of a previously opened file.
 */
public void play() {
    int status;
    if (ShuttleUtils.hasOreo()) {
        AudioFocusRequest audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).setOnAudioFocusChangeListener(audioFocusListener).setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build()).build();
        this.audioFocusRequest = audioFocusRequest;
        status = audioManager.requestAudioFocus(audioFocusRequest);
    } else {
        status = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }
    if (playbackLocation == LOCAL) {
        if (SettingsManager.getInstance().getEqualizerEnabled()) {
            // Shutdown any existing external audio sessions
            equalizer.closeEqualizerSessions(false, getAudioSessionId());
            // Start internal equalizer session (will only turn on if enabled)
            equalizer.openEqualizerSession(true, getAudioSessionId());
        } else {
            equalizer.openEqualizerSession(false, getAudioSessionId());
        }
    }
    if (mediaSession != null && !mediaSession.isActive()) {
        try {
            mediaSession.setActive(true);
        } catch (Exception e) {
            Log.e(TAG, "mSession.setActive() failed");
        }
    }
    switch(playbackLocation) {
        case LOCAL:
            {
                if (player != null && player.isInitialized()) {
                    // if we are at the end of the song, go to the next song first
                    final long duration = player.getDuration();
                    if (repeatMode != RepeatMode.ONE && duration > 2000 && player.getPosition() >= duration - 2000) {
                        gotoNext(true);
                    }
                    player.start();
                    // make sure we fade in, in case a previous fadein was stopped
                    // because of another focus loss
                    playerHandler.removeMessages(PlayerHandler.FADE_DOWN);
                    playerHandler.sendEmptyMessage(PlayerHandler.FADE_UP);
                    setIsSupposedToBePlaying(true, true);
                    cancelShutdown();
                    updateNotification();
                } else if (getCurrentPlaylist().size() == 0) {
                    // something.
                    if (queueReloading) {
                        playOnQueueLoad = true;
                    } else {
                        playAutoShuffleList();
                    }
                }
                break;
            }
        case REMOTE:
            {
                // if we are at the end of the song, go to the next song first
                final long duration = player.getDuration();
                if (repeatMode != RepeatMode.ONE && duration > 2000 && player.getPosition() >= duration - 2000) {
                    gotoNext(true);
                }
                if (!isSupposedToBePlaying) {
                    isSupposedToBePlaying = true;
                    notifyChange(InternalIntents.PLAY_STATE_CHANGED);
                }
                cancelShutdown();
                updateNotification();
                switch(playbackState) {
                    case STOPPED:
                        {
                            try {
                                castManager.checkConnectivity();
                                prepareChromeCastLoad(0, true);
                                playbackState = PLAYING;
                                updateNotification();
                            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                                Log.e(TAG, "Play() called & failed. State: Stopped " + e.toString());
                                playbackState = STOPPED;
                                updateNotification();
                            }
                            break;
                        }
                    case PAUSED:
                        {
                            try {
                                castManager.checkConnectivity();
                                castManager.play();
                                playbackState = PLAYING;
                                updateNotification();
                            } catch (TransientNetworkDisconnectionException | NoConnectionException | CastException e) {
                                Log.e(TAG, "Play() called & failed. State: Paused " + e.toString());
                                playbackState = PAUSED;
                                updateNotification();
                            }
                            break;
                        }
                }
                if (getCurrentPlaylist().size() == 0) {
                    if (queueReloading) {
                        playOnQueueLoad = true;
                    } else {
                        playAutoShuffleList();
                    }
                }
            }
    }
}
Also used : NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) AudioAttributes(android.media.AudioAttributes) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) AudioFocusRequest(android.media.AudioFocusRequest) SuppressLint(android.annotation.SuppressLint) SQLiteException(android.database.sqlite.SQLiteException) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) ConcurrentModificationException(java.util.ConcurrentModificationException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 SQLiteException (android.database.sqlite.SQLiteException)1 AudioAttributes (android.media.AudioAttributes)1 AudioFocusRequest (android.media.AudioFocusRequest)1 CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)1 NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)1 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1