use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException 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();
}
}
}
}
}
use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException in project Shuttle by timusus.
the class BaseCastManager method disconnectDevice.
/**
* Disconnects from the connected device.
*
* @param stopAppOnExit If {@code true}, the application running on the cast device will be
* stopped when disconnected.
* @param clearPersistedConnectionData If {@code true}, the persisted connection information
* will be cleared as part of this call.
* @param setDefaultRoute If {@code true}, after disconnection, the selected route will be set
* to the Default Route.
*/
public final void disconnectDevice(boolean stopAppOnExit, boolean clearPersistedConnectionData, boolean setDefaultRoute) {
LOGD(TAG, "disconnectDevice(" + clearPersistedConnectionData + "," + setDefaultRoute + ")");
if (mSelectedCastDevice == null) {
return;
}
mSelectedCastDevice = null;
mDeviceName = null;
String message = "disconnectDevice() Disconnect Reason: ";
int reason;
if (mConnectionSuspended) {
message += "Connectivity lost";
reason = DISCONNECT_REASON_CONNECTIVITY;
} else {
switch(mApplicationErrorCode) {
case CastStatusCodes.APPLICATION_NOT_RUNNING:
message += "App was taken over or not available anymore";
reason = DISCONNECT_REASON_APP_NOT_RUNNING;
break;
case NO_APPLICATION_ERROR:
message += "Intentional disconnect";
reason = DISCONNECT_REASON_EXPLICIT;
break;
default:
message += "Other";
reason = DISCONNECT_REASON_OTHER;
}
}
LOGD(TAG, message);
for (BaseCastConsumer consumer : mBaseCastConsumers) {
consumer.onDisconnectionReason(reason);
}
LOGD(TAG, "mConnectionSuspended: " + mConnectionSuspended);
if (!mConnectionSuspended && clearPersistedConnectionData) {
clearPersistedConnectionInfo(CLEAR_ALL);
stopReconnectionService();
}
try {
if ((isConnected() || isConnecting()) && stopAppOnExit) {
LOGD(TAG, "Calling stopApplication");
stopApplication();
}
} catch (NoConnectionException | TransientNetworkDisconnectionException e) {
LOGE(TAG, "Failed to stop the application after disconnecting route", e);
}
onDeviceUnselected();
if (mApiClient != null) {
// will throw an exception
if (mApiClient.isConnected()) {
LOGD(TAG, "Trying to disconnect");
mApiClient.disconnect();
}
if ((mMediaRouter != null) && setDefaultRoute) {
LOGD(TAG, "disconnectDevice(): Setting route to default");
mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
}
mApiClient = null;
}
mSessionId = null;
onDisconnected(stopAppOnExit, clearPersistedConnectionData, setDefaultRoute);
}
Aggregations