Home > Back-end >  Flutter download audio zip file and play in application
Flutter download audio zip file and play in application

Time:09-09

Hello friend i am new in flutter i need help i have project where almost different zip file hosted on sever every zip file have different audio with categories now i want if user want to download that zip file and extract audio then able to play in application it not visible in phone memory ?here example of code but this is for images and one zip file i take code from this url

https://www.coderzheaven.com/2019/06/07/download-zip-extract-it-and-show-the-images-file-in-list-in-ui/

here my code

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:archive/archive.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
 
class DownloadAssetsDemo extends StatefulWidget {
  DownloadAssetsDemo() : super();
 
  final String title = "Download & Extract ZIP Demo";
 
  @override
  DownloadAssetsDemoState createState() => DownloadAssetsDemoState();
}
 
class DownloadAssetsDemoState extends State<DownloadAssetsDemo> {
  //
  bool _downloading;
  String _dir;
  List<String> _images, _tempImages;
  String _zipPath = 'https://coderzheaven.com/youtube_flutter/images.zip';
  String _localZipFileName = 'images.zip';
 
  @override
  void initState() {
    super.initState();
    _images = List();
    _tempImages = List();
    _downloading = false;
    _initDir();
  }
 
  _initDir() async {
    if (null == _dir) {
      _dir = (await getApplicationDocumentsDirectory()).path;
    }
  }
 
  Future<File> _downloadFile(String url, String fileName) async {
    var req = await http.Client().get(Uri.parse(url));
    var file = File('$_dir/$fileName');
    return file.writeAsBytes(req.bodyBytes);
  }
 
  Future<void> _downloadZip() async {
    setState(() {
      _downloading = true;
    });
 
    _images.clear();
    _tempImages.clear();
 
    var zippedFile = await _downloadFile(_zipPath, _localZipFileName);
    await unarchiveAndSave(zippedFile);
 
    setState(() {
      _images.addAll(_tempImages);
      _downloading = false;
    });
  }
 
  unarchiveAndSave(var zippedFile) async {
    var bytes = zippedFile.readAsBytesSync();
    var archive = ZipDecoder().decodeBytes(bytes);
    for (var file in archive) {
      var fileName = '$_dir/${file.name}';
      if (file.isFile) {
        var outFile = File(fileName);
        //print('File:: '   outFile.path);
        _tempImages.add(outFile.path);
        outFile = await outFile.create(recursive: true);
        await outFile.writeAsBytes(file.content);
      }
    }
  }
 
  buildList() {
    return Expanded(
      child: ListView.builder(
        itemCount: _images.length,
        itemBuilder: (BuildContext context, int index) {
          return Image.file(
            File(_images[index]),
            fit: BoxFit.fitWidth,
          );
        },
      ),
    );
  }
 
  progress() {
    return Container(
      width: 25,
      height: 25,
      padding: EdgeInsets.fromLTRB(0.0, 20.0, 10.0, 20.0),
      child: CircularProgressIndicator(
        strokeWidth: 3.0,
        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
      ),
    );
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          _downloading ? progress() : Container(),
          IconButton(
            icon: Icon(Icons.file_download),
            onPressed: () {
              _downloadZip();
            },
          ),
        ],
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            buildList(),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

    _initDir() async {
        if (null == _dir) {
          _dir = (await getApplicationDocumentsDirectory()).path;
          print("current dir--$_dir")
        }
      }

use like this

  • Related