remoteMediaPlayer.requestStatus(...)返回statusCode SIGN_IN_REQUIRED

时间:2014-05-25 09:17:37

标签: android google-play-services chromecast google-cast

我有一个广播应用程序,可以在Google Chromecast上播放直播电台。使用remoteMediaPlayer.load(...)启动流可以正常工作。 但remoteMediaPlayer.requestStatus(...)以后停止流说statusCode=SIGN_IN_REQUIRED并抛出IllegalStateException: No current media session

为什么我无法停止播放实时广播流?或者我该如何停止remoteMediaPlayer播放?

直播无线电流是HTTP。这可能是问题吗?

详情如下:

以下代码启动流并且它可以正常工作。

    MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
    mediaMetadata.putString(MediaMetadata.KEY_TITLE, stationName);
    mediaMetadata.addImage(new WebImage(Uri.parse(imageUrl)));
    final MediaInfo mediaInfo = new MediaInfo.Builder(stationUrl).setContentType("audio/mp3").setStreamType(MediaInfo.STREAM_TYPE_LIVE)
            .setMetadata(mediaMetadata).build();
    try {
        mRemoteMediaPlayer.load(apiClient, mediaInfo, true).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
            @Override
            public void onResult(MediaChannelResult result) {
                if (result.getStatus().isSuccess()) {
                    Utils.log(TAG, "Media loaded successfully: " + result.getStatus());
                } else {
                    Utils.log(TAG, "Media loaded NOT successfully: " + result.getStatus());
                }
            }
        });
    } catch .... {

以下代码会停止流,但它不起作用。 结果是statusCode=SIGN_IN_REQUIREDjava.lang.IllegalStateException: No current media session

public static void stop() {
    try {
        mRemoteMediaPlayer.requestStatus(apiClient).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {

            @Override
            public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
                Status status = mediaChannelResult.getStatus();
                Utils.log(TAG, "RemoteMediaPlayer requestStatus: Status=" + status.getStatus());
                try {
                    mRemoteMediaPlayer.stop(apiClient);
                } catch (Exception e) {
                    Log.e(TAG, "Exception while stopping GoogleApiClient. ", e);
                }
            }
        });
    } catch (IllegalStateException e) {
        Log.e(TAG, "Problem occurred while geting requestStatus", e);
    } catch (Exception e) {
        Log.e(TAG, "Exception while geting requestStatus. ", e);
    }
}

logcat的:

05-25 06:19:58.360: D/CastHelper(30561): RemoteMediaPlayer requestStatus: Status=Status{statusCode=SIGN_IN_REQUIRED, resolution=null}
05-25 06:19:58.360: E/CastHelper(30561): Exception while stopping GoogleApiClient. 
05-25 06:19:58.360: E/CastHelper(30561): java.lang.IllegalStateException: No current media session
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.internal.es.dC(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.internal.es.b(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.cast.RemoteMediaPlayer$4.a(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.cast.RemoteMediaPlayer$4.a(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.common.api.a$b.b(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.common.api.b.a(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.common.api.b.b(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.cast.RemoteMediaPlayer.stop(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.cast.RemoteMediaPlayer.stop(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.mydomain.myapp.CastHelper$8.onResult(CastHelper.java:97)
05-25 06:19:58.360: E/CastHelper(30561):    at com.mydomain.myapp.CastHelper$8.onResult(CastHelper.java:1)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.common.api.a$c.b(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at com.google.android.gms.common.api.a$c.handleMessage(Unknown Source)
05-25 06:19:58.360: E/CastHelper(30561):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 06:19:58.360: E/CastHelper(30561):    at android.os.Looper.loop(Looper.java:136)
05-25 06:19:58.360: E/CastHelper(30561):    at android.app.ActivityThread.main(ActivityThread.java:5579)
05-25 06:19:58.360: E/CastHelper(30561):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 06:19:58.360: E/CastHelper(30561):    at java.lang.reflect.Method.invoke(Method.java:515)
05-25 06:19:58.360: E/CastHelper(30561):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-25 06:19:58.360: E/CastHelper(30561):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-25 06:19:58.360: E/CastHelper(30561):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

toString()实际上并不是获取状态代码的好方法。 RemoteMediaPlayer有自己的一组STATUS_ *代码。 SIGN_IN_REQUIRED为4,即RemoteMediaPlayer.STATUS_REPLACED,这意味着您发出了与此请求相同类型的另一个请求。 RemoteMediaPlayer按设计只跟踪每种类型的一个请求。例如,如果在第一个PAUSE请求完成之前发送PAUSE后跟另一个PAUSE(即收到接收方的回复),它将停止跟踪第一个PAUSE并报告此错误。

答案 1 :(得分:1)

您是否有理由使用stop而不是pause?您可以致电stop,但请注意,在致电stop后,您需要重新连接媒体频道,然后加载下一个媒体,这就是为什么只使用pause更方便案件。如果您要加载其他信息流,则无需拨打pause;只需在第一个媒体播放时加载第二个媒体就可以正常运行。