Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The AmbientClient must be properly closed when it is no longer needed. Closing the client releases internal resources, shuts down the audio processing pipeline, and allows a new instance to be created if needed.
Manual cleanup
If your app is managing the client lifetime directly, call the close() method when you've finished with the client:
val ambientClient = AmbientClient(
context = applicationContext,
provider = provider,
accessTokenProvider = accessTokenProvider,
listener = listener
)
// Use the client...
// Close when done
ambientClient.close()
Automatic cleanup with lifecycle components
For lifecycle-aware components such as ViewModel, use addCloseable() to automatically close the client when the component is cleared:
class MyViewModel(application: Application) : AndroidViewModel(application) {
private val ambientClient: AmbientClient
init {
ambientClient = AmbientClient(
context = application.applicationContext,
provider = createProvider(),
accessTokenProvider = createAccessTokenProvider(),
listener = createListener()
)
// Register for automatic cleanup
addCloseable(ambientClient)
}
// ambientClient.close() will be called automatically when ViewModel is cleared
}
The AmbientClient can only be instantiated once per app run. After closing the client, you can create a new instance if needed.