当使用nanohttpd时,Chromcast Remoteplayer寻求在寻找0位置恢复

时间:2016-05-25 06:32:40

标签: android chromecast

Chromcast远程播放器在使用nanohttpd服务器时寻求在搜索位置0恢复。当我在设备中寻找视频播放器时工作正常,但在电视搜索栏设置为0位置和音乐统计开始时的主要问题。

何时在onSeekChanged()中调用mRemoteMediaPlayer.seek()获得结果成功,但在电视搜索栏设置为0位置并开始音乐统计。

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.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();
                }
    }
});
}

远程播放器控制代码。

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);

                }               
        }
    });
    }
}

1 个答案:

答案 0 :(得分:0)

这里可以有两件事:

  1. 我不知道这是nanohttpd的限制还是配置问题,但你看到的是因为nanaohttpd(至少你配置它的方式)不支持搜索。当您进行搜索时,您的接收器将调用您的http服务器(在本例中为nanohttpd)并传递一个位置并要求Web服务器寻找该位置并从那里开始流式传输。如果Web服务器不支持,您将无法成功搜索。作为测试,在您的笔记本电脑上设置一个apache服务器,仅用于测试,并指向它而不是您的嵌入式Web服务器,看看它是否有效。
  2. 位置单位之间可能存在不匹配;所以,如果你的本地玩家正在使用秒并报告,比如60,当它进入内容的一分钟时,如果你把它发送到转换接收器,它将被解释为60毫秒,几乎与0秒相同,请检查一下。