spotube/lib/services/audio_player/custom_player.dart
Alessio fece073def This pull request primarily involves the removal of several configuration files and assets, as well as minor updates to documentation. The most significant changes are the deletion of various .vscode configuration files and the removal of unused assets from the project.
Configuration File Removals:

    .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties.
    .vscode/launch.json: Removed the Dart launch configurations for different environments and modes.
    .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path.
    .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates.
    .vscode/tasks.json: Removed the tasks configuration file.

Documentation Updates:

    CONTRIBUTION.md: Removed heart emoji from the introductory text.
    README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5]

Asset Removals:

    lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3]

Minor Code Cleanups:

    cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4]
    integration_test/app_test.dart: Removed an unnecessary blank line.
    lib/collections/routes.dart: Commented out the TrackRoute configuration.
2025-04-13 18:40:37 +02:00

149 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:audio_session/audio_session.dart';
import 'package:flutter_broadcasts/flutter_broadcasts.dart';
import 'package:media_kit/media_kit.dart';
import 'package:package_info_plus/package_info_plus.dart';
// ignore: implementation_imports
import 'package:spotube/services/audio_player/playback_state.dart';
import 'package:spotube/services/logger/logger.dart';
import 'package:spotube/utils/platform.dart';
/// MediaKit [Player] by default doesn't have a state stream.
/// This class adds a state stream to the [Player] class.
class CustomPlayer extends Player {
final StreamController<AudioPlaybackState> _playerStateStream;
final StreamController<bool> _shuffleStream;
late final List<StreamSubscription> _subscriptions;
bool _shuffled;
int _androidAudioSessionId = 0;
String _packageName = "";
AndroidAudioManager? _androidAudioManager;
CustomPlayer({super.configuration})
: _playerStateStream = StreamController.broadcast(),
_shuffleStream = StreamController.broadcast(),
_shuffled = false {
nativePlayer.setProperty("network-timeout", "120");
_subscriptions = [
stream.buffering.listen((event) {
_playerStateStream.add(AudioPlaybackState.buffering);
}),
stream.playing.listen((playing) {
if (playing) {
_playerStateStream.add(AudioPlaybackState.playing);
} else {
_playerStateStream.add(AudioPlaybackState.paused);
}
}),
stream.completed.listen((isCompleted) async {
if (!isCompleted) return;
_playerStateStream.add(AudioPlaybackState.completed);
}),
stream.playlist.listen((event) {
if (event.medias.isEmpty) {
_playerStateStream.add(AudioPlaybackState.stopped);
}
}),
stream.error.listen((event) {
AppLogger.reportError('[MediaKitError] \n$event', StackTrace.current);
}),
];
PackageInfo.fromPlatform().then((packageInfo) {
_packageName = packageInfo.packageName;
});
if (kIsAndroid) {
_androidAudioManager = AndroidAudioManager();
AudioSession.instance.then((s) async {
_androidAudioSessionId =
await _androidAudioManager!.generateAudioSessionId();
notifyAudioSessionUpdate(true);
await nativePlayer.setProperty(
"audiotrack-session-id",
_androidAudioSessionId.toString(),
);
await nativePlayer.setProperty("ao", "audiotrack,opensles,");
});
}
}
Future<void> notifyAudioSessionUpdate(bool active) async {
if (kIsAndroid) {
sendBroadcast(
BroadcastMessage(
name: active
? "android.media.action.OPEN_AUDIO_EFFECT_CONTROL_SESSION"
: "android.media.action.CLOSE_AUDIO_EFFECT_CONTROL_SESSION",
data: {
"android.media.extra.AUDIO_SESSION": _androidAudioSessionId,
"android.media.extra.PACKAGE_NAME": _packageName
},
),
);
}
}
bool get shuffled => _shuffled;
Stream<AudioPlaybackState> get playerStateStream => _playerStateStream.stream;
Stream<bool> get shuffleStream => _shuffleStream.stream;
Stream<int> get indexChangeStream {
int oldIndex = state.playlist.index;
return stream.playlist.map((event) => event.index).where((newIndex) {
if (newIndex != oldIndex) {
oldIndex = newIndex;
return true;
}
return false;
});
}
@override
Future<void> setShuffle(bool shuffle) async {
_shuffled = shuffle;
await super.setShuffle(shuffle);
_shuffleStream.add(shuffle);
await Future.delayed(const Duration(milliseconds: 100));
if (shuffle) {
await move(state.playlist.index, 0);
}
}
@override
Future<void> stop() async {
await super.stop();
_shuffled = false;
_playerStateStream.add(AudioPlaybackState.stopped);
_shuffleStream.add(false);
}
@override
Future<void> dispose() async {
for (var element in _subscriptions) {
element.cancel();
}
await notifyAudioSessionUpdate(false);
return super.dispose();
}
NativePlayer get nativePlayer => platform as NativePlayer;
Future<void> insert(int index, Media media) async {
await add(media);
await move(state.playlist.medias.length, index);
}
Future<void> setAudioNormalization(bool normalize) async {
if (normalize) {
await nativePlayer.setProperty('af', 'dynaudnorm=g=5:f=250:r=0.9:p=0.5');
} else {
await nativePlayer.setProperty('af', '');
}
}
}