Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play an audio stream of bytes in android?

Tags:

android

audio

I'm developing an android app that will play mp3 files. However the mp3 files are encrypted on the sd card or sqlite. Either ways, after decryption, i'll have a stream of bytes. How do i play them? MediaPlayer does not take inputstream as parameter, so i cannot consider that.

like image 503
Vikram Avatar asked Nov 15 '22 06:11

Vikram


1 Answers

I think you need to store the stream to file system; then you could try using setDataSource method of MediaPlayer

FileInputStream rawmp3file= new FileInputStream(yourByteArrayAsMp3File);
mediaPlayer.setDataSource(rawmp3file.getFD());

If you could switch to PCM audio source, have a look at AudioTrack class.

The AudioTrack class manages and plays a single audio resource for Java applications. It allows to stream PCM audio buffers to the audio hardware for playback. This is achieved by "pushing" the data to the AudioTrack object using one of the write(byte[], int, int) and write(short[], int, int) methods.

like image 103
systempuntoout Avatar answered Jan 03 '23 10:01

systempuntoout