Vikram
Vikram

Reputation: 12261

How to play an audio stream of bytes in android?

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.

Upvotes: 6

Views: 6743

Answers (3)

Andrey Volk
Andrey Volk

Reputation: 3549

Take a look at the BASS library (free for non-commercial use).
Here is a link for Android: http://www.un4seen.com/forum/?topic=13225.0

There is BASS_StreamCreateFile function:

HSTREAM BASS_StreamCreateFile(
    BOOL mem,
    void *file,
    QWORD offset,
    QWORD length,
    DWORD flags
);

Parameters:

mem     TRUE = stream the file from memory.
file    Filename (mem = FALSE) or a memory location (mem = TRUE).

Upvotes: -2

systempuntoout
systempuntoout

Reputation: 74094

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.

Upvotes: 2

Emmanuel
Emmanuel

Reputation: 17051

This won't be easy. I've done it before on BlackBerry so I bet it's doable on Android too.

If I were you I'd register a new provider for the a custom "encryptedmp3:" protocol. Then I would specify this in a data source for the MediaPlayer:

mediaplayer.setDataSource(this, URI.parse("encryptedmp3://....yourfile"));

I'm not sure how to create a new protocol handler on Android.

I hope this helps a little bit.

Emmanuel

Upvotes: 0

Related Questions