use of com.connectsdk.service.airplay.PListParser in project butter-android by butterproject.
the class AirPlayService method startTimer.
/**
* We send periodically a command to keep connection alive and for avoiding
* stopping media session
*
* Fix for https://github.com/ConnectSDK/Connect-SDK-Cordova-Plugin/issues/5
*/
private void startTimer() {
stopTimer();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.d("Timer", "Timer");
getPlaybackPosition(new PlaybackPositionListener() {
@Override
public void onGetPlaybackPositionSuccess(long duration, long position) {
if (position >= duration) {
stopTimer();
}
}
@Override
public void onGetPlaybackPositionFailed(ServiceCommandError error) {
}
});
}
}, KEEP_ALIVE_PERIOD, KEEP_ALIVE_PERIOD);
updateTimer = new Timer();
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
getPlaybackInfo(new ResponseListener<Object>() {
@Override
public void onSuccess(Object object) {
PlayStateStatus playState = PlayStateStatus.Unknown;
try {
JSONObject response = new PListParser().parse(object.toString());
if (response.length() > 0) {
boolean readyToPlay = false;
if (response.has("readyToPlay")) {
readyToPlay = response.getBoolean("readyToPlay");
}
if (!readyToPlay) {
playState = PlayStateStatus.Buffering;
} else {
if (!response.has("rate")) {
playState = PlayStateStatus.Finished;
} else {
int rate = response.getInt("rate");
if (rate == 0) {
playState = PlayStateStatus.Paused;
} else if (rate == 1) {
playState = PlayStateStatus.Playing;
}
}
}
if (mSubscriptions.size() > 0) {
for (URLServiceSubscription<?> subscription : mSubscriptions) {
if (subscription.getTarget().equalsIgnoreCase(PLAY_STATE)) {
for (int i = 0; i < subscription.getListeners().size(); i++) {
@SuppressWarnings("unchecked") ResponseListener<Object> listener = (ResponseListener<Object>) subscription.getListeners().get(i);
Util.postSuccess(listener, playState);
}
}
}
}
}
} catch (Exception e) {
}
}
@Override
public void onError(ServiceCommandError error) {
}
});
}
}, 0, UPDATE_PERIOD);
}
Aggregations