2016-01-13 50 views
10

我有奇怪的問題,我使用下面的代碼工作正常一審創造的Chromecast mediaprovider,顯示設備列表,一旦slected我用router.selectRoute(routeinfo); 但一旦我退出應用程序的代碼無法找到Chromecast裝置,如何從應用程序堆棧中刪除應用程序,此代碼再次正常工作並顯示設備。的Chromecast MediaRouteProviderService

如果沒有選擇任何設備和應用程序使用背壓然後退出也是這個代碼工作正常

所以我在做什麼錯在這裏?當我的應用程序在簡單的後退按下時,我認爲資源不會被清除。

public class ChromecastRouteProviderService extends MediaRouteProviderService { 
final String LOGTAG = "Chromecast"; 
private static final String CONTROL_CATEGORY = CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID); 
private static final MediaRouteSelector SELECTOR = new MediaRouteSelector.Builder().addControlCategory(CONTROL_CATEGORY) 
     .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK).build(); 
private IntentFilter controlFilter; 

public ChromecastRouteProviderService() { 
    controlFilter = new IntentFilter(); 
} 

public void onCreate() { 
    super.onCreate(); 
    controlFilter.addCategory(IAppConstants.CATEGORY); 
    controlFilter.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK); 
} 

@Override 
public MediaRouteProvider onCreateMediaRouteProvider() { 
    return new ChromecastRouteProvider(this); 
} 

class ChromecastRouteProvider extends MediaRouteProvider { 
    MediaRouter.Callback callback; 
    Hashtable routes; 

    public ChromecastRouteProvider(Context context) { 
     super(context); 
     routes = new Hashtable(); 
     callback = new CastCallBack(); 
    } 

    @Nullable 
    @Override 
    public RouteController onCreateRouteController(String routeId) { 
     MediaRouter.RouteInfo routeInfo = (MediaRouter.RouteInfo) routes.get(routeId); 
     if (routeInfo == null) { 
      return super.onCreateRouteController(routeId); 
     } else { 
      return new ChromecastRouteController(getContext(), routeInfo); 
     } 
    } 

    @Override 
    public void onDiscoveryRequestChanged(@Nullable MediaRouteDiscoveryRequest request) { 
     super.onDiscoveryRequestChanged(request); 
     if (request == null || !request.isActiveScan() || !request.isValid()) { 
      stopScan(); 
      return; 
     } 
     if (!request.getSelector().hasControlCategory(IAppConstants.CATEGORY)) { 
      Log.i(LOGTAG, "Not scanning for non remote playback"); 
      stopScan(); 
      return; 
     } else { 
      Log.i(LOGTAG, "Scanning..."); 
      mediarouter.addCallback(ChromecastRouteProviderService.SELECTOR, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); 
      return; 
     } 
    } 

    void updateDescriptor() { 
     final MediaRouteProviderDescriptor.Builder descriptor = new MediaRouteProviderDescriptor.Builder(); 
     for (Iterator iterator = routes.values().iterator(); iterator.hasNext();) { 
      MediaRouter.RouteInfo routeinfo = (MediaRouter.RouteInfo) iterator.next(); 
      try { 
       Bundle bundle = new Bundle(); 
       bundle.putBoolean("has_upsell", true); 
       descriptor.addRoute(new MediaRouteDescriptor.Builder(routeinfo.getId(), routeinfo.getName()) 
         .addControlFilter(controlFilter).setPlaybackStream(3) 
         .setDescription(routeinfo.getDescription()) 
         .setEnabled(true).setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE) 
         .setVolumeHandling(1).setVolumeMax(100).setVolume(100) 
         .setExtras(bundle).build()); 
      } catch (Exception e) { 
       throw new Error("wtf"); 
      } 
     } 

     getHandler().post(new Runnable() { 
      @Override 
      public void run() { 
       setDescriptor(descriptor.build()); 
      } 
     }); 

    } 

    void stopScan() { 
     Log.i(LOGTAG, "Stopping scan..."); 
     try { 
      MediaRouter.getInstance(getContext()).removeCallback(callback); 
      return; 
     } catch (Exception exception) { 
      return; 
     } 
    } 

    class CastCallBack extends MediaRouter.Callback { 
     void check(MediaRouter mediarouter, MediaRouter.RouteInfo routeinfo) { 
      Log.i(LOGTAG, new StringBuilder().append("Checking route ").append 
        (routeinfo.getName()).toString()); 
      CastDevice device = CastDevice.getFromBundle(routeinfo.getExtras()); 
      if (routeinfo.matchesSelector(ChromecastRouteProviderService.SELECTOR) 
        && device != null && device.isOnLocalNetwork()) { 
       routes.put(routeinfo.getId(), routeinfo); 
       updateDescriptor(); 
       return; 
      } else { 
       return; 
      } 
     } 

     public void onRouteAdded(MediaRouter mediarouter, MediaRouter.RouteInfo routeinfo) { 
      super.onRouteAdded(mediarouter, routeinfo); 
      check(mediarouter, routeinfo); 
     } 

     public void onRouteChanged(MediaRouter mediarouter, MediaRouter.RouteInfo routeinfo) { 
      super.onRouteChanged(mediarouter, routeinfo); 
      check(mediarouter, routeinfo); 
     } 

     public void onRouteRemoved(MediaRouter mediarouter, MediaRouter.RouteInfo routeinfo) { 
      super.onRouteRemoved(mediarouter, routeinfo); 
      if (routeinfo.matchesSelector(ChromecastRouteProviderService.SELECTOR)) ; 
     } 

    } 
} 

}

回答

2

好了終於讓我找到我自己的答案,

問題時選擇的供應商是它不添加使用onRouteAdded爲什麼?我真的不明白谷歌的邏輯

所以解決的辦法就是unselect路由器,當你想或者更好地選擇默認路由的時候讓你的路線被釋放

MediaRouter.getInstance(this).getDefaultRoute().select(); 

但同樣十分之一的時候,它會不工作

希望能幫助別人