Code Monkey home page Code Monkey logo

Comments (2)

ryanheise avatar ryanheise commented on May 29, 2024 1

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.

github-actions avatar github-actions commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.