问题
I have a radio application which plays live radio stations on Google Chromecast. Starting a stream with remoteMediaPlayer.load(...) works just fine.
But remoteMediaPlayer.requestStatus(...)
for later stopping the stream says statusCode=SIGN_IN_REQUIRED
and throws a IllegalStateException: No current media session
Why can't I stop the playing live radio stream? Or how can I stop the remoteMediaPlayer playback?
The live radio streams are HTTP. Can it be the problem?
Here the details:
The following code starts the stream and it works.
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 .... {
The following code stops the stream and it does not work.
The result ist statusCode=SIGN_IN_REQUIRED
and java.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)
回答1:
toString() isn't really a good way to get the status code. RemoteMediaPlayer has its own set of STATUS_* codes. SIGN_IN_REQUIRED is 4, which is RemoteMediaPlayer.STATUS_REPLACED which means that you issued another request of the same type as this request. The RemoteMediaPlayer, by design, will only track one request of each type. For example, if you send a PAUSE followed by another PAUSE before the first PAUSE request has completed (i.e., received a reply from the receiver), it will stop tracking the first PAUSE and report this error.
回答2:
Is there a reason you want to use stop
rather than, say pause
? You can call stop
but be aware that after calling stop
, you need to re-attach media channel and then load the next media, that is why just using pause
is more convenient in this case. If you want to load another stream, you don't need to call pause
either; simply loading a second media while the first one is playing would work well.
来源:https://stackoverflow.com/questions/23853777/remotemediaplayer-requeststatus-returns-statuscode-sign-in-required