连接到 Google API 客户端时出现问题

Posted

技术标签:

【中文标题】连接到 Google API 客户端时出现问题【英文标题】:Issue while connecting to Google API client 【发布时间】:2015-10-27 17:20:26 【问题描述】:

我正在构建一个 chromecast 应用程序。我可以使用静态文本启动接收器。但是,我无法与它交互(播放媒体)并且正在获取

10-27 10:01:20.192 2292-6161/? E/MDM: [217] b.run: Couldn't connect to Google API client: ConnectionResultstatusCode=API_UNAVAILABLE, resolution=null, message=null

我认为这可能是我在启动应用程序时遇到超时(状态代码 15)的问题。我还注意到接收器屏幕在一段时间后停止投射,而我的发送器设备没有检测到这一点,仍然认为这是一个正在进行的会话。有人看到问题了吗?

请参阅下面的代码以供参考。

public class MediaRouterButtonActivity extends FragmentActivity 

private static final String TAG = MediaRouterButtonActivity.class
        .getSimpleName();

private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouter.Callback mMediaRouterCallback;
private MediaRouteButton mMediaRouteButton;
private int mRouteCount = 0;

private MediaMetadata mMediaMetadata;
private CastDevice mSelectedDevice;
private GoogleApiClient mApiClient;
private Cast.Listener mCastListener;
private ConnectionCallbacks mConnectionCallbacks;
private RemoteMediaPlayer mRemoteMediaPlayer;
private boolean mWaitingForReconnect;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.media_router_button);

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    mMediaRouteSelector = new MediaRouteSelector.Builder()
            .addControlCategory(
                    CastMediaControlIntent.categoryForCast(getResources().getString(R.string.app_id))).build();

    // Set the MediaRouteButton selector for device discovery.
    mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
    mMediaRouteButton.setRouteSelector(mMediaRouteSelector);

    mMediaRouterCallback = new MediaRouterCallback();

    mCastListener = new CastListener();
    mConnectionCallbacks = new ConnectionCallbacks();
    mConnectionFailedListener = new ConnectionFailedListener();
    mWaitingForReconnect = false;

    mRemoteMediaPlayer = new RemoteMediaPlayer();


@Override
protected void onStart() 
    super.onStart();
    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);


@Override
protected void onResume() 
    super.onResume();

    // Add the callback to start device discovery
    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);


@Override
protected void onPause() 
    mMediaRouter.removeCallback(mMediaRouterCallback);
    super.onPause();


@Override
protected void onStop() 
    setSelectedDevice(null);
    mMediaRouter.removeCallback(mMediaRouterCallback);
    super.onStop();


private class MediaRouterCallback extends MediaRouter.Callback 
    @Override
    public void onRouteAdded(MediaRouter router, RouteInfo route) 
        Log.d(TAG, "onRouteAdded");
        if (++mRouteCount >= 1) 
            // Show the button when a device is discovered.
            mMediaRouteButton.setVisibility(View.VISIBLE);
        
    

    @Override
    public void onRouteRemoved(MediaRouter router, RouteInfo route) 
        Log.d(TAG, "onRouteRemoved");
        if (--mRouteCount == 0) 
            // Hide the button if there are no devices discovered.
            mMediaRouteButton.setVisibility(View.GONE);
        
    

    @Override
    public void onRouteSelected(MediaRouter router, RouteInfo info) 
        Log.d(TAG, "onRouteSelected");
        // Handle route selection
        mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
        setSelectedDevice(mSelectedDevice);
    

    @Override
    public void onRouteUnselected(MediaRouter router, RouteInfo info) 
        Log.d(TAG, "onRouteUnselected: info=" + info);
        setSelectedDevice(null);
    


private void setSelectedDevice(CastDevice device) 
    Log.d(TAG, "setSelectedDevice: " + device);
    mSelectedDevice = device;

    if (mSelectedDevice != null) 
        try 
            disconnectApiClient();
            connectApiClient();
         catch (IllegalStateException e) 
            Log.w(TAG, "Exception while connecting API client", e);
            disconnectApiClient();
        
     else 
        if (mApiClient != null) 
            disconnectApiClient();
        
        mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
    


private void connectApiClient() 
    Cast.CastOptions apiOptions = Cast.CastOptions.builder(mSelectedDevice, mCastListener)
            .build();
    mApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addApi(Cast.API, apiOptions)
            .addConnectionCallbacks(mConnectionCallbacks)
            .addOnConnectionFailedListener(mConnectionFailedListener)
            .build();
    Log.d(TAG, "connectApiClient");
    mApiClient.connect();


private void disconnectApiClient() 
    if (mApiClient != null) 
        if (mApiClient.isConnected() || mApiClient.isConnecting()) 
            mApiClient.disconnect();
            Log.d(TAG, "disconnectApiClient");
        
        mApiClient = null;
    


private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks 
    @Override
    public void onConnectionSuspended(int cause) 
        mWaitingForReconnect = true;
        Log.d(TAG, "ConnectionCallbacks.onConnectionSuspended");
    

    @Override
    public void onConnected(Bundle connectionHint) 
        Log.d(TAG, "ConnectionCallbacks.onConnected");
        if (mWaitingForReconnect) 
            mWaitingForReconnect = false;
         else 
            Cast.CastApi.launchApplication(mApiClient, getResources().getString(R.string.app_id))
                    .setResultCallback(new ConnectionResultCallback());
        
    



private final class ConnectionResultCallback implements
        ResultCallback<ApplicationConnectionResult> 
    @Override
    public void onResult(ApplicationConnectionResult result) 
        Status status = result.getStatus();
        Log.d(TAG, "onResultOnConnected" + status.isSuccess());
        if (status.isSuccess()) 
            try 
                Cast.CastApi.setMessageReceivedCallbacks(mApiClient,
                        mRemoteMediaPlayer.getNamespace(),
                        mRemoteMediaPlayer);
                JSONObject customData = new JSONObject();
                String accessToken = "asdkfalksdjfkaljsdfla";
                try 
                    customData.put("X-At", accessToken);
                 catch (JSONException e) 
                    Log.e(TAG, "Empty Access Token", e);
                
                Log.d(TAG, "mediaMetaDataDebug");
                mMediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
                mMediaMetadata.putString(MediaMetadata.KEY_TITLE, "Demo Video");
                MediaInfo mediaInfo = new MediaInfo.Builder(
                        "https://abcdef.com")       //link Instead
                        .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                        .setCustomData(customData)
                        .build();
                try 
                    mRemoteMediaPlayer.load(mApiClient, mediaInfo, true)
                            .setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() 
                                @Override
                                public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) 
                                    Status status = mediaChannelResult.getStatus();
                                    if (status.isSuccess()) 
                                        //allow users to pause video somehow;
                                    
                                
                            );

                 catch (Exception e) 
                    Log.e(TAG, "Problem while loading media", e);
                
                Log.d(TAG, "wtf");
             catch (IOException e) 
                Log.e(TAG, "Exception while creating channel", e);
            

         else 
            Log.d(TAG, "ConnectionResultCallback. Unable to launch the application. statusCode: "
                    + status.getStatusCode());
        
    



//google play services crap, fix later
private class CastListener extends Cast.Listener 
    @Override
    public void onApplicationDisconnected(int statusCode) 
        Log.d(TAG, "onApplicationDisconnected");
        setSelectedDevice(null);
        try 
            Cast.CastApi.removeMessageReceivedCallbacks(mApiClient,
                    mRemoteMediaPlayer.getNamespace());
         catch (IOException e) 
            Log.w(TAG, "Exception while launching application", e);
        
    

这是我得到的相关日志

10-27 10:01:20.192 2292-6161/? E/MDM: [217] b.run: Couldn't connect to Google API client: ConnectionResultstatusCode=API_UNAVAILABLE, resolution=null, message=null
10-27 10:59:54.962 2292-19493/? D/DeviceConnectionService: client connected with version: 8115000
10-27 11:01:12.922 27024-27024/? D/MediaRouterButtonActivity: ConnectionCallbacks.onConnected
10-27 11:01:13.002 2292-6727/? D/DeviceConnectionService: client connected with version: 8115000
10-27 11:01:33.952 27024-27024/? D/MediaRouterButtonActivity: ConnectionResultCallback. Unable to launch the application. statusCode: 15

【问题讨论】:

它声明 API 不可用,那么您的设备是否有 Play 服务?另一方面,如果你没有连接,你是如何启动你的接收器的?需要更多代码和日志 @Ali Naddaf 是的,我的设备可以与其他示例应用程序一起使用,所以我假设可以。我能够启动应用程序,这意味着直到 C​​onnectionResultCallbacks,一切似乎都正常工作......但在那之后,接收器似乎变得无响应并在一段时间后自行断开连接。感谢您的帮助! 请查看this Google 开发者指南,它可能会对您有所帮助。 为了排除由于您自己的接收器而出现问题的可能性,我建议您将您的应用程序 ID 替换为默认应用程序 ID(使用 CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID),看看会发生什么,在我看来您正在使用标准 API,因此它应该能够与默认接收器一起使用。让我们知道结果如何。 如果您可以在某个地方分享您的应用程序供我抓取,我可以快速浏览一下 【参考方案1】:

以下更改使其在我的环境中工作:

    将应用 ID 更改为我可以访问的内容;您可以使用 CastVideos 应用使用的默认应用 ID 或 4F8B3483,也可以创建自己的样式接收器。 MediaInfo 需要有一个内容类型,因此在定义 MediaInfo 的位置添加了以下内容:'.setContentType("video/mp4")' 将内容的 url 更新为真实存在且可访问的内容(公共服务器上的 mp4 媒体)

然后我能够启动应用程序并通过单击“媒体路由按钮”查看媒体播放

【讨论】:

以上是关于连接到 Google API 客户端时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

从 SignalR .NET 客户端连接到工作集线器时出现 HttpClientException

从 Angular 客户端调用 API 时出现身份服务器 CORS 错误

尝试将客户端连接到服务器时出现Node.js Socket.io错误

从 Qpid JMS 客户端启用 AMQP 1.0 插件连接到 RabbitMQ 3.7.18 时出现错误匹配错误

无法连接到 Google API 客户端?

无法连接到 Google API 客户端