Android意图播放视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1572107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:17:31  来源:igfitidea点击:

Android intent for playing video?

androidvideosdkandroid-intent

提问by PanMan

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

我正在尝试通过启动意图在 Android 上播放视频。我正在使用的代码是:

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).

这适用于大多数手机,但不适用于HTC Hero。它似乎加载了一个有点不同的视频播放器。这确实播放了第一个视频。但是,此后的每个视频都没有响应。(它保持在某个循环中)。

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 HTCspecific call, I can't run this code on other phones (such as the G1). On the G1, this works:

(在 startactivity 之前)它确实适用于HTC Hero。但是,由于这是HTC特定的调用,我无法在其他手机(例如G1)上运行此代码。在G1 上,这有效:

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!

但是这个意图在英雄身上是缺失的。有人知道所有 Android 设备都应该支持的意图/类名列表吗?或者特定的一个来发布视频?谢谢!

回答by Christian

Use setDataAndType on the Intent

在意图上使用 setDataAndType

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.

如果您不知道类型,请使用“video/mp4”作为 MIME 或使用“video/*”。

回答by Kishan Solanki

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

从现在开始 API 24 之后,Uri.parse(filePath)将无法工作。你需要使用这个

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 linkto understand file provider better.

但在使用它之前,您需要了解文件提供程序的工作原理。转到官方文档链接以更好地了解文件提供者。

回答by jamesh

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:

我在 Hero 中遇到过这个问题,使用的是我认为是已发布的 API。最后我用了一个测试,看看能不能收到intent:

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.

显而易见:如果你沿着这条路走下去——使用非公共 API——你必须绝对提供一个你知道肯定有效的回退。它不一定是完美的,它可以是一个 Toast,表示此手机/设备不支持此功能,但您应该避免出现未捕获的异常。结束明显。



I find the Open Intents Registry of Intents Protocolsquite 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.

我发现Intents ProtocolsOpen Intents Registry非常有用,但我还没有找到绝对必须支持的 TCK 类型意图列表,以及应用程序在不同手机上运行的示例。

回答by Chang

following code works just fine for me.

以下代码对我来说很好用。

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieurl));
startActivity(intent);

回答by Leinad

from the debug info, it seems that the VideoIntentfrom the MainActivitycannot send the path of the video to VideoActivity. It gives a NullPointerExceptionerror from the uriString. I think some of that code from VideoActivity:

从调试信息来看,来自MainActivityVideoIntent似乎无法将视频的路径发送到VideoActivity。它NullPointerExceptionuriString. 我认为其中一些代码来自VideoActivity

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

startVideo(b.getString(uri));

Cannot receive the uri from here:

无法从这里接收 uri:

public void playsquirrelmp4(View v) {
    Intent VideoIntent = (new Intent(this, VideoActivity.class));
    VideoIntent.putExtra("android.resource://" + getPackageName()
        + "/"+   R.raw.squirrel, uri);
    startActivity(VideoIntent);
}