[英]Android - Playing MP4 video file from an expansion file
我已經按照測試應用程序擴展文件的所有步驟進行了操作。 我現在被困在哪里是如何播放該擴展文件中包含的視頻文件。
我已經成功地從Google Play下載了它,當我瀏覽文件管理器時,我可以在它應該位於的文件夾中看到它。
在過去的幾天里,我一直試圖弄清楚如何播放文件。 目前只有一個,以保持適當的測試尺寸。
我試圖解析URI並在視頻視圖中設置它,但是,它始終顯示無法播放視頻。
到目前為止,我還嘗試過這段代碼來訪問它(如文檔中所述,我已經用參數替換了參數):
// Get a ZipResourceFile representing a merger of both the main and patch files
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(appContext,
mainVersion, patchVersion);
// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
我如何處理輸入流才能播放視頻?
public class EHZipUriProvider extends APEZProvider {
@Override
public String getAuthority() {
// TODO Auto-generated method stub
return "your.package.name.Utility.EHZipUriProvider";
}
}
public static final Uri CONTENT_URI = Uri
.parse("content://your.package.name.Utility.EHZipUriProvider");
public static VideoView mview;
public static void playVideo(Context context, VideoView resource, String VIDEO_NAME) {
position = 0;
mview = resource;
Uri video = Uri.parse((CONTENT_URI + "/" + VIDEO_NAME));
mview.setVideoURI(video);
mview.start();
mview.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mview = null;
}
});
}
VideoPlayer.playVideo(context, vv, "rulesofenglish.mp4");
例如,我的應用程序包名稱是:
com.bluewindsolution.android.sampleapp
然后我的擴展文件應該是
main.1.com.bluewindsolution.android.sampleapp.obb
詳細信息,您可以在此處關注 :
[main | patch]。<expansion-version>。<package-name> .obb
從擴展開始,您可以通過三種方式獲得它:
通過AssetFileDescriptor
// This one works with an image file and a media file.
// Get a ZipResourceFile representing a specific expansion file
// mainContext, version number of your main expansion, version number of your patch
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 1, 0);
AssetFileDescriptor afd_img1 = expansionFile.getAssetFileDescriptor("your_path/your_file.jpg");
AssetFileDescriptor afd_mpbg = expansionFile.getAssetFileDescriptor("your_path/your_file.mp3");
// To set image
ImageView imageView1 = (ImageView)findViewById(R.id.iv1);
imageView1.setImageBitmap(BitmapFactory.decodeFileDescriptor(afd_iv1.getFileDescriptor()));
// To set sound
try {
mpbg.setDataSource(afd_mpbg.getFileDescriptor(), afd_mpbg.getStartOffset(), afd_mpbg.getDeclaredLength());
mpbg.prepareAsync();
mpbg.start();
} catch (IllegalStateException e) {
Log.w("Error=====", "Failed to prepare media player", e);
}
通過InputStream:
// This one works with an image file and a media file.
// Get a ZipResourceFile representing a specific expansion file
ZipResourceFile expansionFile = new ZipResourceFile(filePathToMyZip);
// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
// This one can use in almost thing such as jpg, mp3, mp4, pdf, etc.
// You need to create a new class to override APEZProvider
public class ZipFileContentProvider extends APEZProvider {
@Override
public String getAuthority() {
return "com.bluewindsolution.android.sampleapp";
}
}
// You need to add <provider> in AndroidManifest.xml
<provider
android:name="com.bluewindsolution.android.sampleapp.ZipFileContentProvider"
android:authorities="com.bluewindsolution.android.sampleapp"
android:exported="false"
android:multiprocess="true"
>
<meta-data android:name="mainVersion"
android:value="1"></meta-data>
</provider>
// After that, you can use it any time by this code:
String filename2 = "image/i_commu_a_1_1_1_1.jpg"; // suppose you store put your file in directory in .obb
String uriString2 = "content://com.bluewindsolution.android.sampleapp" + File.separator + filename2;
Uri uri2 = Uri.parse(uriString2);
imageView2.setImageURI(uri2);
// Filename inside my expansion file
String filename = "video/v_do_1_1_1.mp4"; // Suppose you store put your file in directory in .obb
String uriString = "content://com.bw.sample_extension_download_main" + File.separator + filename;
Uri uri = Uri.parse(uriString);
v1 = (VideoView)findViewById(R.id.videoView1);
v1.setVideoURI(uri);
v1.start();
使用提供商(對我來說是最好的解決方案)! 我測試了它,它完美無缺。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.