Flutter-AssetsAudioPlayer icon indicating copy to clipboard operation
Flutter-AssetsAudioPlayer copied to clipboard

Play sound as soon as we press the play button. Now it takes some time to play the sound which is not good for my app

Open Kashish-Manandhar opened this issue 4 years ago • 13 comments

Describe the feature you'd like A clear and concise description of what you want to happen.

Kashish-Manandhar avatar May 12 '21 04:05 Kashish-Manandhar

What type of audio format are you using? local or online? If its online make sure the cache is turn off since if its on it will take some time to load before playing. If its offline you can try the app in production mode since reading/writing in debug mode is slower.

Mayb3Nots avatar May 16 '21 12:05 Mayb3Nots

I am using the audio from the assets folder

Kashish-Manandhar avatar May 17 '21 04:05 Kashish-Manandhar

In that case have you tired testing in production mode? If its still slow what is the time of loading?

Mayb3Nots avatar May 17 '21 04:05 Mayb3Nots

What type of audio format are you using? local or online? If its online make sure the cache is turn off since if its on it will take some time to load before playing. If its offline you can try the app in production mode since reading/writing in debug mode is slower.

From where can I turn off the cache? any samples?

yubarajshrestha avatar May 22 '21 09:05 yubarajshrestha

What type of audio format are you using? local or online? If its online make sure the cache is turn off since if its on it will take some time to load before playing. If its offline you can try the app in production mode since reading/writing in debug mode is slower.

From where can I turn off the cache? any samples?

Under Audio there is a cache property. By default it is disabled.

Mayb3Nots avatar May 22 '21 12:05 Mayb3Nots

What type of audio format are you using? local or online? If its online make sure the cache is turn off since if its on it will take some time to load before playing. If its offline you can try the app in production mode since reading/writing in debug mode is slower.

From where can I turn off the cache? any samples?

Under Audio there is a cache property. By default it is disabled.

Didn't help. Is this happening because of debug mode?

yubarajshrestha avatar May 30 '21 15:05 yubarajshrestha

Can you provide some code sample and video

Mayb3Nots avatar May 30 '21 15:05 Mayb3Nots

This is global.

library library.globals;

import 'package:assets_audio_player/assets_audio_player.dart';

class Player {
  AssetsAudioPlayer player = AssetsAudioPlayer.withId('saylo_player');

  static final Map<String, Player> _cache = {};

  Player._createInstance();

  factory Player() {
    if (!_cache.containsKey("player")) {
      Player instance = Player._createInstance();
      _cache["player"] = instance;
      instance.player.onErrorDo = (handler) {
        handler.player.stop();
      };
    }
    return _cache['player'];
  }
}

AssetsAudioPlayer player = Player().player;

Inside Provider...

player.open(
      Playlist(
        audios: songs
            .map<Audio>(
              (song) => Audio.network(
                song.songUrl,
                metas: Metas(
                  id: song.id,
                  title: song.name,
                  artist: song.artist ?? "artist",
                  album: song.album ?? "album",
                  image: MetasImage.network(song.coverUrl),
                ),
              ),
            )
            .toList(),
      ),
      showNotification: true,
      autoStart: false,
);
player.play();

Also it plays twice sometimes. And can't stop without closing app.

yubarajshrestha avatar May 30 '21 15:05 yubarajshrestha

This is global.

library library.globals;

import 'package:assets_audio_player/assets_audio_player.dart';

class Player {
  AssetsAudioPlayer player = AssetsAudioPlayer.withId('saylo_player');

  static final Map<String, Player> _cache = {};

  Player._createInstance();

  factory Player() {
    if (!_cache.containsKey("player")) {
      Player instance = Player._createInstance();
      _cache["player"] = instance;
      instance.player.onErrorDo = (handler) {
        handler.player.stop();
      };
    }
    return _cache['player'];
  }
}

AssetsAudioPlayer player = Player().player;

Inside Provider...

player.open(
      Playlist(
        audios: songs
            .map<Audio>(
              (song) => Audio.network(
                song.songUrl,
                metas: Metas(
                  id: song.id,
                  title: song.name,
                  artist: song.artist ?? "artist",
                  album: song.album ?? "album",
                  image: MetasImage.network(song.coverUrl),
                ),
              ),
            )
            .toList(),
      ),
      showNotification: true,
      autoStart: false,
);
player.play();

Also it plays twice sometimes. And can't stop without closing app.

Looks good to me, however you might wanna use auto start insead of manually calling play.

Mayb3Nots avatar May 31 '21 16:05 Mayb3Nots

This is global.

library library.globals;

import 'package:assets_audio_player/assets_audio_player.dart';

class Player {
  AssetsAudioPlayer player = AssetsAudioPlayer.withId('saylo_player');

  static final Map<String, Player> _cache = {};

  Player._createInstance();

  factory Player() {
    if (!_cache.containsKey("player")) {
      Player instance = Player._createInstance();
      _cache["player"] = instance;
      instance.player.onErrorDo = (handler) {
        handler.player.stop();
      };
    }
    return _cache['player'];
  }
}

AssetsAudioPlayer player = Player().player;

Inside Provider...

player.open(
      Playlist(
        audios: songs
            .map<Audio>(
              (song) => Audio.network(
                song.songUrl,
                metas: Metas(
                  id: song.id,
                  title: song.name,
                  artist: song.artist ?? "artist",
                  album: song.album ?? "album",
                  image: MetasImage.network(song.coverUrl),
                ),
              ),
            )
            .toList(),
      ),
      showNotification: true,
      autoStart: false,
);
player.play();

Also it plays twice sometimes. And can't stop without closing app.

Looks good to me, however you might wanna use auto start insead of manually calling play.

Didn't work either.

yubarajshrestha avatar Jun 03 '21 16:06 yubarajshrestha

player.open(
      Playlist(
        audios: songs
            .map<Audio>(
              (song) => Audio.network(

@Kashish-Manandhar are you using Audio.network? if you're playing audio from assets folder like you mention - you might want to try Audio.file

markst avatar Jun 25 '21 03:06 markst

ok i will try to use it

Kashish-Manandhar avatar Jun 25 '21 16:06 Kashish-Manandhar

It feels like the player is starting to download and write the file and then start to play

nimr77 avatar Oct 03 '22 13:10 nimr77