简体   繁体   English

在项目中实施 Google Cast 时如何解决 Task is not yet complete 异常?

[英]How can I resolve Task is not yet complete exception when implementing Google Cast into project?

I've been working on this for weeks no joke but I'm really stumped at this one part.我已经为此工作了数周,这不是开玩笑,但我真的对这一部分感到困惑。 So I'm in the process of implementing Google Cast into a poorly constructed codebase that I inherited.因此,我正在将 Google Cast 实施到我继承的构造不佳的代码库中。

Specifically, I'm using this source as reference since the codebase was written entirely in Java for some arbitrary reason: https://github.com/googlecast/CastVideos-android具体来说,我使用这个源作为参考,因为代码库出于某种任意原因完全用 Java 编写: https ://github.com/googlecast/CastVideos-android

And to be more specific, all I'm trying to do right now is get the更具体地说,我现在要做的就是让

mCastContext = CastContext.getSharedInstance(this,localExecutor).getResult();

line in VideoBrowserActivity to work in my project. VideoBrowserActivity 中的行在我的项目中工作。 However, I am getting this error when implementing it but not when I run the other project: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Task is not yet complete但是,我在实施它时遇到此错误,但在运行其他项目时却没有:无法启动活动 ComponentInfo{}: java.lang.IllegalStateException: Task is not yet complete

Here are the important parts of my work and the reference file, respectively:下面分别是我工作的重要部分和参考文件:

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();
    }

There are some things in mine that look to be missing, but I've tried implementing everything you can see already and nothing has changed my result.我的一些东西看起来似乎缺失了,但我已经尝试实施你已经看到的一切,但没有任何改变我的结果。 Please ask if you need to see other parts of the code because I have no idea if this problem could be coming from somewhere else.请询问您是否需要查看代码的其他部分,因为我不知道这个问题是否来自其他地方。 From what I can tell, there are next to no tutorials about implementing this in Java so I'm doing it very blind and it has been very difficult so I really appreciate any help I can get.据我所知,几乎没有关于用 Java 实现它的教程,所以我非常盲目地做这件事,而且非常困难,所以我非常感谢我能得到的任何帮助。

The "Task is not yet complete" exception is usually thrown when a task has not yet completed and its result is being accessed. “任务尚未完成”异常通常在任务尚未完成且正在访问其结果时抛出。 This can occur when working with asynchronous tasks, such as those that involve network or I/O operations.在处理异步任务时可能会发生这种情况,例如那些涉及网络或 I/O 操作的任务。

To resolve this exception when implementing Google Cast into a project, you will need to ensure that the task has completed before accessing its result.要在将 Google Cast 实施到项目中时解决此异常,您需要确保任务已完成,然后才能访问其结果。 This can typically be done by using a callback or a listener to get notified when the task has completed.这通常可以通过使用回调或侦听器来完成,以便在任务完成时收到通知。

Here is an example of how you could implement a callback to resolve the "Task is not yet complete" exception:下面是一个示例,说明如何实现回调以解决“任务尚未完成”异常:

private void doSomeTask() {
  // Start the task and pass a callback to be called when the task is complete
  someTask.start(new SomeTask.Callback() {
    @Override
    public void onTaskComplete(Result result) {
      // The task is complete, so we can access the result here
      processResult(result);
    }
  });
}

private void processResult(Result result) {
  // Do something with the result
}

Alternatively, you could use a listener to get notified when the task is complete:或者,您可以使用侦听器在任务完成时收到通知:

private void doSomeTask() {
  // Set a listener to be called when the task is complete
  someTask.setListener(new SomeTask.Listener() {
    @Override
    public void onTaskComplete(Result result) {
      // The task is complete, so we can access the result here
      processResult(result);
    }
  });
  // Start the task
  someTask.start();
}

private void processResult(Result result) {
  // Do something with the result
}

By using a callback or listener, you can ensure that the task has completed before accessing its result, which should help resolve the "Task is not yet complete" exception.通过使用回调或侦听器,您可以确保任务在访问其结果之前已完成,这应该有助于解决“任务尚未完成”异常。

here is an example of how you can use the addOnSuccessListener method to get the shared instance of CastContext:下面是一个示例,说明如何使用 addOnSuccessListener 方法获取 CastContext 的共享实例:

CastContext.getSharedInstance(this, localExecutor)
        .addOnSuccessListener(new OnSuccessListener<CastContext>() {
            @Override
            public void onSuccess(CastContext castContext) {
                // The task was successful, and you can get the shared instance of CastContext here
                mCastContext = castContext;
                // Now you can use mCastContext
            }
        });

Here, this is the ApplicationContext and localExecutor is the Executor that you want to use to execute the tasks associated with CastContext.在这里,这是 ApplicationContext,localExecutor 是您要用来执行与 CastContext 关联的任务的执行器。

To use the await() method, you can do the following:要使用 await() 方法,您可以执行以下操作:

mCastContext = CastContext.getSharedInstance(this, 
localExecutor).await().getResult();

Here, await() blocks the current thread until the task is complete.在这里,await() 阻塞当前线程,直到任务完成。 The getResult() method then returns the result of the task, which is the shared instance of CastContext.然后 getResult() 方法返回任务的结果,它是 CastContext 的共享实例。

I hope this helps!我希望这有帮助!

This doesn't answer your question directly as to why your app behaves differently than the google cast reference app but you can add OnCompleteListener then set the context:这不会直接回答您的问题,即为什么您的应用程序的行为与 google cast 参考应用程序不同,但您可以添加 OnCompleteListener 然后设置上下文:

Kotlin:科特林:

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();
            }
        });

I am stuck at the same point (much of today) as yourself trying to figure out why the CastContext.getSharedInstance(this, castExecutor).result gives the "Task is not yet complete" exception in my own app but not the reference app even though I have followed the same steps as far as I can tell.我被困在同一点(今天的大部分时间),因为你试图弄清楚为什么 CastContext.getSharedInstance(this, castExecutor).result 在我自己的应用程序中给出了“任务尚未完成”异常,但在参考应用程序中甚至没有尽管据我所知,我遵循了相同的步骤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google Fit Android API读取数据时出现“任务尚未完成错误” - Getting “Task is not yet complete error” when reading data using google fit android api 当我将角色与用户关联时,如何解决此异常? - How can I resolve this exception when I associate a Role with a User? 如何解决异常:丢弃连接 - How can I resolve the exception: discard connection 如何等待计划任务的第一次迭代完成? - How can I wait for first iteration of a scheduled task to complete? 如何更轻松地完成此任务? [Java] - How can I complete this task more easily? [Java] 当两个相同的类具有相同的名称、package 和内容时,如何解决 class 转换异常? - How to resolve class cast exception when two same classes having same name, package and content? IllegalStateException:任务尚未完成:将数据从 Firebase 添加到谷歌地图 Android Studio 中的图钉 - IllegalStateException: Task is not yet complete: adding data from Firebase to a pin in google maps Android Studio 如何处理异常并在不提示用户已发生的异常的情况下完成方法的功能 - How to handle exception and yet complete the functionality of the method without prompting the user of the exception that has occured 如何解决 thenReturn() 可能缺少 junit 中的异常 - How can I resolve thenReturn() may be missing exception in junit 如何在Java中解决“数据异常:强制转换的无效字符值”? - How to resolve “data exception: invalid character value for cast” in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM