Search in sources :

Example 1 with PListParser

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);
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) PListParser(com.connectsdk.service.airplay.PListParser) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Aggregations

PListParser (com.connectsdk.service.airplay.PListParser)1 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)1 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 JSONObject (org.json.JSONObject)1