0

Chromcast Remote player seek to resume at seek position 0 when using nanohttpd server. Main issue getting when I seek into video player in device its working fine but on TV seek-bar set at 0 position and music stat at beginning.

When to call mRemoteMediaPlayer.seek() in onSeekChanged() getting result success but on TV seek-bar set at 0 position and music stat at beginning.

public class webserver extends NanoHTTPD {  
        FileInputStream fileInputStream;
        public webserver(){
                super(8080);
        }
        @Override
        public Response serve(String uri, Method method, Map<String, String> header,Map<String, String> parameters, Map<String, String> files) {
          String mediasend=" ";
            long size=0;
          FileInputStream fis = null;
          try { 
                 fis = new FileInputStream(path);
              //byte[] buffer =   new byte[(int) fis.getChannel().size()];
              size=fis.getChannel().size();

          } catch (Exception e) {
                    e.printStackTrace();
        }


        switch(mediatype){
          case "photo":
             mediasend="image/jpeg";
             break;
          case "audio":
             mediasend="audio/mp3";
             break;
          case "video":
             mediasend="video/mp4"; 
             break;
        }

        return new NanoHTTPD.Response(com.castoffline.castActivity.NanoHTTPD.Response.Status.OK,mediasend,fis,size);
       }   
    }

Cast connection code

Cast.CastApi.launchApplication(mApiClient,getString(R.string.app_id),false).setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {
                    @Override
                    public void onResult(ApplicationConnectionResult result) {
                    Status status = result.getStatus();
                    if (status.isSuccess()) {
                        ApplicationMetadata applicationMetadata = result.getApplicationMetadata();
                        mSessionId = result.getSessionId();
                        String applicationStatus = result.getApplicationStatus();
                        boolean wasLaunched = result.getWasLaunched();
                        Log.d(TAG,"application name: "+ applicationMetadata.getName()+ ", status: "+ applicationStatus+ ", sessionId: "+ mSessionId+ ", wasLaunched: "+ wasLaunched);
                        mApplicationStarted = true;
                        mRemoteMediaPlayer = new RemoteMediaPlayer();
                        /*
                         * Identify the mediatype and send the metadata details to media info       
                         */
                        switch(mediatype)   
                        {   
                            case "audio" :  mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
                                            mediaMetadata.putString(MediaMetadata.KEY_TITLE, "MY MUSIC TRACK"+":  "+audioTitle);
                                            mediaMetadata.putString(MediaMetadata.KEY_ARTIST,audioArtist);
                                            mediaMetadata.addImage(new WebImage(Uri.parse("https://www.googledrive.com/host/0B61ekPEN_94sZ21mcnQtbVU2RHM/media.png")));
                                            mediaInfo = new MediaInfo.Builder(ipdevice).setContentType(mimetype).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(mediaMetadata).build();
                            break;
                            case "video" :  mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
                                            mediaMetadata.addImage(new WebImage(Uri.parse("https://www.googledrive.com/host/0B61ekPEN_94sZ21mcnQtbVU2RHM/film_reel.png")));
                                            mediaMetadata.putString(MediaMetadata.KEY_TITLE, "My MOVIE"+":  "+videoTitle);
                                            mediaInfo = new MediaInfo.Builder(ipdevice).setContentType(mimetype).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(mediaMetadata).build();
                            break;
                            case "photo" :  mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_PHOTO);
                                            mediaMetadata.putString(MediaMetadata.KEY_TITLE, "My PHOTO"+":  ");
                                            mediaInfo = new MediaInfo.Builder(ipdevice).setContentType(mimetype).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(mediaMetadata).build();
                            break;
                            default:
                         }
                        try {
                                Cast.CastApi.setMessageReceivedCallbacks(mApiClient,mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer);
                             } catch (IOException e) {
                                                Log.d(TAG, "Exception while creating media channel", e);
                             }
                        try {

                                mRemoteMediaPlayer.load(mApiClient, mediaInfo, false,0).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
                                @Override
                                public void onResult(MediaChannelResult result) {
                                    if (result.getStatus().isSuccess()) {
                                                        Log.d(TAG, "Media loaded successfully");
                                    }
                                }});
                                /*
                                 * checks if the video is playing or if it is paused and according it will be played/paused in the receiver
                                 */
                                videoview.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
                                AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
                                @Override
                                public void onPlay() {
                                    playbackPaused=false;  //videoView is playing
                                    if(mSelectedDevice!=null && mApiClient != null && mRemoteMediaPlayer != null){
                                        //volume is set to mute if media is casting in Chromecast
                                        amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                                        sendMediaControl(playbackPaused,false);
                                    }else{
                                             amanager.setStreamVolume(AudioManager.STREAM_MUSIC, 3,1);
                                          }

                                 }
                                 @Override
                                 public void onPause(){
                                    playbackPaused=true; //videoView is paused
                                    if (mSelectedDevice != null && mApiClient != null && mRemoteMediaPlayer != null){
                                        amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
                                        sendMediaControl(playbackPaused,false);
                                    }else{
                                        amanager.setStreamVolume(AudioManager.STREAM_MUSIC, 3,1); }
                                 }
                                 /* Currently Seek function is not working for the media playback while casting
                                  * (non-Javadoc)
                                  * @see com.castoffline.castActivity.CustomVideoView.PlayPauseListener#onSeekChanged(int)
                                  */


                                @Override
                                 public void onSeekChanged(int pos){
                                     Log.d(String.valueOf(videoview.getCurrentPosition()),"seekinsie");
                                    // seek(videoview.getCurrentPosition());

                                    Log.d("mimetype ",mimetype);

                                    Log.d("seek1",""+pos);

                                    if (mSelectedDevice != null && mApiClient != null && mRemoteMediaPlayer != null){

                                        videoview.pause();
                                        final long position=videoview.getCurrentPosition();
                                        Log.d("seek",""+position);


                                        mRemoteMediaPlayer.seek(mApiClient,position,RemoteMediaPlayer.RESUME_STATE_UNCHANGED).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>(){
                                            @Override
                                            public void onResult(MediaChannelResult result) {
                                                if (result.getStatus().isSuccess()) {
                                                    Log.d(String.valueOf("State Code "+result.getStatus().getStatusCode()),""+mRemoteMediaPlayer.getApproximateStreamPosition());

                                                }
                                            }
                                        });
                                        mRemoteMediaPlayer.setOnStatusUpdatedListener(new RemoteMediaPlayer.OnStatusUpdatedListener(){
                                            @Override
                                            public void onStatusUpdated() {
                                                @SuppressWarnings("unused")
                                                MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus();
                                                Log.d("seek state update",""+mediaStatus);

                                            }
                                        });
                                    }


                                }
                             });
                        } catch (IllegalStateException e) {
                                Log.d(TAG, "Problem occurred with media during loading", e);
                } catch (Exception e) {
                                Log.d(TAG, "Problem opening media during loading", e);}
        } else {
                    Log.e(TAG,"application could not launch");
                    teardown();
                }
    }
});
}

Remote player control code.

private void sendMediaControl(final boolean playbackPaused,final boolean change)
{
    if (mApiClient != null && mRemoteMediaPlayer != null){
        mRemoteMediaPlayer.requestStatus(mApiClient).setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
        @Override 
        public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {

                if(playbackPaused ==true){
                    mRemoteMediaPlayer.pause(mApiClient);
                }else{
                    mRemoteMediaPlayer.play(mApiClient);

                }               
        }
    });
    }
}
kishan
  • 54
  • 11

1 Answers1

0

There can be two things here:

  1. I don't know if it is a limitation of nanohttpd or a configuration issue but what you are seeing is because the nanaohttpd (at least the way you have configured it) doesn't support seek. When you do a seek, your receiver will call into your http server (nanohttpd in this case) and passes a position and asks the web server to seek to that position and start streaming from there. If the web server doesn't support that, you will not be able to seek successfully. As a test, set up an apache server on your laptop, just for testing, and point to that instead of your embedded web server and see if that works or not.
  2. There might be a mismatch between units of position; so if, say, your local player is using seconds and is reporting, say, 60 when it is a minute into the content and if you send that to the cast receiver, it will be interpreted as 60 milliseconds which is practically same as 0 seconds, so check on that too.
Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • Might be nanaohttpd doesn't support seek. You have any other way for play local mobile song on chrome-cast with seek function supported? – kishan May 25 '16 at 12:41