Skip to content

Commit

Permalink
feat: Futurebuilder for memory images
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinkey04 committed May 28, 2021
1 parent a031856 commit 68b89ad
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 77 deletions.
5 changes: 0 additions & 5 deletions lib/model/music_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';
import 'dart:typed_data';

Song songFromJson(String str) {
final jsonData = json.decode(str);
Expand All @@ -17,7 +16,6 @@ class Song {
String displayName;
String filePath;
String albumArtwork;
Uint8List albumArtWorkBytes;
String artist;
String album;
String duration;
Expand All @@ -30,7 +28,6 @@ class Song {
this.displayName,
this.filePath,
this.albumArtwork,
this.albumArtWorkBytes,
this.artist,
this.album,
this.duration,
Expand All @@ -44,7 +41,6 @@ class Song {
displayName: json["display_name"],
filePath: json["file_path"],
albumArtwork: json["album_artwork"],
albumArtWorkBytes: json["albumArtWorkBytes"],
artist: json["artist"],
album: json["album"],
duration: json["duration"],
Expand All @@ -58,7 +54,6 @@ class Song {
"display_name": displayName,
"file_path": filePath,
"album_artwork": albumArtwork,
"album_art_work_bytes": albumArtWorkBytes,
"artist": artist,
"album": album,
"duration": duration,
Expand Down
25 changes: 1 addition & 24 deletions lib/provider/audio_helper.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:io';
import 'dart:math';

import 'package:audiotagger/audiotagger.dart';
import 'package:flutter/animation.dart';
import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -56,25 +54,4 @@ Future<File> urlToFile(String imageUrl) async {
http.Response response = await http.get(Uri.parse(imageUrl));
await file.writeAsBytes(response.bodyBytes);
return file;
}

Future<String> webmToMP3(String path) async {
final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();
int result = 0;
final newPath = path.replaceAll('.webm', '.mp3');
var command = "-i \"$path\" -vn -ab 128k -ar 44100 -y \"$newPath\"";
await _flutterFFmpeg.execute(command).then((rc) {
print('FFmpeg Result: $rc');
result = rc;
});
File(path).delete();
return newPath;
}

Future<bool> tagArtWork(String url, String path) async {
var tagger = new Audiotagger();
File albumArtFile = await urlToFile(url);
final result = await tagger.writeTag(
path: path, tagField: "artist", value: 'Artist');
return result;
}
}
5 changes: 1 addition & 4 deletions lib/provider/db_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class DBProvider {
"display_name TEXT,"
"file_path TEXT,"
"album_artwork TEXT,"
"album_art_work_bytes BLOB,"
"artist TEXT,"
"album TEXT,"
"duration TEXT,"
Expand All @@ -49,14 +48,13 @@ class DBProvider {
final db = await database;
var raw = await db.rawInsert(
"INSERT Into Song "
" VALUES (?,?,?,?,?,?,?,?,?,?,?)",
" VALUES (?,?,?,?,?,?,?,?,?,?)",
[
newSong.id,
newSong.title,
newSong.displayName,
newSong.filePath,
newSong.albumArtwork,
newSong.albumArtWorkBytes,
newSong.artist,
newSong.album,
newSong.duration,
Expand Down Expand Up @@ -89,7 +87,6 @@ class DBProvider {
displayName: _item['display_name'],
filePath: _item['file_path'],
albumArtwork: _item['album_artwork'],
albumArtWorkBytes: _item['album_art_work_bytes'],
artist: _item['artist'],
album: _item['album'],
duration: _item['duration'],
Expand Down
8 changes: 1 addition & 7 deletions lib/view/download_music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class _DownloadMusicState extends State<DownloadMusic> {
.replaceAll('/', ' ')
.replaceAll(':', ' ');
//TODO Do something efficient to choose only alpha-numeric characters from $title
var filePath = _raagDownloadsDirectory.path + '/' + tempTitle + '.webm';
var filePath = _raagDownloadsDirectory.path + '/' + tempTitle + '.mp3';

if (streamInfo != null) {
var stream = yt.videos.streamsClient.get(streamInfo);
Expand All @@ -156,12 +156,6 @@ class _DownloadMusicState extends State<DownloadMusic> {
return s;
}).pipe(fileSink);

setTitle('Converting to MP3');
filePath = await webmToMP3(filePath);

// setTitle('Adding tags');
// print(await tagArtWork(video.thumbnails.mediumResUrl, filePath));

setTitle(downloadComplete);
setBody(
'$fileLocation: $filePath\n$fileSize: ${(streamInfo.size.totalMegaBytes.toString().substring(0, 4))} MB');
Expand Down
3 changes: 0 additions & 3 deletions lib/view/splash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ class SplashScreen extends StatefulWidget {
await DBProvider.db.deleteAll();
List<SongInfo> songs = await FlutterAudioQuery().getSongs();
for (var it = 0; it < songs.length; it++) {
Uint8List _albumArtBytes = await FlutterAudioQuery()
.getArtwork(type: ResourceType.SONG, id: songs[it].id);
DBProvider.db.insertSong(new Song(
id: songs[it].id,
title: songs[it].title,
displayName: songs[it].displayName,
filePath: songs[it].filePath,
albumArtwork: songs[it].albumArtwork,
albumArtWorkBytes: _albumArtBytes,
artist: songs[it].artist,
album: songs[it].album,
duration: songs[it].duration,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/refresh_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RefreshButton extends StatelessWidget {
onTap: () async {
showAlert();
var _oldCount = await DBProvider.db.getCount();
_preferencesProvider.setBool(Preferences.DB_POPULATED, false);
// _preferencesProvider.setBool(Preferences.DB_POPULATED, false);
await SplashScreen.populateSongsIntoDB();
var _diff = await DBProvider.db.getCount() - _oldCount;
var toastText = (_diff < 0) ? ' songs removed' : ' new songs added';
Expand Down
51 changes: 27 additions & 24 deletions lib/widgets/song_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_audio_query/flutter_audio_query.dart';
import 'package:provider/provider.dart';
import 'package:raag/model/music_model.dart';
import 'package:raag/provider/audio_helper.dart';
Expand All @@ -15,28 +17,36 @@ class SongWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
final provider = Provider.of<PlayerProvider>(context);
final screenHeight = MediaQuery.of(context).size.height;
final provider = Provider.of<PlayerProvider>(context, listen: false);

Widget getAlbumArt(Song song, BuildContext context) {
final defaultIcon = Icon(Icons.music_note_sharp,
size: 24, color: Theme.of(context).dividerColor);
if (song.albumArtwork !=
null) // Directly access album art when scoped storage approach is not used (less than Android API level 29)
return Image(
image: FileImage(File(song.albumArtwork)),
);

final defaultIcon = Icon(Icons.music_note_sharp,
size: 24, color: Theme.of(context).dividerColor);

return (song.albumArtWorkBytes == null || song.albumArtWorkBytes.isEmpty)
? defaultIcon
: Image.memory(song.albumArtWorkBytes);
return FutureBuilder(
future: FlutterAudioQuery()
.getArtwork(type: ResourceType.SONG, id: song.id),
builder: (context, snapshot) {
Uint8List _imageBytes = snapshot.data;
if (_imageBytes == null || _imageBytes.isEmpty)
return defaultIcon;
else
return Image.memory(_imageBytes);
},
);
}

return Column(
children: [
Flexible(
child: ListView.builder(
itemCount: songList?.length,
padding: EdgeInsets.only(bottom: screenHeight * 0.2),
itemBuilder: (context, songIndex) {
Song song = songList[songIndex];
if (song.displayName.contains(".mp3"))
Expand All @@ -56,9 +66,9 @@ class SongWidget extends StatelessWidget {
provider.playerState = PlayerState.playing;
provider.audioManagerInstance
.start("file://${song.filePath}", song.title,
desc: song.displayName,
auto: true,
cover: song.albumArtwork)
desc: song.displayName,
auto: true,
cover: song.albumArtwork)
.then((err) {
print(err);
});
Expand All @@ -74,9 +84,7 @@ class SongWidget extends StatelessWidget {
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: Container(
color: Theme
.of(context)
.accentColor,
color: Theme.of(context).accentColor,
width: 50,
height: 50,
child: getAlbumArt(song, context))),
Expand All @@ -85,31 +93,26 @@ class SongWidget extends StatelessWidget {
),
Container(
width:
MediaQuery
.of(context)
.size
.width * 0.7,
MediaQuery.of(context).size.width * 0.7,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(song.title,
overflow: TextOverflow.ellipsis,
style: Theme
.of(context)
style: Theme.of(context)
.textTheme
.headline3),
Text(song.artist,
overflow: TextOverflow.ellipsis,
style: Theme
.of(context)
style: Theme.of(context)
.textTheme
.subtitle2),
],
),
),
SizedBox(width: 6),
SizedBox(width: screenWidth * 0.01),
Text(
parseToMinutesSeconds(
int.parse(song.duration)),
Expand Down
7 changes: 0 additions & 7 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.5+6"
flutter_ffmpeg:
dependency: "direct main"
description:
name: flutter_ffmpeg
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.3+3
version: 1.0.4+4

environment:
sdk: ">=2.7.0 <3.0.0"
Expand All @@ -31,7 +31,6 @@ dependencies:
connectivity: ^2.0.2
downloads_path_provider_28: ^0.1.0
flutter_audio_query: ^0.3.5+6
flutter_ffmpeg: ^0.4.0
fluttertoast: ^7.1.1
http: ^0.13.3
marquee: ^1.6.1
Expand Down

0 comments on commit 68b89ad

Please sign in to comment.