3

我已按照所有步骤为我的应用程序测试扩展文件。我现在陷入困境的是如何播放该扩展文件中包含的视频文件。

我已经成功地从 Google Play 下载了它,当我浏览我的文件管理器时,我可以在它应该在的文件夹中看到它。

过去几天我一直在寻找如何播放该文件。目前只有一个用于,以保持适当的大小进行测试。

我试图解析 URI 并将其设置在 videoview 中,但是,它总是显示无法播放视频。

到目前为止,我还尝试过这段代码来访问它(如文档中所述,我已经用我的参数替换了参数):

// 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);

为了播放视频,我该如何处理输入流?

4

3 回答 3

0

提供者代码

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");
于 2013-11-14T10:48:17.103 回答
0

比如我的应用包名是:

com.bluewindsolution.android.sampleapp

那么我的扩展文件应该是

main.1.com.bluewindsolution.android.sampleapp.obb

详细来说,你可以在这里关注:

[main|patch].<扩展版本>.<包名>.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);
}

通过输入流:

// 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();
于 2015-06-19T18:03:56.173 回答
0

使用提供商(对我来说最好的解决方案)!我测试了它,它工作得很好。

请参阅Android:使用 APEZ 从 .obb 读取媒体文件

于 2015-06-22T09:09:18.323 回答