near final
This commit is contained in:
@@ -3,6 +3,11 @@ import 'package:audioplayers/audioplayers.dart' as audioplayers;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:just_audio/just_audio.dart' as justaudio;
|
||||
|
||||
enum AudioWrapper_Mode {
|
||||
Web,
|
||||
Mobile
|
||||
}
|
||||
|
||||
enum AudioWrapper_State {
|
||||
Playing,
|
||||
NotPlaying
|
||||
@@ -13,6 +18,16 @@ class AudioWrapper {
|
||||
audioplayers.AudioPlayer _audioPlayer_AudioPlayer = audioplayers.AudioPlayer();
|
||||
justaudio.AudioPlayer _justAudio_AudioPlayer = justaudio.AudioPlayer();
|
||||
|
||||
AudioWrapper_Mode mode = kIsWeb ? AudioWrapper_Mode.Web : AudioWrapper_Mode.Mobile;
|
||||
|
||||
AudioWrapper() {
|
||||
// mode = AudioWrapper_Mode.Web;
|
||||
|
||||
print("AudioWrapper mode: $mode");
|
||||
|
||||
// mode = AudioWrapper_Mode.Mobile;
|
||||
}
|
||||
|
||||
justaudio.AudioSource _convertSource_JustAudio(AudioWrapperSource source){
|
||||
if (source is AudioWrapperByteSource){
|
||||
return _ByteSource(source.bytes);
|
||||
@@ -33,45 +48,94 @@ class AudioWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Duration?> play(AudioWrapperSource source) async {
|
||||
|
||||
Duration? duration;
|
||||
|
||||
if (kIsWeb) {
|
||||
Future<void> loadSource(AudioWrapperSource source) async {
|
||||
if (mode == AudioWrapper_Mode.Web) {
|
||||
// Use just_audio
|
||||
|
||||
justaudio.AudioSource audioSource = _convertSource_JustAudio(source);
|
||||
|
||||
duration = await _justAudio_AudioPlayer.setAudioSource(audioSource);
|
||||
await _justAudio_AudioPlayer.setAudioSource(audioSource);
|
||||
|
||||
_justAudio_AudioPlayer.play();
|
||||
|
||||
|
||||
} else {
|
||||
} else if (mode == AudioWrapper_Mode.Mobile) {
|
||||
// Use audioplayers
|
||||
|
||||
audioplayers.Source audioSource = _convertSource_AudioPlayers(source);
|
||||
|
||||
await _audioPlayer_AudioPlayer.play(audioSource);
|
||||
await _audioPlayer_AudioPlayer.setSource(audioSource);
|
||||
// await _audioPlayer_AudioPlayer.play(audioSource);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Duration?> getDuration() async {
|
||||
if (mode == AudioWrapper_Mode.Web) {
|
||||
return _justAudio_AudioPlayer.duration;
|
||||
} else if (mode == AudioWrapper_Mode.Mobile) {
|
||||
return await _audioPlayer_AudioPlayer.getDuration();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Duration?> play() async {
|
||||
|
||||
Duration? duration;
|
||||
|
||||
if (mode == AudioWrapper_Mode.Web) {
|
||||
_justAudio_AudioPlayer.play();
|
||||
duration = _justAudio_AudioPlayer.duration;
|
||||
} else if (mode == AudioWrapper_Mode.Mobile) {
|
||||
_audioPlayer_AudioPlayer.resume();
|
||||
duration = await _audioPlayer_AudioPlayer.getDuration();
|
||||
}
|
||||
|
||||
|
||||
|
||||
return duration;
|
||||
}
|
||||
|
||||
// Future<Duration?> play(AudioWrapperSource source) async {
|
||||
//
|
||||
// Duration? duration;
|
||||
//
|
||||
// if (mode == AudioWrapper_Mode.Web) {
|
||||
// // Use just_audio
|
||||
//
|
||||
// justaudio.AudioSource audioSource = _convertSource_JustAudio(source);
|
||||
//
|
||||
// duration = await _justAudio_AudioPlayer.setAudioSource(audioSource);
|
||||
//
|
||||
// _justAudio_AudioPlayer.play();
|
||||
//
|
||||
//
|
||||
// } else if (mode == AudioWrapper_Mode.Mobile) {
|
||||
// // Use audioplayers
|
||||
//
|
||||
// audioplayers.Source audioSource = _convertSource_AudioPlayers(source);
|
||||
//
|
||||
// await _audioPlayer_AudioPlayer.play(audioSource);
|
||||
//
|
||||
// duration = await _audioPlayer_AudioPlayer.getDuration();
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// return duration;
|
||||
// }
|
||||
|
||||
void stop(){
|
||||
if (kIsWeb) {
|
||||
if (mode == AudioWrapper_Mode.Web) {
|
||||
_justAudio_AudioPlayer.stop();
|
||||
} else {
|
||||
try {
|
||||
_justAudio_AudioPlayer.dispose();
|
||||
} catch (e) {}
|
||||
_justAudio_AudioPlayer = justaudio.AudioPlayer();
|
||||
} else if (mode == AudioWrapper_Mode.Mobile) {
|
||||
_audioPlayer_AudioPlayer.stop();
|
||||
try {
|
||||
_audioPlayer_AudioPlayer.dispose();
|
||||
} catch (e) {}
|
||||
_audioPlayer_AudioPlayer = audioplayers.AudioPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
AudioWrapper_State get state {
|
||||
if (kIsWeb) {
|
||||
if (mode == AudioWrapper_Mode.Web) {
|
||||
if (_justAudio_AudioPlayer.playing){
|
||||
return AudioWrapper_State.Playing;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user