无法在Android发件人上恢复,暂停,停止ChromeCast

时间:2014-04-03 05:55:01

标签: android chromecast google-cast

我已经能够创建ChromeCast iOS发送器而没有任何问题但是当我开始构建Android发送器时,我在尝试暂停,搜索或停止当前视频时一直收到IllegalStateException(找不到当前媒体会话错误)来自ChromeCast。

这是我连接演员的方式:

 private void launchReceiver(){
    try {
        setupCastClientListener();
        Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(mSelectedDevice, mCastClientListener);


        // Connect to Google Play services
        mConnectionCallbacks = new ConnectionCallbacks();
        mConnectionFailedListener = new ConnectionFailedListener();

        mApiClient = new GoogleApiClient.Builder(this)
        .addApi(Cast.API, apiOptionsBuilder.build())
        .addConnectionCallbacks(mConnectionCallbacks)
        .addOnConnectionFailedListener(mConnectionFailedListener)
        .build();
        mApiClient.connect();
    }catch(Exception e) {
        Log.e(TAG, "Failed launchReceiver", e);
    }
}

private void attachCustomMediaChannel(){
    if(mCustomChannel==null){
        mCustomChannel = new CustomMediaChannel();
        mCustomChannel.setOnStatusUpdatedListener(
                new RemoteMediaPlayer.OnStatusUpdatedListener() {
                    @Override
                    public void onStatusUpdated() {
                        MediaStatus mediaStatus = mCustomChannel.getMediaStatus();
                        //MediaInfo mediaInfo = mCustomChannel.getMediaInfo();
                        isPlayingContent = mediaStatus.getPlayerState() == MediaStatus.PLAYER_STATE_PLAYING;
                        updateControlsUI();
                    }
                });

        mCustomChannel.setOnMetadataUpdatedListener(
                new RemoteMediaPlayer.OnMetadataUpdatedListener() {

                    @Override
                    public void onMetadataUpdated() {
                        updateControlsUI();
                    }
                });
        try{
            Cast.CastApi.setMessageReceivedCallbacks(
                    mApiClient, 
                    mCustomChannel.getNamespace(), 
                    mCustomChannel);
        }catch(IOException e){
            Log.e(TAG, "Exception while creating channel", e);
        }

        //Request update status
        mCustomChannel.requestStatus(mApiClient).setResultCallback(
                new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
                    @Override
                    public void onResult(MediaChannelResult result) {
                        Status status = result.getStatus();
                        if (!status.isSuccess()) {
                            Log.e(TAG, "Failed to request status [" + status.toString() + "]");
                        }
                    }
                });
    }
}


    private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks{
    @Override
    public void onConnected(Bundle connectionHint){
        if(mWaitingForReconnect){
            mWaitingForReconnect = false;
        }else{
            try{
                Cast.CastApi.launchApplication(mApiClient, CHROME_CAST_ID, false)
                .setResultCallback( 
                        new ResultCallback<Cast.ApplicationConnectionResult>() {
                            @Override
                            public void onResult(ApplicationConnectionResult result) {
                                Status status = result.getStatus();
                                String sessionId = result.getSessionId();
                                boolean wasLaunched = result.getWasLaunched();
                                if(status.isSuccess()){
                                    mApplicationStarted = true;
                                    setCastButtonAttribute(getResources().getString(R.string.chrome_cast_stop_casting), stopCastingClickListener);
                                    attachCustomMediaChannel();
                                }else{
                                    teardown();
                                }
                            }
                        });
            }catch(Exception e){
                Log.e(TAG, "Failed to launch application",e);
            }
        }
    }

    @Override
    public void onConnectionSuspended(int cause){
        mWaitingForReconnect = true;
    }
}

private class ConnectionFailedListener implements GoogleApiClient.OnConnectionFailedListener{
    @Override
    public void onConnectionFailed(ConnectionResult result){
        teardown();
    }
}

以下是我的Android发件人暂停/停止/恢复的代码:

private void loadMedia(String url, String imageUrl, String title, long startTime, String customData, Boolean autoPlay){
    try{
        MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
        mediaMetadata.putString(MediaMetadata.KEY_TITLE, title);
        mediaMetadata.addImage(new WebImage(Uri.parse(imageUrl), 100, 150));
        mediaMetadata.putString("customData", customData);

        MediaInfo mediaInfo = new MediaInfo.Builder(url)
        .setContentType("video/mp4")
        .setStreamType(MediaInfo.STREAM_TYPE_NONE)
        .setMetadata(mediaMetadata)
        .build();

        mCustomChannel.load(mApiClient, mediaInfo, autoPlay, startTime);

        restartTrickplayTimer();
    }catch(IllegalArgumentException e){
        Log.e(TAG, "Load Media failed!");
    }
}

private void watchSelectedVideo(){
    String url = "http://cloudfront.net/testmovie.mp4";
    String imgUrl = "http://cloudfront.net/testmovie-220x330.jpg";
    String title = "Test movie";
    long startTime = 0;
    String customData = getCustomData();
    Boolean autoPlay = true;
    loadMedia(url, imgUrl, title, startTime, customData, autoPlay);
}

private void resumePlaying(){
    try{
        if(mCustomChannel!=null && mApiClient.isConnected()){
            mCustomChannel.play(mApiClient);
        }

    }catch(IOException e){
        Log.e(TAG, "Exception while trying to resume the video", e);
    }catch(IllegalStateException stateEx){
        Log.e(TAG, "Exception while trying to resume the video", stateEx);
    }
}

private void pausePlaying(){
    try{
        if(mCustomChannel!=null && isConnected()){
            mCustomChannel.pause(mApiClient);
        }
    }catch(IOException e){
        Log.e(TAG, "Exception while trying to pause the video", e);
    }catch(IllegalStateException stateEx){
        Log.e(TAG, "Exception while trying to pause the video", stateEx);
    }
}

private void stopPlaying(){
    try{
        if(mCustomChannel!=null && isConnected()){
            mCustomChannel.stop(mApiClient);
            resetUI();
        }

    }catch(IOException e){
        Log.e(TAG, "Exception while trying to stop the video", e);
    }catch(IllegalStateException stateEx){
        Log.e(TAG, "Exception while trying to stop the video", stateEx);
    }
}

即使已经开始播放内容并且requestStatus回调保持返回“statusCode = SERVICE_DISABLED,resolution = null”,即使setOnMetadataUpdatedListener和setOnMetadataUpdatedListener也未被触发。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的帖子缺少申请的一些关键部分;例如:

  • setupCastClientListener()做什么?
  • 什么是CustomMediaChannel类?为什么还需要自定义媒体频道?人们需要编写自定义媒体频道是非常罕见的。

我建议你从我们在github上的一个样本开始,确保运行然后看看流程是什么以及如何根据你的需要调整它。