Chromecast自定义命令无效

时间:2013-08-15 20:40:02

标签: android google-cast chromecast

有没有人能够在ChromeCast API中添加自定义命令?我成功地将TicTacToe示例与我的开发人员ID以及修改后的协议字符串一起使用(在客户端和服务器上都进行了更改)。

在Android方面,我有现有的“join”命令可以使用,我正在添加一个新的“图像”命令:

public final void join(String name) {
    try {
        Log.d(TAG, "join: " + name);
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_JOIN);
        payload.put(KEY_NAME, name);
        sendMessage(payload);
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to join a game", e);
    } catch (IOException e) {
        Log.e(TAG, "Unable to send a join message", e);
    } catch (IllegalStateException e) {
        Log.e(TAG, "Message Stream is not attached", e);
    }
}

public final void sendImage(String sURL) {
    try {
        Log.d(TAG, "sendImage");
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_IMAGE);
        payload.put(KEY_URL, sURL);
        sendMessage(payload);
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to send image", e);
    } catch (IOException e) {
        Log.e(TAG, "Unable to send an image message", e);
    } catch (IllegalStateException e) {
        Log.e(TAG, "Message Stream is not attached", e);
    }
}

如果我调用join命令,它工作正常,我可以在浏览器中看到通过控制台记录的消息。但是如果我调用sendImage函数,我会收到以下错误:

“onEnded无法连接频道:协议错误”

在ChromeCast端,我可以看到收到有效命令的时间。发送join命令时会调用此函数,但在发送自定义“image”命令时则不会调用此函数。

/**
 * Message received event; determines event message and command, and
 * choose function to call based on them.
 * @param {event} event the event to be processed.
 */
onMessage: function(event) {
  console.log('***== pre onMessage ==***');
  var message = event.message;
  var channel = event.target;
  console.log('********onMessage********' + JSON.stringify(message));
  console.log('mPlayer1: ' + this.mPlayer1);
  console.log('mPlayer2: ' + this.mPlayer2);

  if (message.command == 'join') {
    this.onJoin(channel, message);
  } else if (message.command == 'leave') {
    this.onLeave(channel);
  } else if (message.command == 'move') {
    this.onMove(channel, message);
  } else if (message.command == 'queue_layout_request') {
    this.onQueueLayoutRequest(channel);
  } else if (message.command == 'image') {
    this.onImage(channel, message);
  } else if (message.command == 'video') {
    this.onVideo(channel, message);
  } else if (message.command == 'song') {
    this.onSong(channel, message);
  } else {
    cast.log.error('Invalid message command: ' + message.command);
  }
},

有什么想法吗?我还需要在其他地方定义自定义命令吗?

EDITED:还展示了onImage原型:

/**
 * Image event: display an image
 * @param {cast.receiver.channel} channel the source of the move, which
 *     determines the player.
 * @param {Object|string} message contains the URL of the image
 */
onImage: function(channel, message) {
  console.log('****onImage: ' + JSON.stringify(message));

  //Hide video and show image 
  mVideo.style.visibility='hidden';
  mImage.style.visibility='visible';
  mImage.src = message.url;

},

1 个答案:

答案 0 :(得分:1)

这通常意味着您的接收器中存在JavaScript错误。在您的ChromeCast设备的IP地址上打开端口9222上的Chrome,并使用Chrome开发人员工具调试此问题。

您是否在接收器原型中为消息处理程序声明了一个新函数“onImage”?

相关问题