Skip to content

Commit

Permalink
Added: Support for AUDIO SOURCE tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dajanvulaj committed Aug 1, 2024
1 parent 214e551 commit 97916b1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
69 changes: 53 additions & 16 deletions packages/fwfh_just_audio/lib/src/internal/tag_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ignore_for_file: deprecated_member_use

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

import '../just_audio_factory.dart';
Expand All @@ -14,41 +15,77 @@ const kAttributeAudioPreloadNone = 'none';
const kAttributeAudioSrc = 'src';

const kTagAudio = 'audio';
const kTagAudioSource = 'source';

class TagAudio {
final JustAudioFactory wf;

TagAudio(this.wf);

BuildOp get buildOp => BuildOp(
// TODO: set debugLabel when our minimum core version >= 1.0
onWidgets: (meta, widgets) {
debugLabel: kTagAudio,
onRenderBlock: (tree, placeholder) {
if (defaultTargetPlatform != TargetPlatform.android &&
defaultTargetPlatform != TargetPlatform.iOS &&
defaultTargetPlatform != TargetPlatform.macOS &&
!kIsWeb) {
// these are the just_audio's supported platforms
// https://pub.dev/packages/just_audio/versions/0.9.5
return widgets;
return placeholder;
}

final attrs = meta.element.attributes;
final attrs = tree.element.attributes;
final url = wf.urlFull(attrs[kAttributeAudioSrc] ?? '');
if (url != null) {
tree.audioData.urls.add(url);
}

return _buildPlayer(tree) ?? placeholder;
},
onVisitChild: (tree, subTree) {
final e = subTree.element;
if (e.localName != kTagAudioSource) {

Check warning on line 47 in packages/fwfh_just_audio/lib/src/internal/tag_audio.dart

View check run for this annotation

Codecov / codecov/patch

packages/fwfh_just_audio/lib/src/internal/tag_audio.dart#L45-L47

Added lines #L45 - L47 were not covered by tests
return;
}
if (e.parent != tree.element) {

Check warning on line 50 in packages/fwfh_just_audio/lib/src/internal/tag_audio.dart

View check run for this annotation

Codecov / codecov/patch

packages/fwfh_just_audio/lib/src/internal/tag_audio.dart#L50

Added line #L50 was not covered by tests
return;
}

final attrs = e.attributes;

Check warning on line 54 in packages/fwfh_just_audio/lib/src/internal/tag_audio.dart

View check run for this annotation

Codecov / codecov/patch

packages/fwfh_just_audio/lib/src/internal/tag_audio.dart#L54

Added line #L54 was not covered by tests
final url = wf.urlFull(attrs[kAttributeAudioSrc] ?? '');
if (url == null) {
return widgets;
return;
}

final built = wf.buildAudioPlayer(
meta,
url,
autoplay: attrs.containsKey(kAttributeAudioAutoplay),
loop: attrs.containsKey(kAttributeAudioLoop),
muted: attrs.containsKey(kAttributeAudioMuted),
preload: attrs.containsKey(kAttributeAudioPreload) &&
attrs[kAttributeAudioPreload] != kAttributeAudioPreloadNone,
);

return listOrNull(built) ?? widgets;
tree.audioData.urls.add(url);

Check warning on line 60 in packages/fwfh_just_audio/lib/src/internal/tag_audio.dart

View check run for this annotation

Codecov / codecov/patch

packages/fwfh_just_audio/lib/src/internal/tag_audio.dart#L60

Added line #L60 was not covered by tests
},
);

Widget? _buildPlayer(BuildTree tree) {
final sourceUrls = tree.audioData.urls;
if (sourceUrls.isEmpty) {
return null;
}

final attrs = tree.element.attributes;
return wf.buildAudioPlayer(
tree,
sourceUrls.first,
autoplay: attrs.containsKey(kAttributeAudioAutoplay),
loop: attrs.containsKey(kAttributeAudioLoop),
muted: attrs.containsKey(kAttributeAudioMuted),
preload: attrs.containsKey(kAttributeAudioPreload) &&
attrs[kAttributeAudioPreload] != kAttributeAudioPreloadNone,
);
}
}

extension on BuildTree {
_TagAudioData get audioData =>
getNonInherited<_TagAudioData>() ?? setNonInherited(_TagAudioData());
}

@immutable
class _TagAudioData {
final urls = <String>[];
}
11 changes: 5 additions & 6 deletions packages/fwfh_just_audio/lib/src/just_audio_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mixin JustAudioFactory on WidgetFactory {

/// Builds [AudioPlayer].
Widget? buildAudioPlayer(
BuildMetadata meta,
BuildTree tree,
String url, {
required bool autoplay,
required bool loop,
Expand All @@ -29,13 +29,12 @@ mixin JustAudioFactory on WidgetFactory {
);

@override
void parse(BuildMetadata meta) {
switch (meta.element.localName) {
void parse(BuildTree tree) {
switch (tree.element.localName) {
case kTagAudio:
_tagAudio ??= TagAudio(this).buildOp;
meta.register(_tagAudio!);
tree.register(_tagAudio ??= TagAudio(this).buildOp);
break;
}
return super.parse(meta);
return super.parse(tree);
}
}
2 changes: 1 addition & 1 deletion packages/fwfh_just_audio/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fwfh_just_audio
version: 0.15.1
version: 0.15.2
description: WidgetFactory extension to render AUDIO with the just_audio plugin.
homepage: https://github.com/daohoangson/flutter_widget_from_html

Expand Down

0 comments on commit 97916b1

Please sign in to comment.