Flutter中的audioplayer插件-无法加载资产

时间:2018-12-29 05:48:12

标签: dart flutter

我正在尝试在Flutter中创建一个警报,该警报会在一定时间后响起。看来这比在Flutter中说起来容易!

试图使用audioplayer插件实现此目的。使用了playLocal函数,其中资产从rootbundle加载到app目录中,然后播放

根据audioplayer github存储库中的答案,这是应该完成此操作的代码:

class SoundManager {
 AudioPlayer audioPlayer = new AudioPlayer();

Future playLocal(localFileName) async {
  final dir = await getApplicationDocumentsDirectory();
  final file = new File("${dir.path}/$localFileName");
  if (!(await file.exists())) {
    final soundData = await rootBundle.load("assets/$localFileName");
    final bytes = soundData.buffer.asUint8List();
    await file.writeAsBytes(bytes, flush: true);
  }
  await audioPlayer.play(file.path, isLocal: true);
 }
}

我不断收到错误消息:“无法加载资产”。资产(mp3 / wav文件)显然在该文件夹中,并且该文件夹正确包含在pubspec.yaml文件中(其他图像资产已从该文件夹正确加载,因此在这里指定文件夹本身不是问题)

2 个答案:

答案 0 :(得分:0)

您可以使用另一个音频库https://pub.dev/packages/audioplayers

AudioCache文档。 https://github.com/luanpotter/audioplayers/blob/master/doc/audio_cache.md

简单的例子:

import 'package:audioplayers/audio_cache.dart';
AudioCache player = AudioCache();
player.play('sounds/test_sound.m4a');

在此示例中,我的资产文件夹如下所示:assets / sounds / test_sound.m4a

该库将音频缓存为本地文件,然后播放音频

答案 1 :(得分:0)

这适用于 iOS 和 Android。注意:如果本地不可用,则从 url 下载。

AudioProvider audioProvider;
_playSound() async {
    audioProvider = AudioProvider("http:...");
    var soundToPlay = "myLocalSound";
    String localUrl = await audioProvider.load(soundToPlay);
    SoundController.play(localUrl);
  }
}

audio_provider.dart

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart';

typedef void OnError(Exception exception);

class AudioProvider {
  String url;
  AudioProvider(String url) {
    this.url = url;
  }

  Future<Uint8List> _loadFileBytes(String url, {OnError onError}) async {
    Uint8List bytes;
    try {
      bytes = await readBytes(url);
    } on ClientException {
      rethrow;
    }
    return bytes;
  }

  Future<String> load(fileName) async {
    final dir = await getApplicationDocumentsDirectory();
    final file = new File('${dir.path}/$fileName');
    if (await file.exists()) {print("file exists");
      return file.path;
    }
    var filePath = url +fileName;
    final bytes = await _loadFileBytes(filePath,
        onError: (Exception exception) =>
            print('audio_provider.load => exception ${exception}'));
    await file.writeAsBytes(bytes);
    if (await file.exists()) {
      return file.path;
    }
    return '';
  }
}

soundController.dart

import 'package:flutter/foundation.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'dart:io' show Platform;


void audioPlayerHandler(AudioPlayerState value) => null;

class SoundController {

  static AudioPlayer audioPlayer = AudioPlayer(mode: PlayerMode.LOW_LATENCY);
  static AudioCache audioCache = AudioCache(prefix: "assets/audio/", fixedPlayer: audioPlayer);

  static void play(String sound) {

    if (!kIsWeb && Platform.isIOS) {
      audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
    }
    audioPlayer.play(sound, isLocal: true);
  }
}