alnitak / flutter_soloud

Flutter low-level audio plugin using SoLoud C++ library and FFI
MIT License
209 stars 21 forks source link

fix: Web #94

Closed tejainece closed 1 month ago

tejainece commented 3 months ago

Flutter_Soloud should atleast let the app build for web and be a noop/throw until the Soloud is ported to WASM/Web.

daniloapr commented 2 months ago

You can do the trick using conditional imports. This way, the compiler will never import flutter_soloud in your web project. I I think this is a good candidate to include in the official docs.

I got it working like the example below.

Create the following files:

create_audio_service.dart

import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/create_audio_service_native.dart'
    if (dart.library.html) 'package:project/core/services/audio/create_audio_service_web.dart';

AudioService createAudioService() {
  return createAudioServiceForPlatform();
}

create_audio_service_web.dart

import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/no_audio_impl.dart';

AudioService createAudioServiceForPlatform() {
  return NoAudioImpl();
}

create_audio_service_native.dart

import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/soloud_impl.dart';

AudioService createAudioServiceForPlatform() {
  return SoLoudImpl();
}

audio_service.dart

abstract class AudioService {
  Future<void> init();
  void dispose();
  void play();
}

Then, I use get_it to inject the proper implemention, importing the create_audio_service file

locator.registerFactory<AudioService>(() => createAudioService());

I hope it helps.

alnitak commented 2 months ago

Hi @daniloapr and @tejainece,

it's not yet on pub.dev, but yesterday I merged in the main branch the web support.

It would be wonderful if you could try it!

alnitak commented 1 month ago

Version 2.1.0 has landed with web support!

I'll close this, thank to you all!