使用 React Native 在 android 上的默认视频播放器中启动 url

Launch url in default video player on android with react native

我找到了这个本机 android 代码来实现我想要实现的目标

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

(Android intent for playing video?)

但我不知道如何在 Linking.sendIntent api 的 React Native 中使用它,或者 api 是否能够做到这一点。

我也尝试了 this 模块,但它未能构建项目,错误为 method does not override or implement a method from a supertype

我不想为此编写本机模块。

事实证明,您无法使用内置的 sendIntent api 意图发送数据,但是有一个方便的库可以做到这一点 (react-native-send-intent).

所以现在我可以实现我想要的效果了:

import { Linking, Platform } from "react-native";
import SendIntentAndroid from "react-native-send-intent";

export function playVideo(url){
    var fn = Platform.select({
        android(){
            SendIntentAndroid.openAppWithData(
                /* "org.videolan.vlc" */null,
                "https://www.w3schools.com/html/mov_bbb.mp4",
                "video/*"
            ).then(wasOpened => {});
        },
        default(){
            Linking.openURL(url).catch(err => {});
        }
    });
    fn();
    
}

尽管库没有提供启动默认应用程序的功能 url,您可以通过传入 null 作为包名来实现它,因为它在幕后使用的功能 Intent.setPackage(String packageName),接受 null 作为值。