Skip to content

Commit 624055a

Browse files
committed
Refactor transcoding service integration in audio playback
1 parent 2073d4c commit 624055a

3 files changed

Lines changed: 34 additions & 11 deletions

File tree

lib/main.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,18 @@ void main() async {
218218
// is ready and fully decoupled from the Flutter widget lifecycle on iOS.
219219
final audioHandler = await initAudioService();
220220

221+
// Create TranscodingService instance to share across providers
222+
final transcodingService = TranscodingService();
223+
221224
final Widget appWithProviders = MultiProvider(
222225
providers: [
223226
Provider<StorageService>.value(value: storageService),
224227
Provider<SubsonicService>.value(value: subsonicService),
225228
ChangeNotifierProvider<RecommendationService>.value(
226229
value: recommendationService,
227230
),
228-
ChangeNotifierProvider<TranscodingService>(
229-
create: (_) => TranscodingService(),
231+
ChangeNotifierProvider<TranscodingService>.value(
232+
value: transcodingService,
230233
),
231234
ChangeNotifierProvider<LocalMusicService>.value(value: localMusicService),
232235
ChangeNotifierProvider(
@@ -248,6 +251,7 @@ void main() async {
248251
upnpService,
249252
audioHandler,
250253
jukeboxService,
254+
transcodingService,
251255
),
252256
),
253257
ChangeNotifierProvider(create: (_) => LibraryProvider(subsonicService)),

lib/providers/player_provider.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import '../services/jukebox_service.dart';
2929
import '../services/audio_handler.dart';
3030
import '../services/fade_settings_service.dart';
3131
import '../services/lock_screen_lyrics_service.dart';
32+
import '../services/transcoding_service.dart';
3233
import '../providers/library_provider.dart';
3334

3435
enum RepeatMode { off, all, one }
@@ -103,6 +104,7 @@ class PlayerProvider extends ChangeNotifier with WidgetsBindingObserver {
103104
bool _isFading = false;
104105

105106
final JukeboxService _jukeboxService;
107+
final TranscodingService _transcodingService;
106108

107109
double _playbackSpeed = 1.0;
108110
double _pitch = 1.0;
@@ -115,6 +117,7 @@ class PlayerProvider extends ChangeNotifier with WidgetsBindingObserver {
115117
this._upnpService,
116118
this._audioHandler,
117119
this._jukeboxService,
120+
this._transcodingService,
118121
) {
119122
_storageService = storageService;
120123
_discordRpcService = DiscordRpcService(storageService);
@@ -1593,7 +1596,15 @@ class PlayerProvider extends ChangeNotifier with WidgetsBindingObserver {
15931596
if (offlinePath != null) {
15941597
playUrl = 'file://$offlinePath';
15951598
} else {
1596-
playUrl = await _subsonicService.resolveStreamUrlAsync(song);
1599+
// Apply transcoding settings if enabled
1600+
final maxBitRate = _transcodingService.enabled
1601+
? _transcodingService.currentBitRate
1602+
: null;
1603+
final format = _transcodingService.enabled
1604+
? _transcodingService.format
1605+
: null;
1606+
playUrl = _subsonicService.getStreamUrl(song.id,
1607+
maxBitRate: maxBitRate, format: format);
15971608
}
15981609
}
15991610
// Cache remote streams locally so seeking works even when the
@@ -2388,7 +2399,13 @@ class PlayerProvider extends ChangeNotifier with WidgetsBindingObserver {
23882399
if (offlinePath != null) {
23892400
return AudioSource.uri(Uri.file(offlinePath));
23902401
}
2391-
final url = _subsonicService.getStreamUrl(song.id);
2402+
// Apply transcoding settings if enabled
2403+
final maxBitRate =
2404+
_transcodingService.enabled ? _transcodingService.currentBitRate : null;
2405+
final format =
2406+
_transcodingService.enabled ? _transcodingService.format : null;
2407+
final url = _subsonicService.getStreamUrl(song.id,
2408+
maxBitRate: maxBitRate, format: format);
23922409
// Cache remote streams locally so seeking works even when the server
23932410
// transcodes and doesn't support HTTP range requests (issue #170).
23942411
final cacheDir = await getTemporaryDirectory();

lib/services/transcoding_service.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:shared_preferences/shared_preferences.dart';
55

66
class TranscodeBitrate {
7-
static const int original = 0;
7+
static const int original = 0;
88
static const int kbps64 = 64;
99
static const int kbps128 = 128;
1010
static const int kbps192 = 192;
@@ -27,7 +27,7 @@ class TranscodeBitrate {
2727
}
2828

2929
class TranscodeFormat {
30-
static const String original = 'raw';
30+
static const String original = 'raw';
3131
static const String mp3 = 'mp3';
3232
static const String opus = 'opus';
3333
static const String aac = 'aac';
@@ -71,6 +71,11 @@ class TranscodingService extends ChangeNotifier {
7171

7272
int get wifiBitrate => _wifiBitrate;
7373
int get mobileBitrate => _mobileBitrate;
74+
int get currentBitRate => _smartEnabled
75+
? (_currentConnectionType == ConnectionType.wifi
76+
? _wifiBitrate
77+
: _mobileBitrate)
78+
: _wifiBitrate;
7479
String get format => _format;
7580
bool get enabled => _enabled;
7681
bool get smartEnabled => _smartEnabled;
@@ -99,18 +104,15 @@ class TranscodingService extends ChangeNotifier {
99104
}
100105

101106
Future<void> _initConnectivityWatcher() async {
102-
103107
final result = await Connectivity().checkConnectivity();
104108
_updateConnectionType(result);
105109

106110
_connectivitySub?.cancel();
107-
_connectivitySub = Connectivity()
108-
.onConnectivityChanged
109-
.listen(_updateConnectionType);
111+
_connectivitySub =
112+
Connectivity().onConnectivityChanged.listen(_updateConnectionType);
110113
}
111114

112115
void _updateConnectionType(List<ConnectivityResult> results) {
113-
114116
final newType = results.contains(ConnectivityResult.wifi)
115117
? ConnectionType.wifi
116118
: ConnectionType.mobile;

0 commit comments

Comments
 (0)