在断开与演员的联系之前,是否有可能从VideoCastManager
获得当前的播放媒体?
我想在用户手动断开连接时保存当前媒体的最后进度。我使用VideoCastCosumer
方法onDisconnected()
,但它抛出异常。不确定是什么类型的异常,因为我的日志只显示这个:
System.err: at com.economist.newton.mobile.ui.activity.BaseCastActivity$1.onDisconnected (MyCastActivity.java:81)
该行包含onDisconnected()
方法中的以下行:
try {
if(castManager.getCurrentMediaPosition() > 30000){ // <-here error occurs
...
}
} catch (TransientNetworkDisconnectionException | NoConnectionException e) {
e.printStackTrace();
}
如果有任何帮助,castManager
变量在onCreate()
中声明:
castManager = VideoCastManager.getInstance();
演员伴侣图书馆:com.google.android.libraries.cast.companionlibrary:ccl:2.8.4
有几种方法可以做到这一点。我认为最容易的是拥有一个实现ProgressWatcher
的组件(来自CCL)。在那个监听器中,setProgress(int currentPosition, int duration)
将每秒调用一次,它将流的currentPosition作为其第一个参数。您可以缓存此值(如果您的设备断开连接,它将不会更新),以便为您提供有关当前位置的实时信息。例如,如果您的活动实现了此接口:
public MyActivity extends AppCompatActivity implements ProgressWatcher {
private int mCurrentStreamPosition;
public onStart() {
....
mCastManager.addProgressWatcher(this);
....
}
public onStop() {
....
mCastManager.removeProgressWatcher(this);
....
}
public setProgress(int currentTime, int duration) {
mCurrentStreamPosition = currentTime;
}
public int getCurrentStreamPosition() {
return mCurrentStreamPosition;
}
....
}
所以无论何时你想,你都可以调用getCurrentStreamPosition()(例如,你可以在onDisconnected()
中调用它)来获取最后/当前流的位置。请注意,根据您应用的结构,您可以决定在ProgressWatcher
/ onResume()
或onPause()
/ onCreate()
中注册/取消注册您的onDestroy()
。重要的是确保取消注册以避免泄漏内存。
对于Castv3,你可以从onSessionEnding
覆盖SessionManagerListener
并获得mRemoteMediaClient.getMediaStatus().getStreamPosition()