Android intent for playing video?
Asked Answered
T

7

65

I'm trying to play video's on Android, by launching an intent. The code I'm using is:

tostart = new Intent(Intent.ACTION_VIEW);
tostart.setDataAndType(Uri.parse(movieurl), "video/*");
startActivity(tostart); 

This works on most phones, but not on the HTC Hero. It seems to load a bit different video player. This does play the first video thrown at it. However, every video after that it doesn't respond. (it keeps in some loop).

If I add an explicit

tostart.setClassName("com.htc.album","com.htc.album.ViewVideo");

(before the startactivity) it does work on the HTC Hero. However, since this is a HTC specific call, I can't run this code on other phones (such as the G1). On the G1, this works:

tostart.setClassName("com.android.camera","com.android.camera.MovieView"); //g1 version

But this intent is missing from the hero. Does anybody know a list of intents/classnames that should be supported by all Android devices? Or a specific one to launch a video? Thanks!

Theomancy answered 15/10, 2009 at 12:39 Comment(1)
Did you get any solution for this ??Frost
C
95

Use setDataAndType on the Intent

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(newVideoPath));
intent.setDataAndType(Uri.parse(newVideoPath), "video/mp4");
startActivity(intent);

Use "video/mp4" as MIME or use "video/*" if you don't know the type.

Edit: This not valid for general use. It fixes a bug in old HTC devices which required the URI in both the intent constructor and be set afterwards.

Callender answered 7/1, 2013 at 10:41 Comment(12)
Save the parsed uri to a variable. There's no need parse it twice.Finical
-1 That is exactly what the original poster had used (setDataAndType)Lobe
I don't get why this answer got so many upvotes.. It solves not a single dime of the posted question...Bruch
I know it shouldn't - but it does... Adding the uri BOTH in the intent constructor and setDataAndType makes some HTC devices play the video. That's the small difference from what PanMan originally tried.Callender
No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/DCIM/Camera/VID_۲۰۱۶۰۳۰۶_۰۰۳۲۳۸.mp4 typ=video/mp4 } :(Helbona
update: this worked: intent.setDataAndType(Uri.fromFile(new File(path)), "video/mp4");Helbona
This won't work in API 24+, due to file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24.Predesignate
working on API 28. Tested on Nexus 5 with web url videoErgonomics
I'm making a Video player app and from another app i'm opening video using this code,now can anyone tell me how to get video details in Video Player app from this intentBarksdale
why do you set second paramter? it's the same as calling setData(), but then you call setDataAndType() anywayBoggart
@Finical no need to to set it twice as wellBoggart
What if I want to play a video with subtitles? How can I do that?Cristoforo
P
23

From now onwards after API 24, Uri.parse(filePath) won't work. You need to use this

final File videoFile = new File("path to your video file");
Uri fileUri = FileProvider.getUriForFile(mContext, "{yourpackagename}.fileprovider", videoFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "video/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//DO NOT FORGET THIS EVER
startActivity(intent);

But before using this you need to understand how file provider works. Go to official document link to understand file provider better.

Pantin answered 18/1, 2018 at 13:34 Comment(1)
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.example.videotest.fileproviderBoggart
L
11

I have come across this with the Hero, using what I thought was a published API. In the end, I used a test to see if the intent could be received:

private boolean isCallable(Intent intent) {
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
        PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

In use when I would usually just start the activity:

final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
if (isCallable(intent)) {
    // call the intent as you intended.
} else {
    // make alternative arrangements.
}

obvious: If you go down this route - using non-public APIs - you must absolutely provide a fallback which you know definitely works. It doesn't have to be perfect, it can be a Toast saying that this is unsupported for this handset/device, but you should avoid an uncaught exception. end obvious.


I find the Open Intents Registry of Intents Protocols quite useful, but I haven't found the equivalent of a TCK type list of intents which absolutely must be supported, and examples of what apps do different handsets.

Lallation answered 23/10, 2009 at 16:22 Comment(0)
M
11

following code works just fine for me.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieurl));
startActivity(intent);
Many answered 16/11, 2011 at 0:27 Comment(2)
So thats fine, but how to stop the audio once the user presses the back button on the Audio Screen. The audio just keeps on playing.Eterne
It works but it opens the browser first because it doesn't know that it is a video. I do know that it is a video so I want to pass the URL directly to a video player.Topsyturvydom
J
0

from the debug info, it seems that the VideoIntent from the MainActivity cannot send the path of the video to VideoActivity. It gives a NullPointerException error from the uriString. I think some of that code from VideoActivity:

Intent myIntent = getIntent();
String uri = myIntent.getStringExtra("uri");
Bundle b = myIntent.getExtras();

startVideo(b.getString(uri));

Cannot receive the uri from here:

public void playsquirrelmp4(View v) {
    Intent VideoIntent = (new Intent(this, VideoActivity.class));
    VideoIntent.putExtra("android.resource://" + getPackageName()
        + "/"+   R.raw.squirrel, uri);
    startActivity(VideoIntent);
}
Jamesy answered 15/4, 2013 at 16:40 Comment(0)
D
0

First you need to convert path to real path. For example if you have path like content://folder/123 You need to convert it to path like foldername/fil.mp4 with Environment.getExternalStorageDirectory()

So your path string will be: String path = Environment.getExternalStorageDirectory() + "foldername/file.mp4"; Then you need to convert it to file:

File file = new File(path);

And in the end use this in the line:

intent.setDataAndType(Uri.fromFile(file), "video/*");

Dermatophyte answered 22/3, 2021 at 15:59 Comment(0)
P
0

In Kotlin and Also working in Android Version 13+

 private fun openVideo() {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("YourPath"))
    intent.setDataAndType(Uri.parse("YourPath"), "video/mp4")
    startActivity(intent)
}
Pease answered 26/3, 2024 at 11:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.