Chromecast API Sender - Android手机

时间:2015-03-23 10:32:22

标签: android chromecast

我曾尝试为chromecast编写一些webapps,将自己的网站投射到chromecast。在Chrome桌面上,它是非常棒的。我能够连接到chromecast发送消息和...

但是当我从手机上的Chrome浏览器加载网站时,每当我想向接收者发送消息时,它就会一直说连接到chromecast。

我不想为手机编写应用程序,并希望将其保留在webapp上。

这是SDK Developer的示例代码。

<head>

<script type="text/javascript" src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
<meta charset="utf-8">
</script>
<script type="text/javascript">
var applicationID = 'HIDDEN';
var namespace = 'urn:x-cast:com.google.cast.test';
var session = null;
var sessionID = null;

/**
 * Call initialization for Cast
 */
if (!chrome.cast || !chrome.cast.isAvailable) {
    setTimeout(initializeCastApi, 1000);
}

/**
 * initialization
 */
function initializeCastApi() {
    var sessionRequest = new chrome.cast.SessionRequest(applicationID);
    var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
        sessionListener,
        receiverListener);

    chrome.cast.initialize(apiConfig, onInitSuccess, onError);
};

/**
 * initialization success callback
 */
function onInitSuccess() {
    appendMessage("onInitSuccess");
}

/**
 * initialization error callback
 */
function onError(message) {
    appendMessage("onError: "+JSON.stringify(message));
}

/**
 * generic success callback
 */
function onSuccess(message) {
    appendMessage("onSuccess: "+message);
}

/**
 * callback on success for stopping app
 */
function onStopAppSuccess() {
    appendMessage('onStopAppSuccess');
}

/**
 * session listener during initialization
 */
function sessionListener(e) {
    appendMessage('New session ID:' + e.sessionId);
    session = e;
    session.addUpdateListener(sessionUpdateListener);  
    session.addMessageListener(namespace, receiverMessage);
    sessionID = session.sessionId;
}

/**
 * listener for session updates
 */
function sessionUpdateListener(isAlive) {
  var message = isAlive ? 'Session Updated' : 'Session Removed';
  message += ': ' + session.sessionId;
  appendMessage(message);

  if (!isAlive) {
    session = null;
  }
};

/**
 * utility function to log messages from the receiver
 * @param {string} namespace The namespace of the message
 * @param {string} message A message string
 */
function receiverMessage(namespace, message) {
  appendMessage("receiverMessage: "+namespace+", "+message);
};

/**
 * receiver listener during initialization
 */
function receiverListener(e) {
  if( e === 'available' ) {
    appendMessage("receiver found");
   // load();
  }
  else {
    appendMessage("receiver list empty");
  }
}

/**
 * stop app/session
 */
function stopApp() {
  session.stop(onStopAppSuccess, onError);
}

/**
 * send a message to the receiver using the custom namespace
 * receiver CastMessageBus message handler will be invoked
 * @param {string} message A message string
 */
function sendMessage(message) {
  if (session!=null) {
    session.sendMessage(namespace, message, onSuccess.bind(this, "Message sent: " + message), onError);
  }
  else {
    chrome.cast.requestSession(function(e) {
        session = e;
        session.sendMessage(namespace, message, onSuccess.bind(this, "Message sent: " + message), onError);
      }, onError);
  }
}

/**
 * append message to debug message window
 * @param {string} message A message string
 */
function appendMessage(message) {
  console.log(message);
//    var dw = document.getElementById("debugmessage");
//    dw.innerHTML += '\n' + JSON.stringify(message);
};

/**
 * utility function to handle text typed in by user in the input field
 */
function update() {
  sendMessage(document.getElementById("input").value);
}

/**
 * handler for the transcribed text from the speech input
 * @param {string} words A transcibed speech string
 */
function transcribe(words) {
    sendMessage(words);
}

</script>

</head>

我调用函数转录,并将消息发送到我写的chrome接收器。它在桌面版上完美运行。但是在电话上,它一直在说连接到chrome cast。

有人知道在电话上工作是否有任何强制设置?

1 个答案:

答案 0 :(得分:0)

我已经研究了越来越多,并在Stackflow上找到了关于这个问题的一些其他线程。

How can I send a message to a custom Google Cast Receiver from Chrome for iOS?

似乎不可能这样做,因为它没有集成到手机上的镀铬。

然而,有人知道chromecats是否支持其他方式来沟通顺便说一句。发件人和收件人?