通过 buildAsync() 创建 MediaController 永远不会返回

问题描述 投票:0回答:1

我正在尝试用 Java 创建一个 MediaController 作为 Capacitor 插件的一部分。

RemoteStreamerService
继承了
MediaSessionService
并实现了所需的方法,当这个块运行时我可以确认它正在运行。当我从插件运行以下代码时,监听器永远不会被调用。我在日志中没有看到任何错误,并且
ListenableFuture
只是永远徘徊,永远不会解决。知道可能出了什么问题,甚至如何调试它吗?

    private ListenableFuture<MediaController> controllerFuture;
private void initPlayer() {
    SessionToken sessionToken =
            new SessionToken(getContext(), new ComponentName(getContext(), RemoteStreamerService.class));
    MediaController.Builder builder = new MediaController.Builder(getContext(), sessionToken);
    controllerFuture = builder.buildAsync();
    Log.d("stream", "Awaiting the future player");
    controllerFuture.addListener(() -> {
            Log.d("stream", "Got the player");
            try {
                player = controllerFuture.get();
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
    }, ContextCompat.getMainExecutor(getContext()));
}
android ionic-framework capacitor capacitor-plugin android-media3
1个回答
0
投票

我已经测试了你的代码,但它似乎不正确。通过这些调整,它可以在我的应用程序中运行(注意:必须使用 getApplicationContext()):

MediaController player;
private ListenableFuture<MediaController> controllerFuture;
private void initPlayer() {
    SessionToken sessionToken =
            new SessionToken(getApplicationContext(),
            new ComponentName(getApplicationContext(),
            RemoteStreamerService.class));
   // wrong:
   //MediaController.Builder builder =
   //   new  MediaController.Builder(getContext(), sessionToken);
   //controllerFuture = builder.buildAsync();

    // this ist the correct code for me:
    ListenableFuture<MediaController> controllerFuture =
            new MediaController.Builder(getApplicationContext(),
            sessionToken).buildAsync();
    // wrong: controllerFuture = builder.buildAsync();
    Log.d("stream", "Awaiting the future player");
    controllerFuture.addListener(() -> {
        Log.d("stream", "Got the player");
        try {
            player = controllerFuture.get();
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }, ContextCompat.getMainExecutor(getApplicationContext()));
}
© www.soinside.com 2019 - 2024. All rights reserved.