How to decrypt audio file in the form of stream in Flutter/Dart?

283

Firstly, to encrypt or decrypt data, have a look at https://pub.dev/packages/cryptography or https://pub.dev/packages/encrypt or something like that.

Secondly, since you want seeking, it may not be an optimal solution to use streams - the "Stream" abstraction is more like a sequence of data, instead of arbitrarily jumping (seeking) here and there. Instead, divide whole audio (say 1hour) into chunks (say 1minute), and upload/encrypt/decrypt/download each chunk separately and as a whole without using streams. If your chunk is small enough, download/decrypt a chunk will be fast and you do not stuck the UI. If the decrypting is still too slow, have a look at isolates, which are "threads" in Flutter. Run decrypt in a separate isolate then your UI will be smooth.

I need all the usual functions of a music player (e.g. albums, playlists, progress bars with seeking function, etc.)

Seeking is implemented above. For albums/playlists, you may modify any normal audio players in flutter to implement that, or directly implement by your own. it is more like just some UI things, no special things, and anyone familiar with Flutter can write it, no worry.

Share:
283
user1543784
Author by

user1543784

Updated on December 06, 2022

Comments

  • user1543784
    user1543784 over 1 year

    Project Requirement: Music player app which will download audio files, encrypt and save them. The audio files should be playable in the app only. No other app should be able to play the files. Nor the user should be able to copy the files.

    Approach: I don't want the entire decryted audio file to exist at any moment. So I want to encrypt the audio file as soon as it is downloaded. Then when the file is to be played, I want it to be decrypted chunk-by-chunk and played. I believe this can be achieved by using stream. As far as I searched, a package named "just_audio" can play the audio from stream source.

    Problem: I cannot find any encryption package for Flutter/Dart which will output the decrypted data in the form of a stream. This is the first time I am trying to implement encryption/decryption, so my knowledge is very poor in this regard.

    Notes:

    1. The encryption does not need to be heavy. A typical user not being able to copy the files and play them elsewhere would suffice.
    2. Audio files are going to be large, some of them even hours in length.
    3. I need all the usual functions of a music player (e.g. albums, playlists, progress bars with seeking function, etc.)

    Options:

    1. It will be best if there is a package which can do what I need, off the shelf.
    2. To find a basic package and then modifying it into doing what is needed.
    3. Some radically different solution, which takes entirely different path but provides all the solutions.