Android intent for playing video?
Solution 1
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.
Solution 2
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.
Solution 3
following code works just fine for me.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieurl));
startActivity(intent);
Solution 4
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.
PanMan
Updated on July 08, 2022Comments
-
PanMan almost 2 years
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!
-
AndroidDev over 12 yearsSo 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.
-
Timmmm almost 12 yearsIt 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.
-
marden over 11 yearsSave the parsed uri to a variable. There's no need parse it twice.
-
Russ about 11 years-1 That is exactly what the original poster had used (setDataAndType)
-
Carsten Hoffmann over 9 yearsI don't get why this answer got so many upvotes.. It solves not a single dime of the posted question...
-
Christian over 9 yearsI 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.
-
abbasalim about 8 yearsNo Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/DCIM/Camera/VID_۲۰۱۶۰۳۰۶_۰۰۳۲۳۸.mp4 typ=video/mp4 } :(
-
abbasalim about 8 yearsupdate: this worked: intent.setDataAndType(Uri.fromFile(new File(path)), "video/mp4");
-
Binoy Babu over 7 yearsThis won't work in API 24+, due to file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24.
-
Rahul Khurana over 5 yearsworking on API 28. Tested on Nexus 5 with web url video
-
Vivek Thummar over 4 yearsI'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 intent
-
user924 almost 4 yearswhy do you set second paramter? it's the same as calling
setData()
, but then you callsetDataAndType()
anyway -
user924 almost 4 years@marden no need to to set it twice as well
-
user924 almost 4 years
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.example.videotest.fileprovider
-
t0m over 2 yearsWhat if I want to play a video with subtitles? How can I do that?