在尝试查找Chromecast时 - 我的应用程序从未在我的某些用户上调用过MediaRouter.Callback

时间:2015-09-12 13:01:20

标签: android chromecast google-cast

我正在将我的应用程序从CastCompanionLibrary中移开,因为我需要支持更多流媒体设备,而且我的一些用户在使用Chromecast时遇到了问题。我删除了所有不必要的代码,并创建了一个应用程序,只是为了查看发现过程中发生了什么,并将其发送给其中一个用户,代码永远不会从媒体路由器获得任何回调。同时他们可以使用CastCompanionLibrary运行我的旧应用程序,它会发现Chromecast很好。

以下是我发送给用户的代码,它是一个自包含的类和布局:

    public class MainActivity extends AppCompatActivity {

    private static SimpleDateFormat TIME_ONLY = new SimpleDateFormat("HH:mm:ss.SSS");
    private static Handler handler;
    protected MediaRouter.Callback mMediaRouterCallback;
    boolean isRunning = false;
    private TextView logView;
    private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private Timer removeRoutesTimer;

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

        logView = (TextView) findViewById(R.id.log);
        Button sendLog = (Button)findViewById(R.id.send_log);
        sendLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent send = new Intent(Intent.ACTION_SENDTO);
                String uriText = "mailto:" + Uri.encode("myemail") +
                        "?subject=" + Uri.encode("Logs") +
                        "&body=" + Uri.encode(logView.getText().toString());
                Uri uri = Uri.parse(uriText);

                send.setData(uri);
                startActivity(Intent.createChooser(send, "Send logs..."));
            }
        });

        mMediaRouter = createMediaRouter(getApplicationContext());
        mMediaRouterCallback = new MediaRouterCallback();
        mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(CastMediaControlIntent.categoryForCast(
                        CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
                .build();
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
        log("Discovery callback requested");
    }

    private void printRoutes() {
        List<MediaRouter.RouteInfo> routes = mMediaRouter.getRoutes();
        log("has routes " + routes.size() );
        for(MediaRouter.RouteInfo info : routes){
            log("has routes " + info.getName() +" - " + info.getDescription());
        }
    }

    public static void runOnUI(Runnable runnable) {
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }

        handler.post(runnable);
    }
    protected MediaRouter createMediaRouter(Context context) {
        return MediaRouter.getInstance(context);
    }



    private void log(String start) {
        Calendar cal = new GregorianCalendar();
        String currentLog = logView.getText().toString();
        logView.setText(currentLog + "\n" + TIME_ONLY.format(cal.getTime()) + " - " + start);

    }








    private class MediaRouterCallback extends MediaRouter.Callback {

        @Override
        public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
            log("onRouteAdded - " + route.getName());

            super.onRouteAdded(router, route);


        }

        @Override
        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo route) {
            log("onRouteChanged - " + route.getName());

            super.onRouteChanged(router, route);


        }


        @Override
        public void onRoutePresentationDisplayChanged(MediaRouter router,
                                                      MediaRouter.RouteInfo route) {

            log("onRoutePresentationDisplayChanged -  [" + route.getName() + "] ["
                    + route.getDescription() + "]");

            super.onRoutePresentationDisplayChanged(router, route);
        }

        @Override
        public void onRouteRemoved(final MediaRouter router, final MediaRouter.RouteInfo route) {
            log("onRouteRemoved - " + route.getName());

            super.onRouteRemoved(router, route);


        }

        @Override
        public void onRouteVolumeChanged(MediaRouter router, final MediaRouter.RouteInfo route) {

            super.onRouteVolumeChanged(router, route);
        }

        private void removeServices(final MediaRouter.RouteInfo route) {
            log("removeServices - " + route.getName());


        }
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Button
        android:id="@+id/send_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"
        android:text="Send log"/>



    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/send_log">

        <TextView
            android:id="@+id/log"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />
    </ScrollView>
</RelativeLayout>

以下是她寄给我的日志:

08:06:52.542 - Discovery callback requested

所以基本上没有调用回调方法。

这是我的网络中的日志,它工作正常(完全相同的apk):

16:56:06.085 - Discovery callback requested
16:56:07.193 - onRouteAdded - Master bedroom
16:56:07.268 - onRouteAdded - Living room chromecast
16:56:07.274 - onRouteChanged - Master bedroom
16:56:07.332 - onRouteAdded - Gym
16:56:07.337 - onRouteChanged - Living room chromecast
16:56:07.341 - onRouteChanged - Master bedroom
16:56:07.405 - onRouteChanged - Gym
16:56:07.409 - onRouteChanged - Living room chromecast
16:56:07.413 - onRouteAdded - Basement east bedroom
16:56:07.417 - onRouteChanged - Master bedroom
16:56:07.517 - onRouteChanged - Gym
16:56:07.522 - onRouteChanged - Living room chromecast
16:56:07.526 - onRouteChanged - Basement east bedroom
16:56:07.530 - onRouteChanged - Master bedroom

正如您所看到的,发现Chromecast的代码非常简单,我看不出错误在哪里。据我所知,这些都是所需的所有行:

mMediaRouter = createMediaRouter(getApplicationContext());
            mMediaRouterCallback = new MediaRouterCallback();
            mMediaRouteSelector = new MediaRouteSelector.Builder()
                    .addControlCategory(CastMediaControlIntent.categoryForCast(
                            CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
                    .build();
            mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                    MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);

如果我将MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY更改为MediaRouter.CALLBACK_FLAG_FORCE_DISCOVERY,我应该为此特定用户添加该功能,那么它可以正常工作。

那么我的发现代码有什么问题?

感谢。

0 个答案:

没有答案