我何时开始施法?

时间:2014-08-05 05:50:20

标签: android casting chromecast

嗨,只想通过手机上的给定网址投射音频mp3文件。在我的应用程序中,我建立连接时启动了转换方法,这意味着在调用setSelection方法并初始化GoogleApiClient实例之后。但是铸造不会发生,不管我是否正确,如果没有,那么我什么时候开始铸造。请指导我开始铸造及其生命周期。 在此先感谢。

我的代码部分是

private void setSelectedDevice(CastDevice device) {
    Log.d(Tag, "setSelectedDevice: " + device);
    mSelectedDevice = device;

    if (mSelectedDevice != null) {

        try {
            disconnectApiClient();
            connectApiClient();
        } catch (IllegalStateException e) {
            Log.w(Tag, "Exception while connecting API client", e);
            disconnectApiClient();
        }
    } else {
        if (mApiClient != null) {
            if (mApiClient.isConnected()) {
                if (mRemoteMediaPlayer != null && isPlaying) {
                    mRemoteMediaPlayer.pause(mApiClient);
                }
            }
            disconnectApiClient();
        }
        mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
    }
}

private void connectApiClient() {
    Cast.CastOptions apiOptions = Cast.CastOptions.builder(mSelectedDevice,
            mCastListener).build();
    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(Cast.API, apiOptions)
            .addConnectionCallbacks(mConnectionCallbacks)
            .addOnConnectionFailedListener(mConnectionFailedListener)
            .build();

    // Start the casting when connected the route device  // here i am start the casting....

    if(songURL != null){
        Log.d(Tag, "Current casting url:" + songURL);
        isPlaying = false;
        firstTimeCasting = true;
        SynchwithChromeCast(songURL);    // here load the song url with remoteplayer
        //Play(songURL);
    }
}

private void SynchwithChromeCast(String file){
    if(firstTimeCasting){
        firstTimeCasting = false;

        try {

            Log.e("Play Testing", "mRemoteMediaPlayer is play");

            if(firstTimeCasting){
                firstTimeCasting = false;

            //mRemoteMediaPlayer = new RemoteMediaPlayer();

            isPlaying = true;

            MediaMetadata mediaMetadata = new MediaMetadata(
                    MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
            mediaMetadata.putString(MediaMetadata.KEY_ALBUM_ARTIST,
                    "" +singers.getText().toString());
            mediaMetadata.putString(mediaMetadata.KEY_ALBUM_TITLE,
                    "" +songTitle.getText().toString());
            MediaInfo mediaInfo = new MediaInfo.Builder(file)
                    .setContentType("audio/mp3")
                    .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                    .setMetadata(mediaMetadata).build();
            mRemoteMediaPlayer
                    .load(mApiClient, mediaInfo, true)
                    .setResultCallback(
                            new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {

                                @Override
                                public void onResult(
                                        MediaChannelResult result) {
                                    if (result.getStatus().isSuccess()) {
                                        Log.d(Tag, "Media loaded Successfully" + result.getStatus());
                                    }
                                    else{
                                        Log.d(Tag, "Media loaded Not Successfully" + result.getStatus());
                                    }
                                }
                            });
            }

        } catch (IllegalStateException e) {
            Log.e(Tag, e.getMessage());
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(Tag, e.getMessage());
        }
    }
}
// activate play button
    play.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {

            // disable the button
            if (PlusUtilities.isInternetConnected()) {
                play.setEnabled(false);

                // if mediaplayer is in pause state
                if (mp != null & !mp.isPlaying()) {

                    mp.seekTo(mp.getCurrentPosition());
                    playSong(false);
                    updateProgressBar();
// Interact with Chrome-cast while play/pause    ////////

                    //reset
                    firstTimeCasting = false;

                    isPlaying = true;
                    Play(songURL);   // Play or Pause the chromecast receiver
                    }
                    else if (mp != null) {

                    playSong(true);
                    //if(songURL != null)
                    isPlaying = false;
                        Play(songURL);
                }

    });
private void Play(String file) {

    if (mp != null && isPlaying) {
        if (mApiClient != null) {
            Log.e("Play **********", mApiClient.isConnected() + " ++++++++Status");
            if (mApiClient.isConnected()) {
                if (mRemoteMediaPlayer != null) {
                    mRemoteMediaPlayer.pause(mApiClient);
                }
            }
        }
    } else if (mp != null && !isPlaying && !firstTimeCasting) {
        if (mApiClient != null) {
            if (mApiClient.isConnected()) {
                if (mRemoteMediaPlayer != null) {
                    mRemoteMediaPlayer.play(mApiClient);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以查看GitHub上的一些示例,了解流程应该如何;它是一组异步调用,所以

  • 调用connect后,等待onConnected被调用,表示连接
  • 然后使用您在Developer Console上分配的应用ID或默认应用ID来加载“应用”
  • 成功启动应用程序后,您可以加载媒体,然后您可以按照自己的意愿进行播放或播放。

阅读我们的documentation