user4830843
user4830843

Reputation: 41

Can't play video through intent

My app has a photo gallery that displays a constant list of images. I need to add one video to that list (just one, provided by me). This video is inside an expansion file. I would like to let the user decide the video player he wants to use to play the video. So I went for the intent approach:

public void playVideo(View view){
    Intent videoint = new Intent(Intent.ACTION_VIEW);
    Uri uri = CustomAPEZProvider.buildUri("test.3gp");
    Log.d("TEST", uri.toString());
    videoint.setDataAndType(uri, "video/*");
    startActivity(videoint);
}

My CustomAPEZProvider is the following:

public class CustomAPEZProvider extends APEZProvider {
    private static final String AUTHORITY = "com.myapp.package.provider";

    @Override
    public String getAuthority() {
        return AUTHORITY;
    }

    public static Uri buildUri(String path) {
        StringBuilder contentPath = new StringBuilder("content://");
        contentPath.append(AUTHORITY);
        contentPath.append(File.separator);
        contentPath.append(path);
        return Uri.parse(contentPath.toString());
    }
}

Also, I added this to my manifest:

<provider android:name="com.myapp.package.CustomAPEZProvider"
    android:authorities="com.myapp.package.provider" >
    android:exported="true"
    android:multiprocess="true">
    <meta-data
        android:name="mainVersion"
        android:value="4"/>
</provider>

The provider has this meta-data because the expansion file version differs from the apps version code.

I understand that the file is being found, but the video players are not able to play it. They are launching the can't play this video window (and no errors). I tested it on many devices and with different kinds of videos. The 3gp video I'm using to test can be played just fine from the phone's native gallery.

Line 3 on the playVideo method is printing this

content://com.myapp.package.provider/test.3gp

This is correct, right?

The expansion file has no folders, files are just thrown at root.

Also, I actually need to play this test.3gp video from the patch expansion file. Will there be any difference in that case? I'm eliminating this obstacle for now. I know I should add it to the provider's meta-data.

Some extra information: the expansion file has several audio files that I'm being able to play using a MediaPlayer without any issues. Of course, this is different because in that case I'm doing it by getting an AssetFileDescriptor to the file inside the obb expansion file, whereas with the video I need an Uri, which changes everything.

I read lots of questions with similar problems, but they were not helpful. Does anyone had the same problem?

Workarounds are also welcome. For example, I could accept to use a VideoView if needed.

UPDATE

I've just realised that the video player is not working, even if the file is a resource (inside drawable, raw, or whatever). I did manage to play the video with the code below:

public void playVideo(View view){
    Uri uri = CustomAPEZProvider.buildUri("test.3gp");
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView videoHolder = new VideoView(this);
    videoHolder.setMediaController(new MediaController(this));
    videoHolder.setVideoURI(uri);
    setContentView(videoHolder);
    videoHolder.start();
}

But this is not exactly what I want, I'd really like to allow the user to choose the video player of his preference. Mainly because I want to free myself from the responsibility to code a nice-to-look-at video player.

Upvotes: 2

Views: 1000

Answers (2)

Ying
Ying

Reputation: 1

I had the same problem (video played without problem with native gallery, but not through intent). How the problem is solved is to add the following line to manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and give the permission to the installed app.

Upvotes: 0

durbnpoisn
durbnpoisn

Reputation: 4659

I don't think the problem lies anywhere in your code. Or, if it does, it's not your biggest problem.

I think your biggest problem is using a 3GP file. That format is not supported by all devices. You're better off with an MP4. And even then, make sure that it's encoded with a CODEC that all Android devices understand.

Upvotes: 1

Related Questions