Fetching data from memory in MediaPlayer

Scenario: Encrypted mp3 files in my .apk. You must decrypt and send the MediaPlayer object.

Problem: After I read the files and decrypted them, how do I get MediaPlayer to play them?

Now. MediaPlayer has 4 versions of setDataSource ().

setDataSource(String path) setDataSource(FileDescriptor fd) setDataSource(FileDescriptor fd, long offset, long length) setDataSource(Context context, Uri uri) 

None of them are ideal for the situation. Guess idea is to provide MediaPlayer InputStream?

Possible solutions:

  • Write decrypted data to the file this file. A lot of IO overhead.
  • Create a dummy http server (ServerSocket?) And pass the URL of the media player. Again dirty. I am even allowed to create a socket.

Does anyone have a better solution?

+4
source share
3 answers
 byte[] callData = ...; String base64EncodedString = Base64.encodeToString(callData, Base64.DEFAULT); try { String url = "data:audio/amr;base64,"+base64EncodedString; MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); mediaPlayer.start(); } catch(Exception ex){ ... } 
+1
source

If you do not need all the features in MediaPlayer, I recommend trying AudioTrack . It basically meant what you are describing. Unfortunately, MediaPlayer does not use AudioTrack in its constructor, so the best solution in this case is to include a dummy Http server that will send your data back from the URL (which recommends recommendations for the release of Android 1.0).

+1
source

I am not 100% sure, but I do not think that you have another option than temporarily saving the decrypted file before playing it.

This question is similar, but I do not think that you are using the simple solution offered there, since you have an encrypted file. There is also a link to a tutorial for Custom Audio Streaming with MediaPlayer , but it looks like their solution also uses a temporary file.

0
source

All Articles