How to implement own cast.receiver.media.Player

670 Views Asked by At

Preface

I have own player with own native audio engine. Recently I decided to add cast functionality to it. To 'stream' audio to custom cast application I decided to use WebSocket and send float32 PCM over it and then play via WebAudio API. Because custom audio format, I decided to write my own cast.receiver.media.Player implementation sample.MyPlayer

Problem

Once I call play or pause on com.google.android.gms.cast.RemoteMediaPlayer I got

java.lang.IllegalStateException: No current media session

Before call play of course I do com.google.android.gms.cast.RemoteMediaPlayer.load and here interesting moment. And for some reason sample.MyPlayer didn't receive mediaSessionId looks like this is related to exception on java side somehow. But my sample.MyPlayer.load on js side called.

Fragment of log form my application

cast_receiver.js:61 [ 21.034s] [cast.receiver.MediaManager] Load message received:{"requestId":2,"media":{ ... },"autoplay":false,"currentTime":0}

Fragment of log from default receiver sample

cast_receiver.js:61 [ 30.243s] [cast.receiver.MediaManager] Load message received:{"type":"LOAD","requestId":2,"mediaSessionId":0,"customData":null,"media":{ ... },"autoplay":true,"currentTime":0}

As you can see payload in my custom receiver have missing mediaSessionId, type, customData keys

This all I found about a problem.

My player.html

<!DOCTYPE html>
<html>
<head>
  <title>Cast Reference Receiver</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="player.css" />
  <script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script>
  <script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/mediaplayer/1.0.0/media_player.js"></script>
  <script type="text/javascript" src="player.js"></script>
</head>
<body style="margin: 0">
    <div id="player" class="player">
        <div class="logo"></div>
        <div class="spinner"></div>
        <div class="watermark"></div>
        <div class="gradient"></div>
        <div class="overlay">
            <div class="media-info">
                <div class="media-artwork"></div>
                <div class="media-text">
                    <div class="media-title"></div>
                    <div class="media-subtitle"></div>
                </div>
            </div>
            <div class="preview-mode-info">
                <div class="preview-mode-artwork"></div>
                <div class="preview-mode-text">
                    <div class="preview-mode-timer">
                        <div class="preview-mode-timer-starts">Up next in&nbsp;</div>
                        <div class="preview-mode-timer-countdown"></div>
                        <div class="preview-mode-timer-sec">&nbsp;secs...</div>
                    </div>
                    <div class="preview-mode-title"></div>
                    <div class="preview-mode-subtitle"></div>
                </div>
            </div>
            <div class="controls">
                <span class="controls-play-pause"></span>
                <span class="controls-cur-time"></span>
                <span class="controls-total-time"></span>
                <div class="controls-progress">
                    <div class="controls-progress-inner progressBar"></div>
                    <div class="controls-progress-thumb"></div>
                </div>
            </div>
        </div>
    </div>
    <script>
      var mediaManager_ = new cast.receiver.MediaManager(new sample.MyPlayer(), [
            cast.receiver.media.Command.LOAD, 
            cast.receiver.media.Command.PLAY, 
            cast.receiver.media.Command.STOP, 
            cast.receiver.media.Command.GET_STATUS,
            cast.receiver.media.Command.PAUSE, 
            cast.receiver.media.Command.STREAM_VOLUME,
            cast.receiver.media.Command.QUEUE_NEXT,
            cast.receiver.media.Command.QUEUE_PREV
        ]);
      var receiverManager = cast.receiver.CastReceiverManager.getInstance();
      receiverManager.start();
    </script>
</body>
</html>

Questions

  1. Is my approach generally right?
  2. Where can I found any reference implementation of cast.receiver.media.Player ? or any recommendations how to implement it properly?
  3. What is the problem java.lang.IllegalStateException: No current media session ?
0

There are 0 best solutions below