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();
}
}
}
}
}
Aggregations