Comments (2)
Hi @iPatel007
First you should know how to manage a queue/playlist without this plugin, just using a vanilla plugin like audioplayer
or many others. You would need to read the documentation for whatever audio plugin you are using to be able to listen for events such as when one audio file finishes playing and you want to play the next.
Next, if you want this same code to work in the background, you can add this plugin (audio_service
) on top of the code that you already have. This plugin does have a queue API but you can either decide to use it or not. What this plugin's queue API allows you to do is simply to tell the platform what media items are in your queue so that things like Android Auto can show your playlist, but if you decide not to use the queue API, this plugin should still allow your audio code to run in the background while the screen is turned off. So this is completely optional, and it's just if you want to for example show the queue on Android Auto.
Finally, how do you add audio_service
on top of your existing code? Start by reading the example with the associated API documentation to understand the basic code structure. Basically, to have your audio logic run in the background, it needs to run in a separate isolate. So, when your app is running in the foreground, you'll have two isolates: the main isolate for your UI, and the background isolate for driving your audio playback. This way, if the user closes the UI and that isolate is destroyed, your background isolate still continues to run, and that's where your audio playback code will be. Reading the example code with this in mind will help it to make more sense. So whenever the UI starts up, it must connect to the background isolate so that the two isolates can communicate. Whenever you click a button in the UI such as play/pause/next/previous and so on, your UI is going to call the appropriate method on the AudioService
class, and this is going to be passed onto the background isolate through the connection that you have established. You must implement the corresponding callbacks in your background isolate to then handle those requests to play/pause/next/previous. The callback methods in the background isolate typically have the same names just with an "on" prefix. On receiving a request to skip to the next media item, your background isolate should then call whatever audio code that you originally had (see my first paragraph) to play the next media item. The included example shows you how to do this with onPlay and onPause, so you want to just add another callback such as onSkipToNext
and any others that your app needs.
That takes care of communication from the UI to the background isolate. Now you also want to communicate from the background isolate back to the UI, primarily because you want to tell the UI the current state of playback so that the UI can render the current state on the screen. Your background isolate can notify the UI that something has changed via methods in the AudioServiceBackground
class like setState
, setQueue
and setMediaItem
. Going back to the code that you have already written as per my first paragraph, you'll already be listening to your audio plugin for an event that tells you when the current media item has finished and you're ready to play the next media item. At this point, you'll want to insert a call to setMediaItem
to notify the UI isolate that the current media item has changed and allow the UI to display that. The UI can listen to a stream of these current media items and you could use something like a StreamBuilder
to render the current media item in the UI.
from audio_service.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.
from audio_service.
Related Issues (20)
- Is it possible to support custom notification parameters? HOT 3
- Random exception Connection aborted HOT 2
- Android 14/SDK 34 HOT 3
- [just_audio_background] Can't release background audio service at IOS
- Cover art saved as a file does not get displayed on Android Auto HOT 8
- Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
- java.lang.IllegalArgumentException - context shouldn't be null HOT 4
- Adding an option in AudioServiceConfig to switch to lower priority state on AudioProcessingState.completed HOT 3
- `playbackState.add(...)` doesn't update the notification HOT 2
- Shouldn't shuffle the queue when inserting a new queueItem to a specific index HOT 3
- Trouble closing an audio session HOT 3
- Trouble closing audio session HOT 5
- Null pointer exception with audioHandlerInterface inside sendNotificationClicked() HOT 9
- Seekbar is always displayed even without MediaAction.seek HOT 10
- audio_service >=0.17.0-nullsafety.0 depends on js ^0.6.3, please upgrade to 0.7 HOT 4
- Play button on A14 notification not working when targeting API 29 HOT 1
- Deadlock with android_content_provider HOT 2
- Media notification can be swiped away while audio is playing HOT 6
- androidNotificationOngoing setting is ignored HOT 2
- media notification artwork image not work at Android 14 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from audio_service.