Flutter Audiobook Player in 50 lines of code
Tags: flutter, dart, android, ios
Published: 2020-06-27 17:40:51


## Intro

How much can you do with 60 lines of code? Depends how good you are, also more importantly depends how good your language/framework is. While this won't be a tutorial on making super complicated quines of try to code golf until to the limit, I still found it worth sharing how easy and quick you can set up an audiobook player that plays books from librivox that can work on ios, android and web (copy and paste code at the bottom).

## Tutorial


#### Installing Packages

For this tutorial I will be installing the package [assets_audio_player](https://pub.dev/packages/assets_audio_player) for playing the audio. (In my case I will be also creating it as a web app so I will aslo install [assets_audio_player_web](https://pub.dev/packages/assets_audio_player_web)).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uw3fvcd2tkai2qz65s4l.png)


#### Show me the code

In main.dart delete everything except for the main funtion, then create a statefull widget MyApp.

in MyAppState we add the variables for keeping track of time and if the audio is paused or playing. I also added the url that I will be opening here.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1bo4knb7u4y3gtg41vtl.png)

In the build method we want to first make a material app, (I dont like the debug banner so I set debugShowCheckedModeBanner: false)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lhrjsdzwy93eku0hex3r.png)

This MaterialApp's home will be a Scaffold with a Center widget. The center widget will have a Colum so that we can add our video's playback time and the play/pause button. This should be placed in the center of the screen so we set CrossAxisAllignment and MainAxisAllignment to center aswell.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vvxuiyq97gju8s8bs3nb.png)

Now to build the audiobook player part. First, onto the simple stuff, we will create a Text widget that displays the time:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yzuxg1nqaa4vpjz11mc6.png)

Next we add the AudioWidget (you willl need to import assets_audio_player). The paramenters we have to set are: 

- 'audio:' This takes in the audio's input source (can be from network or from asset)
- 'play:' bool that stores the state
- 'child': this where you would add your button or more elaboorate widget
- 'onPositionChanged:' listens for changes in the audio position (c is current time and d is duration)
- 'onReadyToPlay': listens for when the audio is ready to be played

For this tutorial my setup ended up being:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d6q9wl7tftbl872zqsy1.png)

This came out to 52 lines and if we run it we get:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dd8al5jt73391j08xxxz.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dd8al5jt73391j08xxxz.png)

### Conclusion

I hope you have enjoyed reading along and that you can make something much cooler than I did.

As promised here is the copy and paste code:

([source code](https://github.com/Mattds825/flutter_audiobook_demo))

{% gist https://gist.github.com/Mattds825/ed2e8a819290717a24c6c0bca22fe117 %}