This extension allows the playback of encrypted audios through audio_service plugin.
Basically this is an implementation of StreamAudioSource
, mentioned in just_audio documentation.
The stream to be listened to will be the encrypted bytes stream. Consequently, a decryption function is needed, which will process and store the decrypted bytes internally.
encryptedStream.doOnData((encryptedBytes) {
final List<int> decryptedBytes = _decrypter.call(encryptedBytes);
_decryptedBytes.addAll(decryptedBytes);
})
The first step is to define the encryption logic to be implemented. Consider using packages like pointycastle, encrypt or crypto. Either way, you will need a decryption function to handle the encrypted byte stream.
class MyEncrypter {
static List<int> encrypt(List<int> originalBytes) {
///
}
static List<int> decrypt(List<int> encryptedBytes) {
///
}
}
Once this is done, provide a stream of the audio file's encrypted data, and your decrypter to a new EncryptedAudioSource
. For demonstration purposes, the example was implemented using a StreamedResponse
.
Then, provide your EncryptedAudioSource
to your AudioPlayer
, via setAudioSource
method.
// [...]
final player = AudioPlayer();
final Uri uri = Uri.parse("URL_OF_ENCRYPTED_AUDIO");
final http.Request request = http.Request('GET', uri);
final StreamedResponse response = await request.send();
final ByteStream stream = response.stream;
final myCustomSource = EncryptedAudioSource(
encryptedStream: stream,
decrypter: MyEncrypter.decrypt
sourceLength: response.contentLength,
contentType: 'audio/mpeg'
);
player.setAudioSource(myCustomSource);
/// You can handle the delayed duration result through [myCustomSource.totalDurationStream]
// [...]
After all this, you will be able to play your encrypted audios, through the implementation of audio_service.