audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

Byte array audio source (BytesSource, setSourceBytes) on iOS, MacOS, Linux and Web.

Open FluffyBunniesTasteTheBest opened this issue 3 years ago • 8 comments
trafficstars

Checklist

  • [Yes] I made sure that the feature I am proposing doesn't already exist According to the Feature Parity Table it doesn't.

Use case / Problem

Which problem would be solved with this feature? In general, it would enable us to play audio that's already in memory, which would also enable the use of sound fonts through dart_melty_soundfont Why it fits in this package (and not a standalone package for example) Audio sources seem to fit best in this package, but a standalone package would be fine as well.

Proposal / Solution

What do you propose as a solution? Add as much information as you can! Adding byte arrays as audio source to the yet missing platforms.

What API changes that are necessary? Given the fact that it's already supported on Android, the changes in API will most likely be minimal.

Are there any breaking changes expected? Is it a new flag to pass through to android/ios? Is it just manageable with some built in functions? Not that I can think of.

Platforms

Make sure to identify for which platforms this must be implemented According to the Feature Parity Table it's missing on iOS, MacOS, Windows, Linux and Web.

Affected Platforms: According to the Feature Parity Table it's missing on iOS, MacOS, Windows, Linux and Web.

This feature would make sense on all platforms.

Are you interested in working on a PR for this? Sorry, can't.

Is there a technical difficulty to why this feature is not implemented in dart instead of platform specific code?

YoussefLasheen avatar Feb 04 '23 13:02 YoussefLasheen

@YoussefLasheen probably not, haven't looked into it. Maybe one can try to implement this for web and see how it goes. This could be more easily applied to all platforms, I think. We just need more developers who contribute to open source :)

Gustl22 avatar Feb 09 '23 16:02 Gustl22

I recently came across this and found an easy solution, but I am not sure if it works for all platforms. it's using data: url source e.g.:

UrlSource urlSourceFromBytes(Uint8List bytes,{String mimeType= "audio/mpeg"}) {
  return  UrlSource(Uri.dataFromBytes(bytes, mimeType: mimeType).toString());
}

however I noticed that mimeType is a requirement here for it to work correctly, also I only tried this for mp3 format.

ahmednfwela avatar Feb 17 '23 04:02 ahmednfwela

+1

animator avatar May 17 '23 16:05 animator

I recently came across this and found an easy solution, but I am not sure if it works for all platforms. it's using data: url source e.g.:

UrlSource urlSourceFromBytes(Uint8List bytes,{String mimeType= "audio/mpeg"}) {
  return  UrlSource(Uri.dataFromBytes(bytes, mimeType: mimeType).toString());
}

however I noticed that mimeType is a requirement here for it to work correctly, also I only tried this for mp3 format.

What platform did you tested? It's not working on iOS.

lcw99 avatar May 21 '23 07:05 lcw99

mainly web @lcw99

ahmednfwela avatar May 21 '23 07:05 ahmednfwela

Hi, why it threw an exception on iOS:

flutter: \^[[31mAudioPlayers Exception: AudioPlayerException(
	UrlSource(url: data:audio/mpeg;base64,UklGRiTUBABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQDUBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<…>
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(DarwinAudioError, AVPlayerItem.Status.failed on setSourceUrl, null, null)
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(DarwinAudioError, AVPlayerItem.Status.failed on setSourceUrl, null, null)

I use this code:

I recently came across this and found an easy solution, but I am not sure if it works for all platforms. it's using data: url source e.g.:↳

UrlSource urlSourceFromBytes(Uint8List bytes,{String mimeType= "audio/mpeg"}) {
  return  UrlSource(Uri.dataFromBytes(bytes, mimeType: mimeType).toString());
}

however I noticed that mimeType is a requirement here for it to work correctly, also I only tried this for mp3 format.

doubleLLL3 avatar Aug 06 '23 01:08 doubleLLL3

I recently came across this and found an easy solution, but I am not sure if it works for all platforms. it's using data: url source e.g.:

UrlSource urlSourceFromBytes(Uint8List bytes,{String mimeType= "audio/mpeg"}) {
  return  UrlSource(Uri.dataFromBytes(bytes, mimeType: mimeType).toString());
}

however I noticed that mimeType is a requirement here for it to work correctly, also I only tried this for mp3 format.

Moi ca marche pour moi...

J'utilise File_picker et le fichier est converti en Base64 puis je reccupere le contentType Et en votre code ca me donne ca

void playFileAudio() async {
    if (isUrl(this.chantUser.audioPath)) {
      await mediaPlayerBloc.setSourceUrl(path: this.chantUser.audioPath);
      await mediaPlayerBloc.resume();
    } else if (isBase64(this.chantUser.audioPath)) {

      final bytes = base64Decode(this.chantUser.audioPath);
      var url = urlSourceFromBytes(bytesAudio: bytes, mimeType: this.chantUser.contentTypeAudio);
      //await mediaPlayerBloc.setSourceBytes(bytesAudio: bytes, playerId: this.chantUser.noChant);
      await mediaPlayerBloc.setSourceUrl(path: url.url, playerId: this.chantUser.noChant);
      await mediaPlayerBloc.resume();
    }
  }
  UrlSource urlSourceFromBytes({ required Uint8List bytesAudio, String mimeType= "audio/mpeg"}) {
    var uri =Uri.dataFromBytes(bytesAudio, mimeType: mimeType).toString();
    return UrlSource(uri);
  }

pour sélectionner le fichier

void openFileAudioExplorer() async {
    try {
      showLoadingAudio();
      final pickedFile = await FilePicker.platform.pickFiles(
          allowedExtensions: Constant.audioExtensionsAllowed,
          type: FileType.custom,
          dialogTitle: 'label_fichier_audio'.tr);
      if (pickedFile == null) {
        showLoadingAudio(show: false);
        return;
      }
      final PlatformFile file = pickedFile.files.first;
      final bytes = File(file.path!).readAsBytesSync();
      String img64 = base64Encode(bytes);
      setState(() {
        //this.chantUser.audioPath = file.path!;
        this.chantUser.audioPath = img64;
        this.chantUser.audioPathBase64 = img64;
        this.chantUser.sizeAudio = file.size.toString();
        this.chantUser.extentionAudio = file.extension ?? '';
        this.chantUser.contentTypeAudio = 'audio/${file.extension ?? '*'}';
      });
      showLoadingAudio(show: false);
    } catch(ex, trace ){
     logError(ex, trace: trace);
    }
  }

Grafritz avatar Aug 22 '23 03:08 Grafritz