开发者

How to play an audio stream of bytes in android?

开发者 https://www.devze.com 2023-01-24 18:41 出处:网络
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? Med

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 param开发者_运维百科eter, so i cannot consider that.


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.


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


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).
0

精彩评论

暂无评论...
验证码 换一张
取 消