如何使用Google Cast SDK 3添加自定义SessionProvider [英] How to add a custom SessionProvider with Google Cast SDK 3

查看:102
本文介绍了如何使用Google Cast SDK 3添加自定义SessionProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近将Google Cast SDK更新到了版本3。使用此SDK,应该可以通过在 OptionsProvider 。我们创建了一个自定义 SessionProvider 来使用getAdditionalSessionProviders()方法返回。

We've recently updated the Google Cast SDK to version 3. With this SDK it should be possible to add support for non-cast devices by implementing getAdditionalSessionProviders() in an OptionsProvider. We have created a custom SessionProvider to return with the getAdditionalSessionProviders() method.

但是,此SessionProvider从未使用过,它可能仅在 MediaRouteButton android / reference / com / google / android / gms / cast / framework / Session rel = nofollow>会话文档。但是我们找不到将发现的非广播设备添加到此列表的方法。我们已经搜索了在线提供的API,文档和示例,但是找不到如何执行此操作。我们只发现了旧版sdk版本的示例,但这些示例完全不同,无法使用。

However, this SessionProvider is never used and it will probably only be used when a device has been discovered and selected in the selection list when the MediaRouteButton is pressed as described in the Session documentation. But we can not find a way to add our discovered non-cast device to this list. We have searched the API, the documentation and the examples that are available online, but we couldn't find how to do this. We have only found examples of older sdk versions, but these are completely different and not usable.

我们想使用此功能将三星电视添加到带有三星SmartView SDK的Google Cast列表中,就像YouTube和Netflix App一样。

We would like to use this functionality to add Samsung tv's to the Google Cast list with Samsung's SmartView SDK just like the YouTube and Netflix app do.

推荐答案

创建自定义媒体路由提供程序:这是用于自定义媒体路由的假MediaRouteProvider。此类除了发布伪造的自定义MediaRoute外,什么也不做,以便Cast SDK从MediaRouter可以看到并选择它。

Create a custom Media Route Provider: Here is a fake MediaRouteProvider for a custom media route. This class does nothing but publish a fake custom MediaRoute so that it can be seen and selected by the Cast SDK from MediaRouter.

import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.media.MediaRouteDescriptor;
import android.support.v7.media.MediaRouteDiscoveryRequest;
import android.support.v7.media.MediaRouteProvider;
import android.support.v7.media.MediaRouteProvider.RouteController;
import android.support.v7.media.MediaRouteProviderDescriptor;
import android.support.v7.media.MediaRouter;
import java.util.ArrayList;

public final class CustomMediaRouteProvider extends MediaRouteProvider {
  private static final ArrayList<IntentFilter> CONTROL_FILTERS_BASIC;
  private static MediaRouteDescriptor DEFAULT_MEDIA_ROUTE_DESCRIPTOR;

  static {
    // This filter will be used by Cast SDK to match the session category.
    IntentFilter customControls = new IntentFilter();
    customControls.addCategory(CustomSessionProvider.CUSTOM_CATEGORY);

    CONTROL_FILTERS_BASIC = new ArrayList<IntentFilter>();
    CONTROL_FILTERS_BASIC.add(customControls);

    Bundle extras = new Bundle();
    extras.putCharSequence("ROUTE_URL", "http://abcdef.cyz");
    DEFAULT_MEDIA_ROUTE_DESCRIPTOR =
        new MediaRouteDescriptor.Builder("fake-custom-route-id", "fake custom route")
            .setDescription("Foo description")
            .addControlFilters(CONTROL_FILTERS_BASIC)
            .setPlaybackStream(AudioManager.STREAM_MUSIC)
            .setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
            .setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE)
            .setVolumeMax(100)
            .setVolume(10)
            .setExtras(extras)
            .build();
  }

  public CustomMediaRouteProvider(Context context) {
    super(context);
  }

  @Override
  public void onDiscoveryRequestChanged(MediaRouteDiscoveryRequest request) {
    if (request == null || request.getSelector() == null) {
      return;
    }

    publishRoutes();
  }

  @Override
  public RouteController onCreateRouteController(String routeId) {
    return null;
  }

  private void publishRoutes() {
    MediaRouteProviderDescriptor providerDescriptor =
        new MediaRouteProviderDescriptor.Builder().addRoute(DEFAULT_MEDIA_ROUTE_DESCRIPTOR).build();

    setDescriptor(providerDescriptor);
  }
}

您需要自定义会话的SessionProvider实现:

You need a SessionProvider implementation for your custom session:

import android.content.Context;
import com.google.android.gms.cast.framework.Session;
import com.google.android.gms.cast.framework.SessionProvider;

public class CustomSessionProvider extends SessionProvider {
  public static final String CUSTOM_CATEGORY = "CUSTOM";

  public CustomSessionProvider(Context applicationContext) {
    super(applicationContext, CUSTOM_CATEGORY);
  }

  @Override
  public Session createSession(String sessionId) {
    return new CustomSession(getContext(), getCategory(), sessionId);
  }

  @Override
  public boolean isSessionRecoverable() {
    return true;
  }
}

还有一个Session实现:这是一个虚假的Session实现用于自定义媒体路由类型。

And a Session implementation: Here is a fake Session implementation for custom media route type. It always succeeds on start/resume/end.

import android.content.Context;
import android.os.Bundle;
import com.google.android.gms.cast.framework.Session;

public class CustomSession extends Session {
  private static final String FAKE_SESSION_ID = "custom.session.id.12345";

  CustomSession(Context applicationContext, String category, String sessionId) {
    super(applicationContext, category, sessionId);
  }

  @Override
  protected void start(Bundle routeInfoExtra) {
    notifySessionStarted(FAKE_SESSION_ID);
  }

  @Override
  protected void resume(Bundle routeInfoExtra) {
    notifySessionResumed(false);
  }

  @Override
  protected void end(boolean stopCasting) {
    notifySessionEnded(0);
  }
}

在您的Cast V3发送者应用中,指定其他会话提供者:

In your Cast V3 sender app, specify the additional session provider:

public class CastOptionsProvider implements OptionsProvider {
@Override
  public List<SessionProvider> getAdditionalSessionProviders(Context appContext) {
    List<SessionProvider> additionalProviders = new ArrayList<>();
    additionalProviders.add(new CustomSessionProvider(appContext));
    return additionalProviders;
  }
}

在您的应用程序中添加MediaRouter提供程序:

In your Application add the MediaRouter provider:

public class CastVideosApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    MediaRouter mediaRouter = MediaRouter.getInstance(this);
    mediaRouter.addProvider(new CustomMediaRouteProvider(this));
  }
}

这篇关于如何使用Google Cast SDK 3添加自定义SessionProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆