Search in sources :

Example 1 with ServiceCommandError

use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.

the class BeamManager method playVideo.

public void playVideo(StreamInfo info, final MediaPlayer.LaunchListener listener) {
    if (!mConnected) {
        listener.onError(ServiceCommandError.getError(503));
    }
    mStreamInfo = info;
    String location = info.getStreamUrl();
    if (!location.startsWith("http")) {
        BeamServer.setCurrentVideo(location);
        location = BeamServer.getVideoURL();
    }
    String subsLocation = null;
    SubtitleWrapper subtitle = info.getSubtitle();
    if (subtitle != null && subtitle.getFileUri() != null) {
        BeamServer.setCurrentSubs(subtitle.getFileUri().getPath());
        if (mCurrentDevice.hasCapability(MediaPlayer.Subtitles_Vtt)) {
            subsLocation = BeamServer.getSubsURL(BeamServer.VTT);
        } else if (mCurrentDevice.hasCapability(MediaPlayer.Subtitles_Srt)) {
            subsLocation = BeamServer.getSubsURL(BeamServer.SRT);
        }
    } else {
        BeamServer.removeSubs();
    }
    try {
        URL url = new URL(location);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        location = uri.toString();
        if (subsLocation != null) {
            URL subsUrl = new URL(subsLocation);
            URI subsUri = new URI(subsUrl.getProtocol(), subsUrl.getUserInfo(), subsUrl.getHost(), subsUrl.getPort(), subsUrl.getPath(), subsUrl.getQuery(), subsUrl.getRef());
            subsLocation = subsUri.toString();
        }
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
    }
    String title = info.getFullTitle();
    String imageUrl = info.getPosterImage() == null ? "https://butterproject.org/images/header-logo.png" : info.getPosterImage();
    // String url, String mimeType, String title, String description, String iconSrc, boolean shouldLoop, LaunchListener listener
    if (mCurrentDevice != null) {
        MediaInfo mediaInfo = new MediaInfo(location, subsLocation, "video/mp4", title, "");
        mediaInfo.addImages(new ImageInfo(imageUrl));
        mCurrentDevice.getCapability(MediaPlayer.class).playMedia(mediaInfo, false, new MediaPlayer.LaunchListener() {

            @Override
            public void onSuccess(MediaPlayer.MediaLaunchObject object) {
                mLaunchSession = object.launchSession;
                if (listener != null) {
                    listener.onSuccess(object);
                }
            }

            @Override
            public void onError(ServiceCommandError error) {
                Timber.e(error.getMessage());
                if (listener != null) {
                    listener.onError(error);
                }
            }
        });
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) MediaInfo(com.connectsdk.core.MediaInfo) ImageInfo(com.connectsdk.core.ImageInfo) SubtitleWrapper(butter.droid.base.providers.subs.model.SubtitleWrapper) MediaPlayer(com.connectsdk.service.capability.MediaPlayer)

Example 2 with ServiceCommandError

use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.

the class BeamPlayerNotificationService method handleIntent.

private void handleIntent(Intent intent) {
    if (intent == null || intent.getAction() == null) {
        return;
    }
    String action = intent.getAction();
    if (mediaControl == null) {
        Intent stopIntent = new Intent(getApplicationContext(), BeamPlayerNotificationService.class);
        stopService(stopIntent);
        return;
    }
    if (action.equalsIgnoreCase(ACTION_PLAY) || action.equalsIgnoreCase(ACTION_PAUSE)) {
        ResponseListener<Object> responseListener = new ResponseListener<Object>() {

            @Override
            public void onSuccess(Object object) {
                mediaControl.getPlayState(playStateListener);
            }

            @Override
            public void onError(ServiceCommandError error) {
                mediaControl.getPlayState(playStateListener);
            }
        };
        if (isPlaying) {
            isPlaying = false;
            mediaControl.pause(responseListener);
            buildNotification(generateAction(R.drawable.ic_av_play, "Play", ACTION_PLAY));
        } else {
            isPlaying = true;
            mediaControl.play(responseListener);
            buildNotification(generateAction(R.drawable.ic_av_pause, "Pause", ACTION_PAUSE));
        }
        mediaControl.getPlayState(playStateListener);
    } else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD)) {
        mediaControl.getPosition(new MediaControl.PositionListener() {

            @Override
            public void onSuccess(Long object) {
                long seek = object + 10000;
                mediaControl.seek(seek, null);
            }

            @Override
            public void onError(ServiceCommandError error) {
            }
        });
    } else if (action.equalsIgnoreCase(ACTION_REWIND)) {
        mediaControl.getPosition(new MediaControl.PositionListener() {

            @Override
            public void onSuccess(Long object) {
                long seek = object - 10000;
                mediaControl.seek(seek, null);
            }

            @Override
            public void onError(ServiceCommandError error) {
            }
        });
    } else if (action.equalsIgnoreCase(ACTION_STOP)) {
        manager.stopVideo();
    }
}
Also used : MediaControl(com.connectsdk.service.capability.MediaControl) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener)

Example 3 with ServiceCommandError

use of com.connectsdk.service.command.ServiceCommandError 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)

Example 4 with ServiceCommandError

use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.

the class AirPlayService method playVideo.

public void playVideo(final String url, String mimeType, String title, String description, String iconSrc, boolean shouldLoop, final LaunchListener listener) {
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            LaunchSession launchSession = new LaunchSession();
            launchSession.setService(AirPlayService.this);
            launchSession.setSessionType(LaunchSessionType.Media);
            Util.postSuccess(listener, new MediaLaunchObject(launchSession, AirPlayService.this));
            startTimer();
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    String uri = getRequestURL("play");
    String payload = null;
    PListBuilder builder = new PListBuilder();
    builder.putString("Content-Location", url);
    builder.putReal("Start-Position", 0);
    payload = builder.toString();
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, payload, responseListener);
    request.send();
}
Also used : LaunchSession(com.connectsdk.service.sessions.LaunchSession) PListBuilder(com.connectsdk.service.airplay.PListBuilder) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 5 with ServiceCommandError

use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.

the class DLNAService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
    Util.runInBackground(new Runnable() {

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
            String method = command.getTarget();
            String payload = (String) command.getPayload();
            String targetURL = null;
            String serviceURN = null;
            if (payload == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"payload\" is missed", null));
                return;
            }
            if (payload.contains(AV_TRANSPORT_URN)) {
                targetURL = avTransportURL;
                serviceURN = AV_TRANSPORT_URN;
            } else if (payload.contains(RENDERING_CONTROL_URN)) {
                targetURL = renderingControlURL;
                serviceURN = RENDERING_CONTROL_URN;
            } else if (payload.contains(CONNECTION_MANAGER_URN)) {
                targetURL = connectionControlURL;
                serviceURN = CONNECTION_MANAGER_URN;
            }
            if (serviceURN == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"serviceURN\" is missed", null));
                return;
            }
            if (targetURL == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"targetURL\" is missed", null));
                return;
            }
            try {
                HttpConnection connection = createHttpConnection(targetURL);
                connection.setHeader("Content-Type", "text/xml; charset=utf-8");
                connection.setHeader("SOAPAction", String.format("\"%s#%s\"", serviceURN, method));
                connection.setMethod(HttpConnection.Method.POST);
                connection.setPayload(payload);
                connection.execute();
                int code = connection.getResponseCode();
                if (code == 200) {
                    Util.postSuccess(command.getResponseListener(), connection.getResponseString());
                } else {
                    Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Aggregations

ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)84 JSONObject (org.json.JSONObject)70 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)65 ServiceCommand (com.connectsdk.service.command.ServiceCommand)55 JSONException (org.json.JSONException)47 URLServiceSubscription (com.connectsdk.service.command.URLServiceSubscription)13 IOException (java.io.IOException)13 LaunchSession (com.connectsdk.service.sessions.LaunchSession)12 AppInfo (com.connectsdk.core.AppInfo)11 SuppressLint (android.annotation.SuppressLint)8 JSONArray (org.json.JSONArray)7 WebOSWebAppSession (com.connectsdk.service.sessions.WebOSWebAppSession)6 ChannelInfo (com.connectsdk.core.ChannelInfo)5 HttpConnection (com.connectsdk.etc.helper.HttpConnection)4 WebAppSession (com.connectsdk.service.sessions.WebAppSession)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 MediaInfo (com.connectsdk.core.MediaInfo)3 MediaPlayer (com.connectsdk.service.capability.MediaPlayer)3