Decrypting audio/video on-the-fly to MediaPlayer - android

I have been looking around for few days, but unable to get a clear answer on how to do it.
I have some encrypted audio/video files on my sdcard, which I would like to play which it will send to MediaPlayer, and start playing while decryption is working on the same time. It kinds of creates a buffer initially, and when it has enough initial data to start playing, MediaPlayer will start playing, and on background decrypting and keep sending the data over.
I looked around and most of the solution seem to point to having a localhttpserver, and then send the file to the server, and when it comes back, it will start playing the audio/video like a live stream. What I don't know is where does the decryption code comes in? In the server side? If so, in which part of it?
I have also seen this http://libeasy.alwaysdata.net/ solution, that possibly able to solve it, but I can't seem to understand how that works. I know that it creates a localserver, but on the Cipher part, how does my own decryption comes in play?
private void myPlay(String path) {
mServer = new LocalSingleHttpServer();
mServer.setCipher(myGetCipher());
mServer.start();
path = mServer.getURL(path);
mVideoView.setVideoPath(path);
mVideoView.start();
}
public void onCompletion(MediaPlayer mp) { // MediaPlayer.OnCompletionListener interface
mServer.stop();
}
As I am quite new to Android development, so do bear with me if I am unclear or anything.
Thank you.

The decryption is done by the http server, if you provide it a Cipher.
Code something like this (you will have to add the exception catching somewhere):
import javax.crypto.Cipher;
private Cipher myGetCipher() {
Cipher c = Cipher.getInstance("RC4");
c.init(Cipher.DECRYPT_MODE, new SecretKeySpec("myPassword".getBytes(), "RC4"));
return c
}

Related

Get Icy metadata from audio stream with Exoplayer2 without using duplicate streams

I thought I found a way to do this, but it seems like this is for exoplayer1 (the first answer to this question): Extracting metadata from Icecast stream using Exoplayer
I already have a way to read Icy metadata, but it uses a stream of it's own, so it creates extra data costs for the user.
How can this be done just using one instance of the stream?
This can be done easily by registering a listener for your ExoPlayer instance:
exoPlayer.addListener(new Player.Listener() {
#Override
public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
String title = (String)mediaMetadata.title; // This is the artist / song
}
}

Using MediaRecorder to write to a buffer or FIFO

I am developing a low data rate VoIP kind of project . I need to capture audio at low data rates and store it in an internal buffer or FIFO (NOT in a file).
I would like to use low data rate .AMR encoders, which means AudioRecord is out. MediaRecorder looks like it does exactly what I want except that it seems to write to a file.
MediaRecorder takes a FileDescriptor... is there any way I can write a class that implements the FileDescriptor interface... acting as a sync for bytes... but instead of sending them to a file they are stored in a buffer? The documentation on FileDescriptor specifically says that Applications shouldn't write their own but why not and is it possible anyway?
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileDescriptor.html
In short, I'd like to develop my own stream, and trick MediaRecorder to send data to it. Perhaps doing something tricky with opening both ends of a socket within the same APK and giving MediaRecorder the socket to write to? Using the socket as my FIFO? I'm somewhat new to this so any help/suggestions greatly appreciated.
I have a related question on the RX side. I'd like to have a buffer/fifo that feeds MediaPlayer. Can I trick MediaPlayer to accept data from a buffer fed by my own proprietary stream?
I know its a bit late to answer this question now...
...But if it helps here's the solution.
Android MediaRecorder's method setOutputFile() accepts FileDescriptor as a parameter.
As for your need a unix data pipe could be created and its FD could be passed as an argument in the following manner...
mediaRecorder.setOutputFile(getPipeFD());
FileDescriptor getPipeFD()
{
final String FUNCTION = "getPipeFD";
FileDescriptor outputPipe = null;
try
{
ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
outputPipe = pipe[1].getFileDescriptor();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
return outputPipe;
}
The ParcelFileDescriptor.createPipe() creates a Unix Data Pipe and returns an array of ParcelFileDescriptors. The first object refers to the read channel (Source Channel) and the second one refers to the write channel (Sink Channel) of the pipe. Use MediaRecorder object to write the recorded data to the write channel...
As far as MediaPlayer is concerned the same technique could be used by passing the FileDescriptor object related to the created pipe's read channel to the setDataSource() method...

Can one retrieve data from a MediaPlayer's stream?

If I were to stream some sort of media to a MediaPlayer, is there any way I could copy it before/as/after it is played? For instance, if I were to stream a YouTube clip, is it possible to save that clip as it is being played?
Edit:
(Ocelot's answer made me realise how localised this question is).
What I am looking to do is copy the stream of a MediaPlayer already in progress (be it youtube or music stream). I want to be able to be notified when a new stream starts and ends. So far the only thing I found (for the latter) that is even remotely close it the broadcast string ACTION_AUDIO_BECOMING_NOISY but that doesn't really do anything for what I need. I there any way to do this?
I haven't tested this, and it looks like quite a bit of work, but here is what I would try:
Create a subclass of Socket. In this class, you can handle all byte reads, and save the stream locally or do whatever you want with it
Create your own content provider, which you can use to pass URIs to your media player, in your own format. Example: mystream://youtube.com/watch?v=3Rhy37u
In your content provider, override the openFile method and in it, open your own socket, and create a ParcelFileDescriptor with it.
Now, simply passing the new format url to your mediaplayer should make all streams go through your Socket, where you can save your data.
one way is to first find out where the video is in the server for exmaple in youtube with simple regex like this :
Regex("(?<=&t=)[^&]*").Match(file).Value;
you could retrieve url to the video and then download it like
public static void Download(string videoID, string newFilePath)
{
WebClient wc = new WebClient();
string file = wc.DownloadString(string.Format("http://www.youtube.com/watch?v={0}", videoID));
string t = new Regex("(?<=&t=)[^&]*").Match(file).Value;
wc.DownloadFile(string.Format("http://www.youtube.com/get_video?t={0}=&video_id={1}",t,videoID), newFilePath);
}
it's c# code but you could easily convert it to java.
#zrgiu
I tried to go with this solution, but the MediaPlayer retrieves a FileDescriptor from the URI, so sadly no http URL can be passed like this.
I also found another solution, it suggests to create a local ProxyServer on your device to serve files from the internet, it should be possible to also save the files streamed via the proxy.

How to play online radio in android

I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();

Feeding data from memory to MediaPlayer

Scenario:
Have encrypted mp3 files in my .apk. Need to decrypt and send to MediaPlayer object.
Problem:
After I read the files and decrypt 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 which are ideal for the situation. Guess ideal would be to give MediaPlayer an InputStream ?
Possible solutions:
Write decrypted data to file play
that file. A lot of IO overhead.
Create a dummy http server
(ServerSocket ?) and pass the url to
MediaPlayer. Again, messy. Am I
even allowed to create a socket.
Does anyone have a better solution ?
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){
...
}
If you don't need all the functionality in MediaPlayer, I recommend trying AudioTrack. It's meant for basically what you describe. Unfortunately, MediaPlayer doesn't take an AudioTrack in its constructor, so the best solution in that case is to include a dummy Http server that sends your data back from a URL (which is what the Android 1.0 release notes recommends).
I'm not a 100% sure, but I don't think you have any other option than to temporarily save the the decrypted file before playing it.
This question is kind of similar, but I don't think you use the easy solution suggested there since you have an encrypted file. There is also provided a link to a tutorial for Custom Audio Streaming with MediaPlayer, but it seems like their solution also use a temporary file.

Categories

Resources