rxlabz / audioplayer

A flutter plugin to play audio files iOS / Android / MacOS / Web ( Swift/Java )

Home Page:https://pub.dartlang.org/packages/audioplayer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Play mp3 from Asset

Gabriel-Espinoza opened this issue · comments

Hello, do anyone know how can I play a WAV or MP3 from an asset? - I've tried using isLocal: true with no luck.

thanks

Copy the audio files audio1.mp3 and audio2.wav to

lib/assets/audio

and place the following in pubspec.yaml:

assets:
  - assets/audio/audio1.mp3
  - assets/audio/audio2.wav

then to copy from bundle to the mobile's local directory:

import 'package:path_provider/path_provider.dart';
import 'package:flutter/services.dart' show rootBundle;

  Future copyLocalAssets() async {
    final bundleDir = 'assets/audio';
    final assetName1 = 'audio1.mp3';
    final assetName2 = 'audio2.wav';
    final localDir = await getApplicationDocumentsDirectory();
    final localAssetFile1 = await copyLocalAsset(localDir, bundleDir, assetName1);
    final localAssetFile2 = await copyLocalAsset(localDir, bundleDir, assetName2);
    myLocalAssetPath1 = localAssetFile1.path;
    myLocalAssetPath2 = localAssetFile2.path;
  }

  Future<File> copyLocalAsset(
      Directory localDir, String bundleDir, String assetName) async {
    final data = await rootBundle.load('$bundleDir/$assetName');
    final bytes = data.buffer.asUint8List();
    final localAssetFile = File('${localDir.path}/$assetName');
    await localAssetFile.writeAsBytes(bytes, flush: true);
    return localAssetFile;
  }

Then u can play them when needed with:

    audioPlayer.play(myLocalAssetPath1, isLocal: true);
    audioPlayer.play(myLocalAssetPath2, isLocal: true);

Works for both mp3 and wav on both iOS and Android.
Tip: audio format should be stereo not mono. (Other formatting specs may be required)

Thank you very much, it worked like a charm. FYI, you need dart:io package too

Copy the audio files audio1.mp3 and audio2.wav to

lib/assets/audio

and place the following in pubspec.yaml:

assets:
  - assets/audio/audio1.mp3
  - assets/audio/audio2.wav

then to copy from bundle to the mobile's local directory:

import 'package:path_provider/path_provider.dart';
import 'package:flutter/services.dart' show rootBundle;

  Future copyLocalAssets() async {
    final bundleDir = 'assets/audio';
    final assetName1 = 'audio1.mp3';
    final assetName2 = 'audio2.wav';
    final localDir = await getApplicationDocumentsDirectory();
    final localAssetFile1 = await copyLocalAsset(localDir, bundleDir, assetName1);
    final localAssetFile2 = await copyLocalAsset(localDir, bundleDir, assetName2);
    myLocalAssetPath1 = localAssetFile1.path;
    myLocalAssetPath2 = localAssetFile2.path;
  }

  Future<File> copyLocalAsset(
      Directory localDir, String bundleDir, String assetName) async {
    final data = await rootBundle.load('$bundleDir/$assetName');
    final bytes = data.buffer.asUint8List();
    final localAssetFile = File('${localDir.path}/$assetName');
    await localAssetFile.writeAsBytes(bytes, flush: true);
    return localAssetFile;
  }

Then u can play them when needed with:

    audioPlayer.play(myLocalAssetPath1, isLocal: true);
    audioPlayer.play(myLocalAssetPath2, isLocal: true);

Works for both mp3 and wav on both iOS and Android. Tip: audio format should be stereo not mono. (Other formatting specs may be required)

现在应该是先调用:
setFilePath

play