|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:smtc_windows/smtc_windows.dart'; |
| 4 | +import 'package:watch_it/watch_it.dart'; |
| 5 | + |
| 6 | +import '../app_config.dart'; |
| 7 | +import '../common/data/audio_type.dart'; |
| 8 | +import 'player_service.dart'; |
| 9 | + |
| 10 | +StreamSubscription<PressedButton>? smtcSubscription; |
| 11 | + |
| 12 | +Future<SMTCWindows> registerSMTCWindows() async { |
| 13 | + final playerService = di<PlayerService>(); |
| 14 | + |
| 15 | + await SMTCWindows.initialize(); |
| 16 | + final smtc = SMTCWindows( |
| 17 | + enabled: true, |
| 18 | + config: const SMTCConfig( |
| 19 | + fastForwardEnabled: false, |
| 20 | + nextEnabled: true, |
| 21 | + pauseEnabled: true, |
| 22 | + playEnabled: true, |
| 23 | + rewindEnabled: false, |
| 24 | + prevEnabled: true, |
| 25 | + stopEnabled: false, |
| 26 | + ), |
| 27 | + ); |
| 28 | + |
| 29 | + smtcSubscription = smtc.buttonPressStream.listen((event) { |
| 30 | + switch (event) { |
| 31 | + case PressedButton.play: |
| 32 | + playerService.playOrPause().then( |
| 33 | + (_) => smtc.setPlaybackStatus( |
| 34 | + playerService.isPlaying |
| 35 | + ? PlaybackStatus.playing |
| 36 | + : PlaybackStatus.paused, |
| 37 | + ), |
| 38 | + ); |
| 39 | + case PressedButton.pause: |
| 40 | + playerService.playOrPause().then( |
| 41 | + (_) => smtc.setPlaybackStatus( |
| 42 | + playerService.isPlaying |
| 43 | + ? PlaybackStatus.playing |
| 44 | + : PlaybackStatus.paused, |
| 45 | + ), |
| 46 | + ); |
| 47 | + case PressedButton.next: |
| 48 | + playerService.playNext(); |
| 49 | + case PressedButton.previous: |
| 50 | + playerService.playPrevious(); |
| 51 | + default: |
| 52 | + break; |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + playerService.registerMediaControlsCallBacks( |
| 57 | + onSetMetaData: ({required artUri, required audio}) => smtc.updateMetadata( |
| 58 | + MusicMetadata( |
| 59 | + title: audio.title ?? AppConfig.appTitle, |
| 60 | + album: audio.album, |
| 61 | + albumArtist: audio.artist, |
| 62 | + artist: audio.artist ?? '', |
| 63 | + thumbnail: audio.audioType == AudioType.local |
| 64 | + ? AppConfig.fallbackThumbnailUrl |
| 65 | + : artUri == null |
| 66 | + ? null |
| 67 | + : '$artUri', |
| 68 | + ), |
| 69 | + ), |
| 70 | + onIsPlaying: |
| 71 | + ({required audioType, required isPlaying, required queueNotEmpty}) => |
| 72 | + smtc.setPlaybackStatus( |
| 73 | + isPlaying ? PlaybackStatus.playing : PlaybackStatus.paused, |
| 74 | + ), |
| 75 | + onSetPosition: (position) => smtc.setPosition(position ?? Duration.zero), |
| 76 | + onSetDuration: null, |
| 77 | + ); |
| 78 | + |
| 79 | + return smtc; |
| 80 | +} |
0 commit comments