How to play video from apk expansion files? - android

I create developing app in android which have about 500 mb video i use for uploading apk expansion files method i upload expansion and download it but now i cant play videos from it http://blogmobile.itude.com/2012/11/22/expanding-your-horizons-using-expansion-files-for-android/ i follow this example for playing video but it's not working with me http://ktakeda47.blogspot.com/2012/04/apk-expansion-files.html i followed this link for expansion file downloading .Now plz help me how to play videos from expansion file Dir="/sdcard/Android/obb" ?

I'm using the following code to play mp4 from obb APKexpansion file using APKExpansionSupport lib:
ZipResourceFile expansionFile = null;
AssetFileDescriptor mAFD= null;
try {
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 1, 0);
mAFD = expansionFile.getAssetFileDescriptor(strVideoFile);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
if (expansionFile==null || mAFD == null){
Toast.makeText(this, "obb is not here or doesn't contain file: "+strVideoFile, Toast.LENGTH_LONG).show();
return false;
}
mMediaPlayer.setDataSource(mAFD.getFileDescriptor(), mAFD.getStartOffset(), mAFD.getLength());
Remember - you have to pack obb zip archive without any compression level. This is very important. It must be a zip archive with no compression (level=0).
However there is a URI mentioned in APKES docs (speaking of VideoView) but I cant get a glue how to get the URI from APKES. Finally I found how to do it here
So to use URI you has to extend com.android.vending.expansion.zipfile.APEZProvider like in his example and make changes to Manifest and it should work.

Related

How to cache audio for offline use in Android Studio

I am developing an Android application in which I need to get the specified audio file from my website when the user plays it, but I don't want to stream it or download it every time, just the first time. So I was thinking of caching it and play offline whenever the user is in need. So please suggest any method to do so. Or if exists any other method rather than caching like downloading the actual file to file storage and play whenever needed.
If you need to cache files, you should use createTempFile(). For example, the following method extracts the file name from a URL and creates a file with that name in your app's internal cache directory:
private File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null,
context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
You can also see here for more about caching files.
https://developer.android.com/training/data-storage/files.html#WriteCacheFileInternal
Hope this will help.

video compression using silicompressor in android not working

i am trying to compress videos in project therefore using silicompressor. but when i pass it the destination path my application gets and hang and does nothing. But it does create a folder in my storage and stores a video file but when i try to play it, it gives error "Failed to play video". And this file has size of 24 bytes. so take a look and tell what i have done wrong.
Here is my code.
File destinationPath = new File("/storage/emulated/0/DCIM/Camera/myvideo");
destinationPath.mkdir();
File file = new File(destinationPath.getAbsolutePath());
Toast.makeText(Post.this, "folder: " + file, Toast.LENGTH_SHORT).show();
try {
filePath = SiliCompressor.with(Post.this).compressVideo(videouri, file.toString());
video.setVideoURI(Uri.parse(filePath));
Toast.makeText(Post.this, "Completed", Toast.LENGTH_SHORT).show();
} catch (URISyntaxException e) {
Log.d("EXCEPTION", e.toString());
Toast.makeText(Post.this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Try running compression code using AsyncTask
Here you can find demo app code for video compression.
I tried this SiliCompressor for video compressor, which is very well, and in this version, Audio and Video work well but not maintaining the resolution.
https://github.com/Tourenathan-G5organisation/SiliCompressor/tree/v2.2.2
Note: In the latest version(2.2.3) of SiliCompressor audio is not working after video compressor
OR
I made this and working well
https://github.com/iamkdblue/CompressVideo
I hope it helps you.

Android - Save video internal storage and playback

I have saved the sample mp4 video in android internal storage in a file, But when try to read from file and playback in android videoview by parsing file path as a uri , am getting can't play this video error. I had checked many links but no use, How do i implement this.?
final String mFileName = "sample.mp4";
try{
FileOutputStream mFileOutputStream = openFileOutput(mFileName, Context.MODE_PRIVATE );
mFileOutputStream.write(getResources().getAssets().open("sample_video.mp4").read());
mFileOutputStream.close();
Log.v(TAG, "Success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(getFilesDir().getAbsolutePath()+"/sample.mp4");
mVideoView.start();
Simply you can't. VideoView internally uses a MediaPlayer, which explicitly requires a file to be world-readable (app's dirs aren't). The only way to bypass this limitation, as doc says, is to pass a File descriptor rather than the file path.
Unfortunately VideoView does not have a method like setDataSource(FileDescriptor), so you have two ways:
Access the VideoView's MediaPlayer through reflection and call setDataSource(FileDescriptor) on it. Not sure if this is a feasible solution, because the internal media player is instantiated when you call VideoView.setVideoPath, but you should not call it... in short, evaluate it, but it may be a dead end.
Use a MediaPlayer, set a surface holder on which play the video and pass a FileDescriptor directly to the MediaPlayer (imo this second solution is cleaner).

Patching an Android app

I have an Android app with an expansion file already attached. This is all working fine.
The file is: main.2.com.mydomain.myapp.obb
I want to update some assets in my app so I planned on adding a patch expansion file. Going by the next version iteration this file was: patch.10.com.mydomain.myapp.obb
I've uploaded an alpha build and I can see the files are downloading successfully. The assets in question are videos so I have the following code to access them:
ZipResourceFile expansionFile= null;
AssetFileDescriptor fd= null;
try {
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(getApplicationContext(),2,10);
fd = expansionFile.getAssetFileDescriptor("myvideo.mp4");
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
My understanding is that the getAPKExpansionZipFile call will merge the two expansion files but my questions are:
If my patch file contains a file of the same name as the original main expansion, will the patch file version take precedent or should I have new filenames in my patch file (e.g. patch_myvideo.mp4)
How do I know the video will be correctly loaded from the patch file? I tried checking the AssetFileDescriptor offsets (start, length etc..) but it was the same with and without the patch file call.
Would love to know if I'm on the right track here!
Thanks for any help.

How can I play a video file on Android Wearable?

Hi I am trying to play a video file on my Android wear.
The video file is located in the sdcard folder on the wearable device.
But I am unable to play it using my Android application.
If i run the same application on my smartphone (Nexus 5), it works.
How can I play a video file on Android Wearable using my android app.
Android Wear only includes Audio codecs as of Android Wear 5.0 so the normal MediaPlayer APIs won't work. You need to roll your own using video libraries like ffmpeg, ...
Finally I made it work. I don't know if this is something Android Wear specific or a bug, but it turns out that
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
File file = new File(path);
does not give access to the file on Android Wear devices.
Instead one has to convert the file into a temp file first:
InputStream ins = MainActivityBackup.this.getResources().openRawResource (R.raw.hyperlapse2);
File tmpFile = null;
OutputStream output;
try {
tmpFile = File.createTempFile("video","mov");
output = new FileOutputStream(tmpFile);
final byte[] buffer = new byte[102400];
int read;
while ((read = ins.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
output.close();
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
And then it can be loaded into a videoView
mVideoView.setVideoPath(tmpFile.getPath());
Provided you are using your own video decoder or a library like ffmpeg or vitamio, since Android Wear does not support native video playback yet.
I implemented vitamio in roughly 5 minutes, if needed it can be imported via gradle dependency (look for it on gradleplease). Built my app around vitamio videoView and it works like a charm. It can even play flash video.

Categories

Resources