media_playback.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/foundation.dart';
  2. /// Read-only snapshot of the active Android media session.
  3. @immutable
  4. class MediaPlayback {
  5. const MediaPlayback({
  6. this.hasAccess = false,
  7. this.hasSession = false,
  8. this.playing = false,
  9. this.title,
  10. this.artist,
  11. this.packageName,
  12. this.durationMs = 0,
  13. this.positionMs = 0,
  14. });
  15. final bool hasAccess;
  16. final bool hasSession;
  17. final bool playing;
  18. final String? title;
  19. final String? artist;
  20. final String? packageName;
  21. final int durationMs;
  22. final int positionMs;
  23. bool get hasTitle => (title?.trim().isNotEmpty ?? false);
  24. String? get displayTitle {
  25. final name = title?.trim();
  26. if (name == null || name.isEmpty) return null;
  27. final by = artist?.trim();
  28. return by == null || by.isEmpty ? name : '$name — $by';
  29. }
  30. double get progress {
  31. if (durationMs <= 0) return 0;
  32. return (positionMs / durationMs).clamp(0.0, 1.0);
  33. }
  34. @override
  35. bool operator ==(Object other) {
  36. return other is MediaPlayback &&
  37. other.hasAccess == hasAccess &&
  38. other.hasSession == hasSession &&
  39. other.playing == playing &&
  40. other.title == title &&
  41. other.artist == artist &&
  42. other.packageName == packageName &&
  43. other.durationMs == durationMs &&
  44. other.positionMs == positionMs;
  45. }
  46. @override
  47. int get hashCode => Object.hash(
  48. hasAccess,
  49. hasSession,
  50. playing,
  51. title,
  52. artist,
  53. packageName,
  54. durationMs,
  55. positionMs,
  56. );
  57. factory MediaPlayback.fromMap(Map<dynamic, dynamic> map) {
  58. int integer(String key) => (map[key] as num?)?.toInt() ?? 0;
  59. String? string(String key) {
  60. final value = (map[key] as String?)?.trim();
  61. return value == null || value.isEmpty ? null : value;
  62. }
  63. return MediaPlayback(
  64. hasAccess: map['hasAccess'] == true,
  65. hasSession: map['hasSession'] == true,
  66. playing: map['playing'] == true,
  67. title: string('title'),
  68. artist: string('artist'),
  69. packageName: string('packageName'),
  70. durationMs: integer('durationMs'),
  71. positionMs: integer('positionMs'),
  72. );
  73. }
  74. }