Skip to content

Commit

Permalink
feat(playback): use assets_audio_player to fix macos double duration …
Browse files Browse the repository at this point in the history
…problems and android high loading latency
  • Loading branch information
Kingkor Roy Tirtho committed May 4, 2023
1 parent be91e33 commit 1fff0f1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/collections/intents.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PlayPauseAction extends Action<PlayPauseIntent> {
if (playlist == null) {
return null;
} else if (!audioPlayer.isPlaying) {
if (audioPlayer.hasSource && !audioPlayer.isCompleted) {
if (audioPlayer.hasSource && !await audioPlayer.isCompleted) {
await playlistNotifier.resume();
} else {
await playlistNotifier.play();
Expand Down
109 changes: 86 additions & 23 deletions lib/services/audio_player.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';

import 'package:audioplayers/audioplayers.dart' as ap;
import 'package:assets_audio_player/assets_audio_player.dart' as aap;
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';

final audioPlayer = SpotubeAudioPlayer();

Expand All @@ -24,6 +26,17 @@ enum PlayerState {
}
}

static PlayerState fromAapPlayerState(aap.PlayerState state) {
switch (state) {
case aap.PlayerState.play:
return PlayerState.playing;
case aap.PlayerState.pause:
return PlayerState.paused;
case aap.PlayerState.stop:
return PlayerState.stopped;
}
}

ap.PlayerState get asAudioPlayerPlayerState {
switch (this) {
case PlayerState.playing:
Expand All @@ -42,27 +55,34 @@ enum PlayerState {

class SpotubeAudioPlayer {
final ap.AudioPlayer? _audioPlayer;
final aap.AssetsAudioPlayer? _assetsAudioPlayer;

SpotubeAudioPlayer()
: _audioPlayer = apSupportedPlatform ? ap.AudioPlayer() : null;
: _audioPlayer = apSupportedPlatform ? ap.AudioPlayer() : null,
_assetsAudioPlayer =
!apSupportedPlatform ? aap.AssetsAudioPlayer.newPlayer() : null;

/// Whether the current platform supports the audioplayers plugin
static const bool apSupportedPlatform = true;
static final bool apSupportedPlatform =
DesktopTools.platform.isWindows || DesktopTools.platform.isLinux;

// stream getters
Stream<Duration> get durationStream {
if (apSupportedPlatform) {
return _audioPlayer!.onDurationChanged.asBroadcastStream();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.onReadyToPlay
.where((event) => event != null)
.map((event) => event!.duration)
.asBroadcastStream();
}
}

Stream<Duration> get positionStream {
if (apSupportedPlatform) {
return _audioPlayer!.onPositionChanged.asBroadcastStream();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.currentPosition.asBroadcastStream();
}
}

Expand All @@ -71,15 +91,28 @@ class SpotubeAudioPlayer {
// audioplayers doesn't have the capability to get buffered position
return const Stream<Duration>.empty().asBroadcastStream();
} else {
throw UnimplementedError();
return const Stream<Duration>.empty().asBroadcastStream();
}
}

Stream<void> get completedStream {
if (apSupportedPlatform) {
return _audioPlayer!.onPlayerComplete.asBroadcastStream();
} else {
throw UnimplementedError();
int lastValue = 0;
return positionStream.where(
(pos) {
final posS = pos.inSeconds;
final duration = _assetsAudioPlayer
?.current.valueOrNull?.audio.duration.inSeconds ??
0;
final isComplete =
posS > 0 && duration > 0 && posS == duration && posS != lastValue;

if (isComplete) lastValue = posS;
return isComplete;
},
).asBroadcastStream();
}
}

Expand All @@ -89,38 +122,45 @@ class SpotubeAudioPlayer {
return state == ap.PlayerState.playing;
}).asBroadcastStream();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.isPlaying.asBroadcastStream();
}
}

Stream<bool> get bufferingStream {
if (apSupportedPlatform) {
return Stream.value(false).asBroadcastStream();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.isBuffering.asBroadcastStream();
}
}

Stream<PlayerState> get playerStateStream =>
_audioPlayer!.onPlayerStateChanged
Stream<PlayerState> get playerStateStream {
if (apSupportedPlatform) {
return _audioPlayer!.onPlayerStateChanged
.map((state) => PlayerState.fromApPlayerState(state))
.asBroadcastStream();
} else {
return _assetsAudioPlayer!.playerState
.map(PlayerState.fromAapPlayerState)
.asBroadcastStream();
}
}

// regular info getter

Future<Duration?> get duration async {
if (apSupportedPlatform) {
return await _audioPlayer!.getDuration();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.current.valueOrNull?.audio.duration;
}
}

Future<Duration?> get position async {
if (apSupportedPlatform) {
return await _audioPlayer!.getCurrentPosition();
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.currentPosition.valueOrNull;
}
}

Expand All @@ -129,15 +169,15 @@ class SpotubeAudioPlayer {
// audioplayers doesn't have the capability to get buffered position
return null;
} else {
throw UnimplementedError();
return null;
}
}

bool get hasSource {
if (apSupportedPlatform) {
return _audioPlayer!.source != null;
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.current.valueOrNull != null;
}
}

Expand All @@ -146,31 +186,31 @@ class SpotubeAudioPlayer {
if (apSupportedPlatform) {
return _audioPlayer!.state == ap.PlayerState.playing;
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.isPlaying.valueOrNull ?? false;
}
}

bool get isPaused {
if (apSupportedPlatform) {
return _audioPlayer!.state == ap.PlayerState.paused;
} else {
throw UnimplementedError();
return !isPlaying && hasSource;
}
}

bool get isStopped {
if (apSupportedPlatform) {
return _audioPlayer!.state == ap.PlayerState.stopped;
} else {
throw UnimplementedError();
return !isPlaying && !hasSource;
}
}

bool get isCompleted {
Future<bool> get isCompleted async {
if (apSupportedPlatform) {
return _audioPlayer!.state == ap.PlayerState.completed;
} else {
throw UnimplementedError();
return !isPlaying && hasSource && await position == await duration;
}
}

Expand All @@ -179,7 +219,7 @@ class SpotubeAudioPlayer {
// audioplayers doesn't have the capability to get buffering state
return false;
} else {
throw UnimplementedError();
return _assetsAudioPlayer!.isBuffering.valueOrNull ?? false;
}
}

Expand All @@ -191,7 +231,11 @@ class SpotubeAudioPlayer {
return ap.DeviceFileSource(url);
}
} else {
throw UnimplementedError();
if (url.startsWith("https")) {
return aap.Audio.network(url);
} else {
return aap.Audio.file(url);
}
}
}

Expand All @@ -201,7 +245,7 @@ class SpotubeAudioPlayer {
// audioplayers doesn't have the capability to preload
return;
} else {
throw UnimplementedError();
return;
}
}

Expand All @@ -210,35 +254,54 @@ class SpotubeAudioPlayer {
if (apSupportedPlatform && urlType is ap.Source) {
await _audioPlayer?.play(urlType);
} else {
throw UnimplementedError();
await _assetsAudioPlayer?.stop();
await _assetsAudioPlayer?.open(
urlType as aap.Playable,
autoStart: true,
audioFocusStrategy: const aap.AudioFocusStrategy.request(
resumeAfterInterruption: true,
),
loopMode: aap.LoopMode.none,
playInBackground: aap.PlayInBackground.enabled,
headPhoneStrategy: aap.HeadPhoneStrategy.pauseOnUnplugPlayOnPlug,
showNotification: false,
respectSilentMode: true,
);
}
}

Future<void> pause() async {
await _audioPlayer?.pause();
await _assetsAudioPlayer?.pause();
}

Future<void> resume() async {
await _audioPlayer?.resume();
await _assetsAudioPlayer?.play();
}

Future<void> stop() async {
await _audioPlayer?.stop();
await _assetsAudioPlayer?.stop();
}

Future<void> seek(Duration position) async {
await _audioPlayer?.seek(position);
await _assetsAudioPlayer?.seek(position);
}

Future<void> setVolume(double volume) async {
await _audioPlayer?.setVolume(volume);
await _assetsAudioPlayer?.setVolume(volume);
}

Future<void> setSpeed(double speed) async {
await _audioPlayer?.setPlaybackRate(speed);
await _assetsAudioPlayer?.setPlaySpeed(speed);
}

Future<void> dispose() async {
await _audioPlayer?.dispose();
await _assetsAudioPlayer?.dispose();
}
}
4 changes: 4 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import FlutterMacOS
import Foundation

import assets_audio_player
import assets_audio_player_web
import audio_service
import audio_session
import audioplayers_darwin
Expand All @@ -24,6 +26,8 @@ import window_manager
import window_size

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AssetsAudioPlayerPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerPlugin"))
AssetsAudioPlayerWebPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerWebPlugin"))
AudioServicePlugin.register(with: registry.registrar(forPlugin: "AudioServicePlugin"))
AudioSessionPlugin.register(with: registry.registrar(forPlugin: "AudioSessionPlugin"))
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.2"
assets_audio_player:
dependency: "direct main"
description:
name: assets_audio_player
sha256: dcea8cd9c11cd9c34586f2446bfcdf099362159c56f97517ba941ac151974ea9
url: "https://pub.dev"
source: hosted
version: "3.0.6"
assets_audio_player_web:
dependency: transitive
description:
name: assets_audio_player_web
sha256: "4575ec40033d818ff022d48f7d46e24c01bc632bd1cd36a4f8b58b38e9aa4a81"
url: "https://pub.dev"
source: hosted
version: "3.0.6"
async:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:

dependencies:
args: ^2.3.2
assets_audio_player: ^3.0.6
async: ^2.9.0
audio_service: ^0.18.9
audio_session: ^0.1.13
Expand Down

0 comments on commit 1fff0f1

Please sign in to comment.