简体   繁体   English

Android - 从扩展文件播放MP4视频文件

[英]Android - Playing MP4 video file from an expansion file

I have followed all the steps in testing an expansion file for my application. 我已经按照测试应用程序扩展文件的所有步骤进行了操作。 Where I am stuck now is how to play a video file contained in that expansion file. 我现在被困在哪里是如何播放该扩展文件中包含的视频文件。

I have successfully been able to download it from Google Play, and I can see it in the folder it is supposed to be in when I browse through my file manager. 我已经成功地从Google Play下载了它,当我浏览文件管理器时,我可以在它应该位于的文件夹中看到它。

I have looked for the past couple of days in trying to figure out how to play the file. 在过去的几天里,我一直试图弄清楚如何播放文件。 At the moment there is only one for, to maintain appropriate size for testing. 目前只有一个,以保持适当的测试尺寸。

I have tried to parse the URI and set it in the videoview, however, it always shows that the video cannot be played. 我试图解析URI并在视频视图中设置它,但是,它始终显示无法播放视频。

I have also tried this code so far to access it (as read in the documentation, and I have replaced the parameters with my parameters): 到目前为止,我还尝试过这段代码来访问它(如文档中所述,我已经用参数替换了参数):

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

What do I do with the input stream in order to play the video? 我如何处理输入流才能播放视频?

Code for provider 供应商代码

public class EHZipUriProvider extends APEZProvider {

@Override
public String getAuthority() {
    // TODO Auto-generated method stub
    return "your.package.name.Utility.EHZipUriProvider";
}

} }

Custom video player 定制视频播放器

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

Calling in activity 打电话给活动

VideoPlayer.playVideo(context, vv, "rulesofenglish.mp4");

For example, my application package name is: 例如,我的应用程序包名称是:

com.bluewindsolution.android.sampleapp com.bluewindsolution.android.sampleapp

Then my expansion file should be 然后我的扩展文件应该是

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

In detail, you can follow in here : 详细信息,您可以在此处关注

[main|patch].< expansion-version >.< package-name >.obb [main | patch]。<expansion-version>。<package-name> .obb


From expansion you can get it via three ways: 从扩展开始,您可以通过三种方式获得它:

via AssetFileDescriptor 通过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);
}

Via InputStream: 通过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);

Via Uri 通过Uri

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

Using a provider (the best solution for me)! 使用提供商(对我来说是最好的解决方案)! I tested it, and it works perfectly. 我测试了它,它完美无缺。

See Android: read media file from .obb using APEZ . 请参阅Android:使用APEZ从.obb读取媒体文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM