Skip to content

Instantly share code, notes, and snippets.

@tomasbaran
Created December 4, 2021 11:45
Show Gist options
  • Save tomasbaran/758b7a073a7b92ca61f0da1d8080c6aa to your computer and use it in GitHub Desktop.
Save tomasbaran/758b7a073a7b92ca61f0da1d8080c6aa to your computer and use it in GitHub Desktop.
audio background not looping
import 'package:ambee2/models/animations/light_animation.dart';
import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';
// import 'package:just_audio/just_audio.dart';
import 'package:audioplayers/audioplayers.dart';
// Two lines below: this is how I call the audio to start
// await lightAnimation.audioHandler.setRepeatMode(AudioServiceRepeatMode.all);
// await lightAnimation.audioHandler.playUrl(lightAnimation.audioUrl, volumeSetValue);
class LightAnimation {
String audioUrl;
bool isAudioDownloaded;
AudioPlayerHandler audioHandler = AudioPlayerHandler();
LightAnimation({
this.isAudioDownloaded = false,
this.soundId,
this.audioUrl,
});
}
class AudioPlayerHandler extends BaseAudioHandler {
final _player = AudioPlayer();
AudioPlayerHandler() {
//set the audio to repeat itself once it's done
_player.setReleaseMode(ReleaseMode.LOOP);
}
Future<void> setUrl(String url) async => await _player.setUrl(url);
Future<void> playUrl(String url, double volumeValue) => _player.play(url, volume: volumeValue);
Future<void> resume() => _player.resume();
@override
Future<void> pause() => _player.pause();
@override
Future<void> stop() => _player.stop();
Future<void> setVolume(double newValue) => _player.setVolume(newValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment