on_audio_room

Pub.dev Platforms Flutter

on_audio_room is a Flutter Package used to create a "room" to storage audio sections Favorites, Internal Playlists, Most Played, etc....

This Package works as a "extension" to on_audio_query and some methods will require it.

Help:

Any problem? Issues
Any suggestion? Pull request

Translations:

NOTE: Feel free to help with readme translations

Topics:

How to Install:

Add the following code to your pubspec.yaml:

dependencies:
  on_audio_room: ^2.2.1+1

Some Features:

  • Favorites section
  • Last Played section
  • Most Played section
  • Internal playlists section

TODO:

  • Add better performance for all plugin.
  • Add [Features].
  • Fix bugs.

How to use:

Before calling any method you need init the room/room's:

void main() async {
  //Init only one room.
  await OnAudioRoom().initRoom(RoomType.FAVORITES); //Add the RoomType. 
  runApp(MaterialApp(home: MyApp()));
}

//Or

void main() async {
  //Init all the room's.
  await OnAudioRoom().initRoom(); //Don't add the RoomType.
  runApp(MaterialApp(home: MyApp()));
}

Important:

  • This method will ONLY work 100% if you add a async/await, if don't, the widget will load almost in the same time and will throw a error.
  • REMEMBER TO CLOSE THE ROOM'S INSIDE THE DISPOSE.
  • See the documentation
OnAudioRoom() // The main method to start using the plugin.

All types of methods on this plugin:

Room methods

Methods Parameters Return
initRoom (RoomType, SubDir?) bool
closeRoom bool
deleteRoomFile bool
getRoomInfo RoomEntity
clearAll bool

Global methods

Methods Parameters Return
addTo (RoomType, Entity, PlaylistKey?) int?
addAllTo (RoomType, Entity, PlaylistKey?) List<int>
updateRoom (RoomType, Entity) bool
deleteFrom (RoomType, EntityKey, PlaylistKey?) bool
deleteAllFrom (RoomType, EntityKeys, PlaylistKey?) bool
clearRoom (RoomType) bool
checkIn (RoomType, EntityKey, PlaylistKey?) bool

Query methods

Methods Parameters Return
queryFromFavorites (EntityKey) FavoritesEntity
queryFavorites (Limit?, Reverse?, RoomSortType?) List<FavoritesEntity>
queryFromLastPlayed (EntityKey) LastPlayedEntity
queryLastPlayed (Limit?, Reverse?, RoomSortType?) List<LastPlayedEntity>
queryFromMostPlayed (EntityKey) MostPlayedEntity
queryMostPlayed (Limit?, Reverse?, RoomSortType?) List<MostPlayedEntity>
queryFromPlaylist (PlaylistKey, EntityKey) SongEntity
queryAllFromPlaylist (PlaylistKey, Limit?, Reverse?, RoomSortType?) List<SongEntity>

Playlist methods

Methods Parameters Return
createPlaylist (PlaylistName) int?
deletePlaylist (PlaylistKey) bool
renamePlaylist (PlaylistKey, NewName) bool
clearPlaylists bool
queryPlaylist (PlaylistKey) PlaylistEntity
queryPlaylists (Limit?, Reverse?) List<PlaylistEntity>

Examples:

All examples will use on_audio_query plugin to get songs/audios information

addTo

  //If you use [on_audio_query] just extend SongModel to create any entity.
  someName() async {
    //The return will be the song id inside the room.
    int? addToResult = await OnAudioRoom().addTo(
      RoomType.FAVORITES, 
      entity[index].getMap.toFavoritesEntity(),
    );
  }

  //If you don't use [on_audio_query] just create a map with all information.
  someOtherName() async {
    Map<dynamic, dynamic> favoritesEntity = {
      "_data": song.data,
      "_display_name": song.displayName,
      "_id": song.id,
      "album": song.album,
      "album_id": song.albumId,
      "artist": song.artist,
      "artist_id": song.artistId,
      "date_added": song.dateAdded,
      "duration": song.duration,
      "title": song.title,
      "artwork": song.artwork,
    };

    //Now, add to the room
    //The return will be the song key inside the room.
    int? addToResult = await OnAudioRoom().addTo(
      RoomType.FAVORITES, 
      favoritesEntity.toFavoritesEntity(),
    ); 
  }

addAllTo

  //If you use [on_audio_query] just extend SongModel to create any entity.
  someName() async {
    //Create a list with all SongModel.
    List<SongModel> listOfSongs;
    List<dynamic> listOfEntities;
    
    //Add all songs from model to entity.
    listOfSongs.forEach(element) {
      listOfEntities.add(element.getMap.toFavoritesEntity());
    }

    //Now, add to the room.
    //The return will be a list of song id inside the room.
    List<int> addAllToResult = await OnAudioRoom().addAllTo(
      RoomType.FAVORITES, 
      listOfEntities,
      //playlistKey,
    );
  }

  //If you don't use [on_audio_query] just create a list of map with all information.
  someOtherName() async {
    List<dynamic> listOfEntities;

    listOfSongs.forEach(element) {
      Map<dynamic, dynamic> favoritesEntity = {
      "last_data": song.data,
      "display_name": song.displayName,
      "id": song.id,
      "album": song.album,
      "album_id": song.albumId,
      "artist": song.artist,
      "artist_id": song.artistId,
      "date_added": song.dateAdded,
      "duration": song.duration,
      "title": song.title,
      "artwork": song.artwork,
      };

      listOfEntities.add(favoritesEntity.toFavoritesEntity());
    }

    //Now, add to the room
    //The return will be a list of song id inside the room.
    List<int> addAllToResult = await OnAudioRoom().addAllTo(
      RoomType.FAVORITES, 
      favoritesEntity,
      //playlistKey,
    ); 
  }

deleteFrom

  someName() async {
    //The return will be [true] if song has been deleted or [false] if not.
    bool deleteFromResult = await OnAudioRoom().deleteFrom(
      RoomType.FAVORITES,
      EntityKey,
      //playlistKey,
    );
  }

  //When [Adding/Deleting/Checking] songs from a playlist, remember to add the
  //[PlaylistKey] or you'll get a exception.
  someName() async {
    //The return will be [true] if song has been deleted or [false] if not.
    bool deleteFromResult = await OnAudioRoom().deleteFrom(
      RoomType.PLAYLIST,
      EntityKey,
      PlaylistKey: PlaylistKey,
    );
  }

deleteAllFrom

  someName() async {
    List<int> listOfKeysToDelete = [...];
    //The return will be [true] if all songs has been deleted or [false] if not.
    bool deleteAllFromResult = await OnAudioRoom().deleteAllFrom(
      RoomType.FAVORITES,
      listOfKeysToDelete,
      //playlistKey,
    );
  }

clearRoom

  //This one it's pretty simple.
  someName() async {
    //The return will be [true] if all songs has been deleted or [false] if not.
    bool deleteAllFromResult = await OnAudioRoom().clearRoom(
      RoomType.FAVORITES,
      //playlistKey,
    );
  }

checkIn

  //You'll use this one to check if some song has already been added.
  //Why? If you try to add the same song twice, will work. To avoid this, check if exist.
  someName() async {
    //The return will be [true] if song has already been added or [false] if not.
    bool checkInResult = await OnAudioRoom().checkIn(
      RoomType.FAVORITES,
      EntityKey,
      //playlistKey,
    );
  }

queryFromFavorites

  someName() async {
    //With this method you will define a key and will return all information
    //about this favorite song.
    FavoritesEntity? queryFromResult = await OnAudioRoom().queryFromFavorites(
      EntityKey,
    );
  }

queryFavorites

  someName() async {
    //With this method you will get all songs with all information based on Type[Entity].
    //
    //You can add a [int] as parameter to define how much song will return from this query.
    //You can add a [bool] as parameter to define if list will be reversed or no.
    //You can add a [RoomSortType] as parameter to define the list sort type.
    List<FavoritesEntity> queryResult = await OnAudioRoom().queryFavorites(
      100, //Default: 50
      true, //Default: false
      RoomSortType.TITLE //Default: null
    );
  }

LICENSE: