Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

media3 create MediaController no result #1524

Closed
aacai opened this issue Jul 3, 2024 · 3 comments
Closed

media3 create MediaController no result #1524

aacai opened this issue Jul 3, 2024 · 3 comments
Assignees
Labels

Comments

@aacai
Copy link

aacai commented Jul 3, 2024

https://developer.android.google.cn/media/media3/session/connect-to-media-app?hl=zh-cn#create-controller
I created the MediaController exactly as per the tutorial on the official website, but it just doesn't call back the code block in my listener。
image
image
It was available at first, but now it does not enter the callback anyway

media3 version is 1.3.1, android 14

@aacai
Copy link
Author

aacai commented Jul 3, 2024

PlayVideo.zip
this is my sample

@debz-eight
Copy link

Try this:

Create two variables and initialize as I did below:

private var controller: MediaController? = null
private var mediaControllerFuture: ListenableFuture<MediaController>? = null

@UnstableApi
  private fun initializeMediaController() {
        val sessionToken =
            SessionToken(this, ComponentName(this, SongPlayerService::class.java))
        mediaControllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
        mediaControllerFuture?.apply {
            addListener(Runnable {
                controller = get()
            }, MoreExecutors.directExecutor())
        }
    }

As for initializeMediaController(), call it inside onCreate of Acitivty or onViewCreated of fragment.

@marcbaechinger
Copy link
Contributor

See comment from @debz-eight as an exaample.

As per JavaDoc of MediaControlle.Builder.buildAsync(), you should keep a reference of the Future until the listener is executed and you can call get() on the future. The future may be garbage collected if you don't do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants