How to play a video from Amazon S3 in Android App?
First we have to provide the name of our bucket and the object (see aws-android-sdk-1.4.3/samples/S3_SimpleDB_SNS_SQS_Demo
for a complet guide) we want to open then get the URL to our object:
AWSCredentials myCredentials = new BasicAWSCredentials("YOUR_AMAZON_ACCESS_KEY_ID", "YOUR_AMAZON_SECRET_KEY_ID");
AmazonS3 s3client = new AmazonS3Client(myCredentials);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
URL objectURL = s3client.generatePresignedUrl(request);
Now, just play the video in a video view, supplying the URL obtained:
getWindow().setFormat(PixelFormat.TRANSLUCENT);
mediaCtrl = new MediaController(this);
mediaCtrl.setMediaPlayer(videoView);
videoView.setMediaController(mediaCtrl);
Uri clip = Uri.parse(objectURL.toString());
videoView.setVideoURI(clip);
videoView.requestFocus();
videoView.start();
I want to give thanks to @CommonsWare for
indicating me through
REST API
(even the code I used is fromaws-sdk
reading theREST API
documentation helped me and show also other ways of requesting Amazon objects)indicating me to use
generatePresignedUrl()
the code for playing the video is also inspired from his materials.
1.Using the method provided above, how could I get a video from ByteArrayOutputStream (ByteArrayOutputStream.toString()) and play it in a VideoView or using MediaPlayer or an approach... ?
Maybe you could get it to work by publishing the byte array through a ContentProvider
and openFile()
. Here is a sample project where I demonstrate serving a file by means of a custom InputStream
this way.
The media subsystem is rather fussy, though, and so I do not give you good odds on this working.
2 . Does anybody know any other solution to this problem of preview videos stored on Amazon ? (I heard that on their sdk for IOS they use URLs to access files...)
Last I checked, S3 had a REST API that you could use to generate URLs to the videos. I'd hand that URL to MediaPlayer
or VideoView
.
Supplying the file URL and open it in browser does not make sense, because this URLs expire after a wile.
But you control how long "a wile [sic]" is. Make it be 24 hours or something.