我已经研究这个几个星期了,但我真的被这个部分难住了。因此,我正在将Google Cast实现到我继承的构造不佳的代码库中。
具体来说,我使用这个源代码作为参考,因为代码库完全是用Java编写的,因为一些任意的原因:https://github.com/googlecast/CastVideos-android
更具体地说,我现在要做的就是得到mCastContext = CastContext.getSharedInstance(this,localExecutor).getResult();
行在VideoBrowserActivity工作在我的项目。然而,我在实现它时得到这个错误,而不是当我运行其他项目时:无法启动活动ComponentInfo{}: java.lang.IllegalStateException:任务尚未完成
下面分别是我的工作和参考文件的重要部分:
public class VideoExoPlayerActivity extends BaseActivity implements OnClickHandlerInterface {
private ActivityVideoExoPlayerBinding binding;
private VideoPlayerViewModel viewModel;
private String videoUuid;
private ExoPlayer exoPlayer;
private DefaultTrackSelector trackSelector;
private String drmLicenseUrl;
private String mimeType;
private Boolean isDashDRM;
private AudioManager manager;
private boolean isScreenLock = false;
private boolean isShortVideo = false;
private boolean isRollOverVideoAvailable = false;
private boolean isRollOverShow = false;
private boolean isRollOverDismiss = false;
private int currentPosition = 0;
private int continuePosition = -1;
private List<VideoUuidURLModel> list_url = new ArrayList<>();
private List<VideoPlayerDataResponse.Data> list_video = new ArrayList<>();
private PictureInPictureParams.Builder mPictureInPictureParamsBuilder;
private CastContext mCastContext;
private CastSession mCastSession;
private MediaRouteButton mMediaRouteButton;
private SessionManager mSessionManager;
private SessionManagerListener<CastSession> mSessionManagerListener =
new SessionManagerListenerImpl();
private Executor localExecutor = Executors.newSingleThreadExecutor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_video_exo_player);
viewModel = new ViewModelProvider(this, new CommonFactory<>(this)).get(VideoPlayerViewModel.class);
mCastContext = CastContext.getSharedInstance(this, localExecutor).getResult();
initView();
hideStatusBar();
startService(new Intent(this, HistoryAPIBackgroundService.class));
}
public class VideoBrowserActivity extends AppCompatActivity {
private static final String TAG = "VideoBrowserActivity";
private CastContext mCastContext;
private final SessionManagerListener<CastSession> mSessionManagerListener =
new MySessionManagerListener();
private CastSession mCastSession;
private MenuItem mediaRouteMenuItem;
private MenuItem mQueueMenuItem;
private Toolbar mToolbar;
private IntroductoryOverlay mIntroductoryOverlay;
private CastStateListener mCastStateListener;
private Executor localExecutor = Executors.newSingleThreadExecutor();
private class MySessionManagerListener implements SessionManagerListener<CastSession> {
@Override
public void onSessionEnded(CastSession session, int error) {
if (session == mCastSession) {
mCastSession = null;
}
invalidateOptionsMenu();
}
@Override
public void onSessionResumed(CastSession session, boolean wasSuspended) {
mCastSession = session;
invalidateOptionsMenu();
}
@Override
public void onSessionStarted(CastSession session, String sessionId) {
mCastSession = session;
invalidateOptionsMenu();
}
@Override
public void onSessionStarting(CastSession session) {
}
@Override
public void onSessionStartFailed(CastSession session, int error) {
}
@Override
public void onSessionEnding(CastSession session) {
}
@Override
public void onSessionResuming(CastSession session, String sessionId) {
}
@Override
public void onSessionResumeFailed(CastSession session, int error) {
}
@Override
public void onSessionSuspended(CastSession session, int reason) {
}
}
/*
* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_browser);
setupActionBar();
mCastStateListener = new CastStateListener() {
@Override
public void onCastStateChanged(int newState) {
if (newState != CastState.NO_DEVICES_AVAILABLE) {
showIntroductoryOverlay();
}
}
};
mCastContext = CastContext.getSharedInstance(this,localExecutor).getResult();
}
有一些东西在我看来是缺失的,但我已经尝试实现你可以看到的一切,没有什么改变我的结果。请询问是否需要查看代码的其他部分,因为我不知道这个问题是否可能来自其他地方。据我所知,几乎没有关于在Java中实现此功能的教程,所以我非常盲目地这样做,而且非常困难,所以我真的很感谢我能得到的任何帮助。
这并不能直接回答你的问题,为什么你的应用程序的行为不同于google cast参考应用程序,但你可以添加OnCompleteListener,然后设置上下文:
芬兰湾的科特林:
CastContext.getSharedInstance(this, castExecutor).addOnCompleteListener {
castContext = it.result
castContext?.addCastStateListener(this)
}
Java:
Task task = CastContext.getSharedInstance(this,localExecutor);
task.addOnCompleteListener(task1 -> mCastContext = (CastContext) task1.getResult());
// or
Task task = CastContext.getSharedInstance(this,localExecutor);
task.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
mCastContext = (CastContext)task.getResult();
}
});
我被困在同一点(今天的大部分时间),你试图找出为什么CastContext。castExecutor getSharedInstance(这)。结果给出"任务尚未完成";在我自己的应用程序例外,而不是参考应用程序,即使我已经按照相同的步骤,我可以告诉。
这不是最好的选择,我猜,但如果你想在主线程中获得对象,这对我来说是有效的。Java版本:
public CastContext getCastContext(Context context) {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
CompletableFuture<CastContext> castContextFuture = new CompletableFuture<>();
castContextFuture.complete(CastContext.getSharedInstance(context, executor).getResult());
return castContextFuture.get();
}
} catch (Exception e) {
throw new IllegalStateException("Error: couldn't get CastContext", e);
}
return null;
}
芬兰湾的科特林版:
fun getCastContext(context: Context) : CastContext {
val executor = Executors.newSingleThreadExecutor()
val castContextTask: Task<CastContext> = CastContext.getSharedInstance(context, executor)
return try {
runBlocking {
castContextTask.await()
}
} catch (e : Exception) {
throw IllegalStateException("error couldn't get CastContext")
}
}