Last active
May 17, 2022 17:44
-
-
Save MarkOSullivan94/616d2a3bbd4a8d9c9d79771808fdc30c to your computer and use it in GitHub Desktop.
Playing background music in Flutter with assets_audio_player 1.0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:assets_audio_player/assets_audio_player.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
AssetsAudioPlayer _assetsAudioPlayer; | |
@override | |
void initState() { | |
super.initState(); | |
_assetsAudioPlayer = AssetsAudioPlayer(); | |
_assetsAudioPlayer.open( | |
AssetsAudio( | |
asset: "song3.mp3", | |
folder: "assets/music/", | |
), | |
); | |
_assetsAudioPlayer.playOrPause(); | |
} | |
@override | |
void dispose() { | |
_assetsAudioPlayer = null; | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('Asset Audio Example'), | |
), | |
body: Container(), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment