How to play local mp3 file with audioplayer plugin in Flutter - flutter

How to play local mp3 file with audioplayer 0.2.0 in Flutter.
pubspec.yaml
flutter:
assets:
- sounds/music.mp3
main.dart
Future<ByteData> loadAsset() async {
return await rootBundle.load('sounds/music.mp3');
}
// FIXME: This code is not working.
Future playLocal() async {
final result = await audioPlayer.play(loadAsset());
if (result == 1) setState(() => playerState = PlayerState.playing);
}
Can I get local assets path from rootBundle?
Can I pay audio from ByteData on audioPlayer?

The audioplayer plugin currently only supports network paths and files. You can move an asset to a temporary folder and play it with this code:
import 'package:path_provider/path_provider.dart';
...
final file = new File('${(await getTemporaryDirectory()).path}/music.mp3');
await file.writeAsBytes((await loadAsset()).buffer.asUint8List());
final result = await audioPlayer.play(file.path, isLocal: true);

I have just created a repo in github that uses audioplayers plugin to stream local mp3 files. This player can seek, pause, stop and play local audio
https://github.com/samupra/local_flutter_audio_player

A way that worked for me was to create the assets file and load the mp3 file in to the directory.
Then load the file using the Future
AudioPlayer audio = await AudioCache.play("filetobeloaded.mp3");

Related

Flutter file word readable

I want to write a file with flutter, that has got the same function as WORLD_READABLE in android. So i want tha this file can be read by all the applications.
More clear: i have an app A that write hello.txt i want that another app B can read this file. How i should set my file? There is a parameter of some function in the package path_provider?
I seen the package path_provider and File class, but i didn't find something.
I have already tried by access direct to file from app B, but is not world readable.
Something like this should work and make sure you use the latest path_provider dependency. getExternalStorageDirectory() does the trick!
//Write file from App 1
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<void> writeToFile(String data) async {
final directory = await getExternalStorageDirectory();
final file = File('${directory.path}/hello.txt');
await file.writeAsString(data);
}
//Read the written file from App 2
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<String> readFromFile() async {
final directory = await getExternalStorageDirectory();
final file = File('${directory.path}/hello.txt');
return file.readAsString();
}

flutter_downloader auto open file when complete

so I'm trying to work with download manager in flutter using flutter_downloader package. I manage to make my app download the file I wanted. How can I make my app more advance ? I wanted my app to be able to open the downloaded file automatically when the download is done.
Another question is, if I already download the file first time how can I make the button trigger to open the file location rather than downloading the file again ?
Here's my current code:
TextButton(
child: Text(
ticketData['file_attachment_map'][index]['name'],
style: primaryColor600Style.copyWith(fontSize:14.sp),
),
onPressed: () async {
final permissionStatus = await Permission.storage.request();
if (permissionStatus.isGranted) {
final externalDir = await getExternalStorageDirectory();
final taskId =await FlutterDownloader.enqueue(
url: ticketData['file_attachment_map']
[index]['url'],
savedDir:externalDir!.path,
showNotification:true,
openFileFromNotification:true,
);
} else {
print('Permission denied');
}
},
),
Use open_file package after downloaded
if(taskId != null){
await OpenFile.open(filePath);
}
}
And you can check file exist before download file
import 'dart:io';
File("path/to/file").exists()
As a complement to #Xuann Thucc answer, you can use the open_filex which is a fork of open_file but is better maintained and fixes many issues with the original lib.
import 'package:open_filex/open_filex.dart';
// in async function
await OpenFilex.open(filePath);

Is there any option to hide/ remove .hive files?

When I used hive as local db in flutter, .hive files are generated in user's app-directory. Is there any way to hide or remove these files from the app-directory?
You can remove file which contains the box and closes the box by using this method.
deleteFromDisk
Use path_provider to store hive file in getApplicationSupportDirectory
Future<void> init() async {
final path = await getApplicationSupportDirectory();
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
await Hive.openBox<User>(HiveConst.userDataBox, path: path.path);
}

flutter path_provider could not read downloaded files

I am using path_provider on flutter and would like to build Download file(mp4&jpg, from aws S3) and play it. I succeeded download and confirmed file is downloaded.(on ios)
But, when access the files using path_provider, raised error like below.
Unable to load asset: /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers
the saving method is below
Future<File> _getFile(String filename) async {
final dir = await getApplicationDocumentsDirectory();
return File("${dir.path}/$filename");
}
final file = await _getFile("filename");
file.writeAsBytes(Bytes);
and the load method is below
final directory = await getApplicationDocumentsDirectory();
.
.
.
Image(image: AssetImage('${directory.path}/filename')) // /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers/Data/Application/46EF23F5-840C-47D4-8314-B97CDEE2198C/Documents
.
.
.
Actually I need to use FileImage instead of Asset.
Thanks for GrahamD

Is it possible to change the location of sorted video of image_picker plugin?

I am picking video from camera in this way. and using image_picker: ^0.6.7+22 plugin.
final _picker = ImagePicker();
Future<String> recordAndGetVideo()async{
PickedFile file = await _picker.getVideo(source: ImageSource.camera);
if(file != null){
return file.path;
}
return null;
}
After record ad video it stored the video and final path is this path. /storage/emulated/0/Android/data/com.example.app/files/Pictures/a190e227-a42c-4b09-bad8-4a9591454ff64584234230431626592.mp4
Now is it possible to store that video is different dir ? like /storage/emulated/0/Example App/a190e227-a42c-4b09-bad8-4a9591454ff64584234230431626592.mp4
Yes it is possible, But you have to re-write the file from one location to another.
This is the only way.