Flutter Web音频在部署后不工作

b09cbbtk  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(224)
void listenToOrderChanges() {
    CollectionReference reference = FirebaseFirestore.instance.collection('orders');
    orderSubscription = reference.snapshots().listen((querySnapshot) {
      final addedChanges = querySnapshot.docChanges.where((change) => change.type == DocumentChangeType.added).toList();
      if (isFirstLaunch) {
        isFirstLaunch = false;
      } else if (addedChanges.isNotEmpty) {
        // Play alert sound
        html.AudioElement audioElement = html.AudioElement('assets/mixkit.wav');
        audioElement.play();

        // Show Snackbar
        Get.snackbar('New order added', 'alert');
      }
    });
  }

当新文档使用Flutter Web添加到集合中时,我试图使用snackbar获取声音,在部署之前它工作正常,但在部署之后它会给snakbar没有声音。
我试过很多包,但都有同样的问题

wnavrhmk

wnavrhmk1#

我使用了这个软件包https://pub.dev/packages/audioplayer并为web工作

import 'dart:typed_data';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/services.dart';

代码:

AudioPlayer player = AudioPlayer();
String audioasset = "assets/sound/bell-dinging-jam-fx-2-2-00-05.mp3";

ByteData bytes = await rootBundle.load(audioasset); //load sound from assets

Uint8List soundbytes = bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);

final urlSource = UrlSource(Uri.dataFromBytes(soundbytes, mimeType:'audio/mpeg').toString());

player.play(urlSource);

相关问题