|
| 1 | +import 'dart:developer' as dev; |
| 2 | + |
| 3 | +import 'package:audio_session/audio_session.dart'; |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_soloud/flutter_soloud.dart'; |
| 7 | +import 'package:logging/logging.dart'; |
| 8 | + |
| 9 | +void main() async { |
| 10 | + // The `flutter_soloud` package logs everything |
| 11 | + // (from severe warnings to fine debug messages) |
| 12 | + // using the standard `package:logging`. |
| 13 | + // You can listen to the logs as shown below. |
| 14 | + Logger.root.level = kDebugMode ? Level.FINE : Level.INFO; |
| 15 | + Logger.root.onRecord.listen((record) { |
| 16 | + dev.log( |
| 17 | + record.message, |
| 18 | + time: record.time, |
| 19 | + level: record.level.value, |
| 20 | + name: record.loggerName, |
| 21 | + zone: record.zone, |
| 22 | + error: record.error, |
| 23 | + stackTrace: record.stackTrace, |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + WidgetsFlutterBinding.ensureInitialized(); |
| 28 | + |
| 29 | + /// Initialize the player. |
| 30 | + await SoLoud.instance.init(); |
| 31 | + |
| 32 | + runApp( |
| 33 | + const MaterialApp( |
| 34 | + home: AudioContext(), |
| 35 | + ), |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +enum ContextState { |
| 40 | + playing, |
| 41 | + paused, |
| 42 | + stopped, |
| 43 | + ducking, |
| 44 | + unknown, |
| 45 | +} |
| 46 | + |
| 47 | +/// Simple usecase of flutter_soloud plugin |
| 48 | +class AudioContext extends StatefulWidget { |
| 49 | + const AudioContext({super.key}); |
| 50 | + |
| 51 | + @override |
| 52 | + State<AudioContext> createState() => _AudioContextState(); |
| 53 | +} |
| 54 | + |
| 55 | +class _AudioContextState extends State<AudioContext> { |
| 56 | + final soloud = SoLoud.instance; |
| 57 | + late final AudioSession session; |
| 58 | + AudioSource? sound; |
| 59 | + SoundHandle? soundHandle; |
| 60 | + ValueNotifier<ContextState> isPlaying = ValueNotifier(ContextState.stopped); |
| 61 | + |
| 62 | + @override |
| 63 | + void initState() { |
| 64 | + super.initState(); |
| 65 | + // Initialize the audio session. |
| 66 | + AudioSession.instance.then((audioSession) async { |
| 67 | + session = audioSession; |
| 68 | + await session.configure( |
| 69 | + const AudioSessionConfiguration( |
| 70 | + androidWillPauseWhenDucked: true, |
| 71 | + androidAudioAttributes: AndroidAudioAttributes( |
| 72 | + usage: AndroidAudioUsage.media, |
| 73 | + contentType: AndroidAudioContentType.music, |
| 74 | + ), |
| 75 | + androidAudioFocusGainType: |
| 76 | + AndroidAudioFocusGainType.gainTransientMayDuck, |
| 77 | + avAudioSessionCategory: AVAudioSessionCategory.playback, |
| 78 | + avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.none, |
| 79 | + ), |
| 80 | + ); |
| 81 | + |
| 82 | + // Listen to audio interruptions and pause or duck as appropriate. |
| 83 | + _handleInterruptions(session); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @override |
| 88 | + void dispose() { |
| 89 | + SoLoud.instance.deinit(); |
| 90 | + super.dispose(); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Widget build(BuildContext context) { |
| 95 | + return Scaffold( |
| 96 | + body: FutureBuilder<AudioSession>( |
| 97 | + future: AudioSession.instance, |
| 98 | + builder: (context, asyncSnapshot) { |
| 99 | + final session = asyncSnapshot.data; |
| 100 | + if (session == null) return const CircularProgressIndicator(); |
| 101 | + return Center( |
| 102 | + child: Column( |
| 103 | + mainAxisSize: MainAxisSize.min, |
| 104 | + spacing: 12, |
| 105 | + children: [ |
| 106 | + ElevatedButton( |
| 107 | + onPressed: () async { |
| 108 | + await SoLoud.instance.disposeAllSources(); |
| 109 | + |
| 110 | + sound = await soloud |
| 111 | + .loadAsset('assets/audio/8_bit_mentality.mp3'); |
| 112 | + soundHandle = await soloud.play(sound!, looping: true); |
| 113 | + |
| 114 | + await session.setActive(true); |
| 115 | + isPlaying.value = ContextState.playing; |
| 116 | + }, |
| 117 | + child: const Text('play asset'), |
| 118 | + ), |
| 119 | + |
| 120 | + ElevatedButton( |
| 121 | + onPressed: () async { |
| 122 | + await session.setActive(true); |
| 123 | + soloud |
| 124 | + ..setPause(soundHandle!, false) |
| 125 | + ..fadeGlobalVolume(1, const Duration(milliseconds: 300)); |
| 126 | + isPlaying.value = ContextState.playing; |
| 127 | + }, |
| 128 | + child: const Text('unpause'), |
| 129 | + ), |
| 130 | + |
| 131 | + // Display the current state. |
| 132 | + ValueListenableBuilder<ContextState>( |
| 133 | + valueListenable: isPlaying, |
| 134 | + builder: (context, state, child) { |
| 135 | + return Text( |
| 136 | + 'Current state: ${state.name}', |
| 137 | + style: const TextStyle(fontSize: 20), |
| 138 | + ); |
| 139 | + }, |
| 140 | + ), |
| 141 | + ], |
| 142 | + ), |
| 143 | + ); |
| 144 | + }, |
| 145 | + ), |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + void fadeoutThenPause() { |
| 150 | + if (soundHandle == null) return; |
| 151 | + soloud.fadeGlobalVolume(0, const Duration(milliseconds: 300)); |
| 152 | + Future.delayed(const Duration(milliseconds: 300), () { |
| 153 | + // After fading out, we can pause. |
| 154 | + soloud.setPause(soundHandle!, true); |
| 155 | + isPlaying.value = ContextState.paused; |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + void fadeinThenResume() { |
| 160 | + if (soundHandle == null) return; |
| 161 | + isPlaying.value = ContextState.playing; |
| 162 | + soloud |
| 163 | + ..setPause(soundHandle!, false) |
| 164 | + ..fadeGlobalVolume(1, const Duration(milliseconds: 300)); |
| 165 | + } |
| 166 | + |
| 167 | + void _handleInterruptions(AudioSession audioSession) { |
| 168 | + audioSession.becomingNoisyEventStream.listen((_) { |
| 169 | + // The user unplugged the headphones, so we should pause |
| 170 | + // or lower the volume. |
| 171 | + debugPrint('audio_context: becomingNoisy, pausing...'); |
| 172 | + if (soundHandle == null) return; |
| 173 | + soloud.setPause(soundHandle!, true); |
| 174 | + isPlaying.value = ContextState.paused; |
| 175 | + }); |
| 176 | + audioSession.interruptionEventStream.listen((event) { |
| 177 | + debugPrint('audio_context: interruption begin: ${event.begin}'); |
| 178 | + debugPrint('audio_context: interruption type: ${event.type}'); |
| 179 | + if (soundHandle == null) return; |
| 180 | + if (event.begin) { |
| 181 | + switch (event.type) { |
| 182 | + case AudioInterruptionType.duck: |
| 183 | + // Another app started playing audio and we should duck. |
| 184 | + soloud.fadeGlobalVolume(0.1, const Duration(milliseconds: 300)); |
| 185 | + isPlaying.value = ContextState.ducking; |
| 186 | + case AudioInterruptionType.pause: |
| 187 | + // Another app started playing audio and we should pause. |
| 188 | + fadeoutThenPause(); |
| 189 | + isPlaying.value = ContextState.paused; |
| 190 | + case AudioInterruptionType.unknown: |
| 191 | + // Another app started playing audio and we should pause. |
| 192 | + soloud.setPause(soundHandle!, true); |
| 193 | + isPlaying.value = ContextState.unknown; |
| 194 | + } |
| 195 | + } else { |
| 196 | + switch (event.type) { |
| 197 | + case AudioInterruptionType.duck: |
| 198 | + // The interruption ended and we should unduck. |
| 199 | + soloud.fadeGlobalVolume(1, const Duration(milliseconds: 300)); |
| 200 | + isPlaying.value = ContextState.playing; |
| 201 | + case AudioInterruptionType.pause: |
| 202 | + // The interruption ended and we should resume. |
| 203 | + fadeinThenResume(); |
| 204 | + isPlaying.value = ContextState.playing; |
| 205 | + case AudioInterruptionType.unknown: |
| 206 | + // The interruption ended but we should not resume. |
| 207 | + isPlaying.value = ContextState.unknown; |
| 208 | + } |
| 209 | + } |
| 210 | + }); |
| 211 | + audioSession.devicesChangedEventStream.listen((event) { |
| 212 | + debugPrint('audio_context: Devices added: ${event.devicesAdded}'); |
| 213 | + debugPrint('audio_context: Devices removed: ${event.devicesRemoved}'); |
| 214 | + }); |
| 215 | + } |
| 216 | +} |
0 commit comments