audio player flutter package that provides an easy widgets to have audio player that can play remote or local audio files across all platforms
dependencies:
flutter:
sdk: flutter
# add this line 👇
easy_audio_player: {latest_version}
after the 'manifest' tag and before the 'application' tag add these permissions .
<!-- Just audio background PERMISSIONS -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
then change the 'activity' tag
change this:
android:name=".MainActivity"
to this
android:name="com.ryanheise.audioservice.AudioServiceActivity"
then after 'activity' tag and before the end of 'application tag ' add this :
<!-- ADD THIS "SERVICE" element -->
<service android:name="com.ryanheise.audioservice.AudioService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<!-- ADD THIS "RECEIVER" element -->
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Insert this in your Info.plist file:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
- call
initJustAudioBackground
as early as possible
void main() async {
// init the background service to display notifications while playing
await initJustAudioBackground(NotificationSettings(androidNotificationChannelId: 'com.example.example'));
runApp(MaterialApp(
home: HomeScreen(),
));
}
- load your audios in a
ConcatenatingAudioSource
playlist containing anAudioSource
or one of it's inherted classes .
final ConcatenatingAudioSource _playlist = ConcatenatingAudioSource(children: [
AudioSource.uri(Uri.parse('https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3'),
tag: MediaItem(
id: '1',
artUri: Uri.parse('https://picsum.photos/300/300'),
title: 'Audio Title ',
album: 'amazing album'))
]);
- call one of the players widgets with your playlist and options
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: FullAudioPlayer(autoPlay: false, playlist: _playlist),
))));